6
votes

I've been searching around for a way to use png images as axis ticks in ggplot2, more precisely in ggridges I've read and tried to replicate answers to these posts, but packages have changed a lot in syntax from the time when they were posted to the date:

Annotate ggplot with an extra tick and label How can I use a graphic imported with grImport as axis tick labels in ggplot2 (using grid functions)? Icons as x-axis labels in R - ggplot2

I'd like to add png (or another kind of) images to tick labels, instead of the labels virginica, setosa and versicolor;

library(ggridges)
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges()
1

1 Answers

3
votes

cowplot package has made this somewhat easier.

Build the plot:

library(ggridges)
library(ggplot2)

p <- ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges()

Load the images and use axis_canvas() to build a strip of vertical images:

library(cowplot)

pimage <- axis_canvas(p, axis = 'y') + 
  draw_image("https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/Iris_virginica.jpg/295px-Iris_virginica.jpg", y = 2.5, scale = 0.5) +
  draw_image("https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Iris_versicolor_3.jpg/320px-Iris_versicolor_3.jpg", y = 1.5, scale = 0.5) +
  draw_image("https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg/450px-Kosaciec_szczecinkowaty_Iris_setosa.jpg", y = 0.5, scale = 0.5)


# insert the image strip into the plot
ggdraw(insert_yaxis_grob(p, pimage, position = "left"))

enter image description here

Without the axis.text.y:

p <- ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges() +
  theme(axis.text.y = element_blank())
ggdraw(insert_yaxis_grob(p, pimage, position = "left"))

enter image description here

You could remove also the vertical line, currently I can't find a way of having the image strip on the left-side of the axis line.