我一直在寻找一种方法来使用png图像作为ggplot2中的轴刻度,更准确地说是在 ggridges 中 我已经阅读并尝试复制这些帖子的答案,但是从发布日期到发布日期,软件包的语法已经发生了很大变化:
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
我想添加png(或其他类型)图像到刻度标签,而不是标签virginica,setosa和versicolor;
library(ggridges)
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges()
答案 0 :(得分:0)
cowplot
软件包使此操作变得容易一些。
建立情节:
library(ggridges)
library(ggplot2)
p <- ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges()
加载图像并使用axis_canvas()
构建一条垂直图像:
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"))
没有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"))
您也可以删除垂直线,目前我找不到在轴的左侧放置图像带的方法。