ggplot customized legend symbols beyond key gliph

Hello
I am looking for ways to replace standard legend symbols by external icon/png or icon libraries

example below


df <- data.frame(
  a = runif(50, 0, 100),                         # Random x-coordinates
  b = runif(50, 0, 100),                         # Random y-coordinates
  case = sample(c(5, 10, 12),   # Random case sizes
                50, replace = TRUE),
  myimage = sample(c(here("img.png"),       # Simulated image paths
                     here("img.png"), 
                     here("img.png")), 
                   50, replace = TRUE)
)


ggplot() +
        geom_point(data = df, aes(x=a,y=b,fill = case,size =case)) +
        ggimage::geom_image(data = df, aes(x = a, y = b, image = myimage, size=case), 
                   color="black",show.legend = F) + 
        scale_size_continuous(range = c(0.03, 0.12)) 


the legend size is showing circles, not the the image used in the plot

thank you for your help!

Hi @antoine4ucsd , did you find the answer to this? I am having the same challenge.

unfortunately nor. let me know if you figure it out
thx

I am planning to create the legend and insert it as a static image if I do not find a better solution. The downside will be tha Legend will not dynamically change when the map location and elements changes.

I've done something like this by overwriting GeomText$draw_key with another function. It's probably easier to just make two charts and put them together with patchwork.

1 Like