I am trying to annotate a plot by including custom images as the labels for a discrete x axis.
I have used the package ggtext
which adds this functionality to ggplot, and while I am able to replicate the example provided in the documentation of ggtext fior the iris
datase, as son as i stat using custom images, either from a link or reading them from file i stat getting errors which i cannot seems to solve. I have tried converting the image to different formats still no success.
here is an example with my custom images
labels <- c(
setosa = "<img src='https://github.com/hcuve/halo_faces/blob/main/ggtextimgs/comp1.JPG'
width='100' /><br>*I. *",
virginica = "<img src='https://github.com/hcuve/halo_faces/blob/main/ggtextimgs/comp2.JPG'
width='100' /><br>*I. *",
versicolor = "<img src='https://github.com/hcuve/halo_faces/blob/main/ggtextimgs/comp3.JPG'
width='100' /><br>*I. *"
)
ggplot(iris, aes(Species, Sepal.Width)) +
geom_boxplot() +
scale_x_discrete(
name = NULL,
labels = labels
) +
theme(
axis.text.x = element_markdown(color = "black", size = 11)
)
this throws the following error: Error in jpeg::readJPEG(get_file(path), native = TRUE) : JPEG decompression error: Not a JPEG file: starts with 0x0a 0x0a
When you copy the link of the download button of each image on Github, you can see that the link is different. That's why you are getting an error. Here is a reproducible example with the right URLs:
labels <- c(
setosa = "<img src='https://github.com/hcuve/halo_faces/raw/main/ggtextimgs/comp1.png'
width='100' /><br>*I. *",
virginica = "<img src='https://github.com/hcuve/halo_faces/raw/main/ggtextimgs/comp2.png'
width='100' /><br>*I. *",
versicolor = "<img src='https://github.com/hcuve/halo_faces/raw/main/ggtextimgs/comp3.png'
width='100' /><br>*I. *"
)
library(ggtext)
library(ggplot2)
ggplot(iris, aes(Species, Sepal.Width)) +
geom_boxplot() +
scale_x_discrete(
name = NULL,
labels = labels
) +
theme(
axis.text.x = element_markdown(color = "black", size = 11)
)
Created on 2022-12-27 with reprex v2.0.2