Add geom_rich text to a facet_wrap strip

Hi Everyone,

Is it possible to use rich text on a facet_warp strip?

I usually use this code to add an image to the Lab title or subtitle

"<img src ='/Users/jcosta/Desktop/Reports/Rreports/teamlogos/U23premierleague{teamShortName}.png' "width ='5'/>"

Can I do the same for the strip.text?

Would i need to use it as element_markdown or element_text?

I am using a column called "teamShortName" that has the name of the team as a text in the strip of the facet

Thanks
Julio

1 Like

For better help all the community, remember put a reproducible example. And use a well format like:

You could paste the result of dput(GPSdata[ 1:30, ]) for 1 to 30 rows and all columns.

  teamShortName        position_name            day_name         Total Distance total_duration Max Speed IMA Accel IMA dec

1 Crystal Palace Centre Attacking Midfielder Friday 7728.8 4650.51 28 7 1
2 Chelsea Centre Attacking Midfielder Monday 11066.8 6027.00 27 7 4
3 West Ham. Centre Attacking Midfielder Friday 10742.5 6116.00 30 7 4
4 Chelsea Defender Monday 10959.5 6027.00 33 9 7
5 Crystal Palace Defender Friday 10424.8 5790.00 31 9 3
6 West Ham Defender Friday 11075.7 6116.00 30 8 12
7 Chelsea Central Midfielder Monday 11677.4 6027.00 30 12 11
8 Crystal Palace Central Midfielder Friday 10657.9 5790.00 30 14 5
9 West Ham Central Midfielder Friday 11804.1 6116.00 31 15 7
10 Chelsea Attacker Monday 8077.6 4963.34 32 7 7
11 West Ham Attacker Friday 6997.3 4509.10 33 13 7
12 Crystal Palace Defender Friday 7563.8 4668.95 32 9 5
13 Chelsea Defender Monday 10357.8 6027.00 32 9 1

My code:

"ggplot(data = GPSdata, aes(x = Total Distance, y =Time ))+
geom_point( color = "#007AA3", alpha = 0.3, show.legend = TRUE,size=2)+
geom_point(data =GPSdata%>%filter(athlete_name== paste0(FFCGPS$athlete_name)), aes(x = Total Distance, y =Time ), color = "#db3b48", fill = alpha("#db3b48",1.3),size=2,show.legend = FALSE)+
geom_label_repel(data =GPSdata%>%filter(athlete_name== paste0(FFCGPS$athlete_name)), aes(label = athlete_name),nudge_x =- 0.07, nudge_y = 0.6,fill =alpha("#db3b48",1.3),size = 2.3, color = alpha("white",1.3))+
geom_label_repel(data =GPSdata%>%filter(Total Distance== max(Total Distance)), aes(label = athlete_name),nudge_x = 0.07, nudge_y = 0.4,fill =alpha("#FFFFFF",0.5),size = 2.3, color = alpha("black",0.9))+
geom_label_repel(data =GPSdata%>%filter(Total Distance== min(Total Distance)), aes(label = athlete_name),nudge_x = 0.07, nudge_y = 0.4,fill =alpha("#FFFFFF",0.5),size = 2.3, color = alpha("black",0.9))+
geom_vline(data = GPSdata, aes(xintercept = mean(Total Distance)), color = "#EC9A29", alpha = 0.7, linetype = 'dashed')+
facet_wrap(~teamShortName,scales = "free_x")+
labs(title = paste0(FFCGPS$athlete_name[1]," ", "","Total Distance"),
subtitle = NULL,
x = "Total Distance",
y = "Minutes",
)+
theme(plot.background = element_rect(fill = "#f0f0f0"),
strip.text.y.left = element_text(angle = 0, size = 8),
panel.background = element_rect(fill = "#f0f0f0", color = "black"),
panel.grid.major = element_line(color = "black", size = 0.1 , linetype = "dotdash"),
panel.grid.minor = element_line(color = "black", size = 0.1 , linetype = "dotdash"),
axis.title.y = element_text(angle = 90, color = "black" , face = "bold", size = 8),
axis.title.x = element_text(angle = 0, color = "black" , face = "bold", size = 8),
axis.text.y = element_text(angle = 0, color = "black" , face = "bold", size = 8),
axis.text.x = element_text(angle = 0, color = "black" , face = "bold", size = 8),
legend.background = element_rect(fill = "#f0f0f0", color = NULL),
legend.key.size = unit(0.6, "cm"),
legend.key.width = unit(0.7,"cm"),
strip.text.x = element_markdown(glue(GPSdata$teamShortName,":"<img src ='/Users/jcosta/Desktop/Reports/Rreports/teamlogos/U23premierleague{teamShortName}.png' width ='5'/">"'size = .02,fill = NA,label.color = NA,parse=FALSE, inherit.aes = FALSE,label.padding = grid::unit(rep(0,2),"pt"))),
legend.key = element_rect(fill = "#f0f0f0"),
legend.title = element_text(color = "black", size = 5),
legend.text = element_text(color = "black",size = 4),
legend.box.background = element_blank(),
plot.title = element_text(size = 8,colour = 'black',vjust = 0.7, hjust = 0.4, face = "bold"),
plot.subtitle = element_text(size = 6, hjust = 0.5, colour = 'black'),
legend.position="top",)"

I am trying to replace the wrap label name with an image

I found solution!
Assign the img source as a df and left join with the GPS data, then use
element_markdown in the strip text.

Hope this helps others in the future

1 Like