Have you ever needed to show logos, icons, or flags directly on the axes of your ggplot charts?
You can do this using the element_markdown() function from the {ggtext} package!
element_markdown() is a function that tells ggplot to treat labels as HTML instead of plain text. It must be used in the theme() section of your ggplot code.
The chart below shows an example where league logos appear directly on the x-axis. It displays the total number of goals scored in the major European football leagues during the 2023/2024 season:
European football leagues 2023/2024
Exploratory Data Analysis Workflow
Here is a short, practical guide showing how to recreate this plot.
Step 0: Start from a basic dataframe
In most real situations you will already have a dataframe to work with. Here is the one used in this example:
df<-data.frame( league =c("Premier League", "La Liga", "Serie A", "Bundesliga", "Ligue 1"), goals =c(1197, 977, 968, 960, 804))df
league goals
1 Premier League 1197
2 La Liga 977
3 Serie A 968
4 Bundesliga 960
5 Ligue 1 804
Step 1: Choose the images you want to display (PNG recommended)
First decide which images you want to show on your ggplot axis. In this example I am using online image URLs (including links from Wikimedia), but you can use any source that provides a direct image URL, such as files hosted on GitHub repos or any public static hosting service. Just make sure the image format is supported β typically PNG or JPG/JPEG.
# Define image URLs as variablespremier_url<-"https://upload.wikimedia.org/wikipedia/it/6/6d/Premier_League_Logo_2016.png"laliga_url<-"https://upload.wikimedia.org/wikipedia/it/9/9b/LFP_La_Liga.png"seriea_url<-"https://www.fifplay.com/img/public/serie-a-logo.png"bundes_url<-"https://www.fifplay.com/img/public/bundesliga-logo.png"ligue1_url<-"https://upload.wikimedia.org/wikipedia/commons/a/a0/Ligue_1_2024_Logo.png"
Step 2: Add a column with HTML tags
In order to display images on the axis, each image URL must be wrapped inside an HTML tag. This simply means that the URL cannot appear alone β it must be placed inside the structure:
`<img src= + image_url + width='45'/>`
Please note that in this example I use width=β45β just as a reference size, but you can choose any value depending on how big you want the image to appear in your plot.
Now that the dataframe contains a column of HTML tags, we can use it directly on the x-axis. The key part is here:
theme( axis.text.x =element_markdown())
element_markdown() function is what allows ggplot to interpret the HTML in your axis labels. Without it, ggplot would show the literal string <img src='...'> as plain text. With it, the <img> tags are rendered as actual images.
With element_markdown() applied to axis.text.x, ggplot correctly displays the league logos instead of plain text labels, as seen in the barchart above.
To Recap
Prepare your dataframe (existing or new)
Choose your images (PNG recommended) and store their URLs
Wrap each URL inside an HTML tag with a defined width