Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

error when annotating axis labels with custom images in ggplot/ggtext

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading