How to change background colour of legend in ggplot2? How to change background colour of legend in ggplot2? r r

How to change background colour of legend in ggplot2?


You can use the legend.key parameter of theme. From ?theme:

legend.key: background underneath legend keys (element_rect(); inherits from rect)

That is

theme(legend.key = element_rect(fill = "black"))

An example:

a <- seq(1:5)b <- seq(1:5)c <- seq(1:5)d <- data.frame(a, b, c)ggplot(data = d, aes(x = a, y = b, color = factor(c))) +  geom_point() +  theme(legend.key = element_rect(fill = "yellow"))

produces:

enter image description here