1

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)

enter image description here

yuliaUU
  • 1,581
  • 2
  • 12
  • 33
  • Does this answer your question? [Custom legend with imported images](https://stackoverflow.com/questions/36133374/custom-legend-with-imported-images) – A.Elsy Apr 23 '20 at 11:48
  • Hi, thanks, almost! But it does not produce the desired color. – yuliaUU Apr 23 '20 at 14:18
  • I want to change a shape of a color in legend to the image, so 9instead of circle there would be an image, but I would like to preserve color in legend – yuliaUU Apr 23 '20 at 14:19
  • One shape can be used changing the underlying draw_key function like in this answer https://stackoverflow.com/a/61333337/7941188. This does not allow modification of shape by group though. – tjebo Apr 23 '20 at 18:21
  • yes, I saw it, but again, this not what I am looking for – yuliaUU Apr 23 '20 at 18:41

1 Answers1

0

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

yuliaUU
  • 1,581
  • 2
  • 12
  • 33