Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

image as axis tick ggplot

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()
like image 869
Elio Diaz Avatar asked Jan 18 '19 04:01

Elio Diaz


People also ask

How to modify ggplot x axis tick labels in R?

Use scale_x_discrete to Modify ggplot X Axis Tick Labels in R scale_x_discrete together with scale_y_discrete are used for advanced manipulation of plot scale labels and limits. In this case, we utilize scale_x_discrete to modify x axis tick labels for ggplot objects.

How to set default value of Y-axis tick marks using Ggplot2?

The default value of Y-axis tick marks using ggplot2 are taken by R using the provided data but we can set it by using scale_y_continuous function of ggplot2 package. For example, if we want to have values starting from 1 to 10 with a gap of 1 then we can use scale_y_continuous (breaks=seq (1,10,by=1)).

How to set tick marks on the Y axis in R?

trans for axis transformations. Possible values are “log2”, “log10”, “sqrt”, etc The R code below set the position of tick marks on the y axis of the box plot. The function scale_y_continuous () and the argument breaks are used to choose where the tick marks appear :

What are the possible labels for Axis tick marks?

labels : labels of axis tick marks. Allowed values are : trans for axis transformations. Possible values are “log2”, “log10”, … The functions scale_x_continuous () and scale_y_continuous () can be used as follow :


1 Answers

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.

like image 81
RLave Avatar answered Sep 21 '22 17:09

RLave