Is it possible to add custom shapes ( from png) to ggplot legend?

若如初见. 提交于 2020-04-30 06:29:39

问题


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)


回答1:


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)

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



来源:https://stackoverflow.com/questions/61381710/is-it-possible-to-add-custom-shapes-from-png-to-ggplot-legend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!