是否可以向ggplot图例添加自定义形状(从png)? [英] Is it possible to add custom shapes ( from png) to ggplot legend?
问题描述
我想知道是否可以为ggimage()
的图例添加形状.我发现了类似的问题,但它们要么是1)图例中具有不同颜色的同一张图片,要么是2)同一张图片具有不同的颜色.我使用了此链接将具有颜色的图像添加到图例中,但是我不知道如何编辑它,以便图像会有所不同.我尝试了此,但是我还不足以调整代码以适应我的情况
I was wondering whether it is possible to add shapes to the legend for ggimage()
. I found similar questions, but they either have 1) same picture with a different color in legend, or 2) same picture different colors. I used this link to add images to the legend that have color, but i can't figure out how to edit it so that image will be different. I tried this, but I am not advance enough to adjust the code to fit my case
d <- data.frame(x = rnorm(10),
y = rnorm(10),
image = sample(c("https://www.r-project.org/logo/Rlogo.png",
"https://jeroenooms.github.io/images/frink.png"),
size=10, replace = TRUE)
)
ggplot(d, aes(x, y)) + geom_image(aes(image=image, color=image), size=.05)
推荐答案
使用ggdraw
解决:
p2
在ggplot()
对象上,在该对象中我在行中绘制图像(坐标x = 2,y = c(1,2))并添加了文本(坐标x = 2.5,y = c(1,2))>
Solved by ussing ggdraw
:
p2
os the ggplot()
object where I ploted images in row (coordinates x=2, y = c(1,2) ) and added text (coordinates x=2.5, y=c(1,2))
ggdraw() +
draw_plot(p1) +
draw_plot(p2, x = 0.55, y = 0.6)
然后我只是在尝试p2的位置,并在p2中添加xlim和ylim以使文本和图像在一起
And then I just play around with the position of p2 as well as added xlim and ylim to p2 to make text and image together
这篇关于是否可以向ggplot图例添加自定义形状(从png)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!