Ggplot2: change the background color of one of the legend items

I would like to change the background color for one legend item, but not for the rest. For example using the built-in dataset RToothGrowth

ggplot(ToothGrowth, aes(x=dose,y=len,colour=supp)) +
  geom_point()

      

Instead of changing the color of the points themselves, I want to change the background color of the box in the legend from gray to a different color for just one of the legend items. For example, change the background VC

, but not OJ

.

enter image description here

+3


source to share


1 answer


This is kind of a hack because you are using a package {magick}

, but it gives the desired output:

library(ggplot2)
library(magick)

fig <- image_graph(width = 1000, height = 500, res = 96)
ggplot(ToothGrowth, aes(x=dose,y=len,colour=supp)) +
  geom_point()
dev.off()

fig %>% 
  image_fill("lightblue", point = "+950+220", fuzz = 5)

      



image_fill()

is the MSPaint equivalent of a paint bucket

enter image description here

+1


source







All Articles