I’m trying to align a text and an image in a label made with geom_richtext()
but the logo ends up higher than the text. I’ve tried a workaround using the nudge_y
parameter but it doesn’t fix the issue.
Here is a screenshot of the output I’ve managed to get so far:
Besides, I would like to add some space between the € sign and the logo, but I couldn’t find how to do so with sprintf
.
Finally, I guess this is linked to the issue above but you can see that the minus sign is not aligned on all endpoints. For example for the red lign it looks ok but for the blue lign it looks off.
Here is a sample of my dataframe:
structure(list(team = c("Lyon", "Lyon", "Lyon", "Lyon", "Lyon",
"Lyon", "Lyon", "Lyon", "Lyon", "Marseille", "Marseille", "Marseille",
"Marseille", "Marseille", "Marseille", "Marseille", "Marseille",
"Marseille", "PSG", "PSG", "PSG", "PSG", "PSG", "PSG", "PSG",
"PSG", "PSG", "Lyon"), FULLDATE = structure(c(19581, 19581, 19582,
19587, 19588, 19588, 19595, 19595, 19596, 19581, 19581, 19582,
19587, 19588, 19588, 19595, 19595, 19596, 19581, 19581, 19582,
19587, 19588, 19588, 19595, 19595, 19596, 19601), class = "Date"),
image_file = c("~/teamlg/Lyon.png", "~/teamlg/Lyon.png",
"~/teamlg/Lyon.png", "~/teamlg/Lyon.png", "~/teamlg/Lyon.png",
"~/teamlg/Lyon.png", "~/teamlg/Lyon.png", "~/teamlg/Lyon.png",
"~/teamlg/Lyon.png", "~/teamlg/Marseille.png", "~/teamlg/Marseille.png",
"~/teamlg/Marseille.png", "~/teamlg/Marseille.png", "~/teamlg/Marseille.png",
"~/teamlg/Marseille.png", "~/teamlg/Marseille.png", "~/teamlg/Marseille.png",
"~/teamlg/Marseille.png", "~/teamlg/PSG.png", "~/teamlg/PSG.png",
"~/teamlg/PSG.png", "~/teamlg/PSG.png", "~/teamlg/PSG.png",
"~/teamlg/PSG.png", "~/teamlg/PSG.png", "~/teamlg/PSG.png",
"~/teamlg/PSG.png", "~/teamlg/Lyon.png"), Season = c("2023/2024",
"2023/2024", "2023/2024", "2023/2024", "2023/2024", "2023/2024",
"2023/2024", "2023/2024", "2023/2024", "2023/2024", "2023/2024",
"2023/2024", "2023/2024", "2023/2024", "2023/2024", "2023/2024",
"2023/2024", "2023/2024", "2023/2024", "2023/2024", "2023/2024",
"2023/2024", "2023/2024", "2023/2024", "2023/2024", "2023/2024",
"2023/2024", "2023/2024"), League = c("Ligue 1", "Ligue 1",
"Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1",
"Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1",
"Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1",
"Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1",
"Ligue 1", "Ligue 1"), DATEMM = structure(c(2023.58333333333,
2023.58333333333, 2023.58333333333, 2023.58333333333, 2023.58333333333,
2023.58333333333, 2023.58333333333, 2023.58333333333, 2023.58333333333,
2023.58333333333, 2023.58333333333, 2023.58333333333, 2023.58333333333,
2023.58333333333, 2023.58333333333, 2023.58333333333, 2023.58333333333,
2023.58333333333, 2023.58333333333, 2023.58333333333, 2023.58333333333,
2023.58333333333, 2023.58333333333, 2023.58333333333, 2023.58333333333,
2023.58333333333, 2023.58333333333, 2023.66666666667), class = "yearmon"),
bankroll = c(0, 0, -100, -100, -200, -200, -200, -200, -300,
70, 70, 70, -30, -30, -30, 20, 20, 20, 0, -100, -100, -100,
-100, -200, -200, -140, -140, -300), bankroll_interp = c(0,
0, -100, -100, -200, -200, -200, -200, -300, 70, 70, 70,
-30, -30, -30, 20, 20, 20, -50, -50, -100, -100, -150, -150,
-170, -170, -140, -300)), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -28L), groups = structure(list(
team = c("Lyon", "Marseille", "PSG"), .rows = structure(list(
c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 28L), 10:18, 19:27), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L), .drop = TRUE))
And below is the code I’ve written for the graph:
glp<- ggplot(dlf, aes(x=FULLDATE, y = bankroll_interp,group=team,color=team))+
geom_line(linewidth=2.5)+
geom_point(size=8)+
ggtext::geom_richtext(
aes(y=bankroll_interp,
x = FULLDATE,
label = sprintf("%1.0f€ <img src='%s' width='70'/>", bankroll_interp, image_file),
color=team),
hjust = 0,
nudge_y=25,
size=13,
family="Oswald",
inherit.aes = TRUE,
fill = NA, label.color = NA,
label.padding = grid::unit(0.5, "cm"))+
scale_color_manual(values = colteam1) +
scale_x_date(date_labels = "%b-%d",expand = expansion(mult=c(0,0.4))) +
guides(color = guide_none())+
theme_minimal()+
theme(
panel.grid = element_blank(),
panel.grid.major.y = element_line( linewidth = .8, color="#122623",linetype="longdash"),
panel.background = element_rect(colour = NA,fill="transparent"),
legend.position = "none",
axis.line.y = element_line(linewidth = 1.8,colour="#122623",linetype = 1),
axis.line.x = element_line(linewidth=1.8,colour="#122623",linetype = 1),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_text(size=20,face = 'bold',family="Oswald",margin=margin(1, 0, 0, 0, "cm")),
axis.text.y = element_text(size=20,face = 'bold',family="Oswald",margin=margin(0, 1, 0, 0, "cm")),
plot.margin = margin(8, 3, 8, 1, "cm"),
plot.background = element_rect(fill = "#ffffff", color = NA),
plot.subtitle = element_text(size = 25.5, hjust = 0.5, color = "grey",family="Archivo Black",margin=margin(0, 0, 2, 0, "cm")),
plot.caption = element_text(size = 16.5, color = "grey", vjust = 0,family="Archivo Black",margin=margin(0.5, 0, 0, 0, "cm")),
)
showtext_auto()
anim <- glp +
transition_reveal(FULLDATE) +
view_follow(
fixed_y = c(-1200,NA),
fixed_x = FALSE,
)+
ease_aes('linear')
Thanks a lot for your insight