wilsonm2
wilsonm2

Reputation: 591

How to change background colour of legend in 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

Upvotes: 23

Views: 24019

Answers (1)

bjoseph
bjoseph

Reputation: 2166

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

Upvotes: 31

Related Questions