Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background colour of legend in ggplot2?

Tags:

r

legend

ggplot2

Does anybody know how to change the background colour for the points legend in ggplot2. I have created the plot below and would like to change the white background on the legend? Any ideas?

enter image description here

like image 884
wilsonm2 Avatar asked Sep 28 '15 15:09

wilsonm2


1 Answers

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

like image 105
bjoseph Avatar answered Sep 24 '22 19:09

bjoseph