如何更改ggplot2中图例的背景颜色?

How to change background colour of legend in ggplot2?

有谁知道如何更改 ggplot2 中点图例的背景颜色。我创建了下面的情节并想更改图例上的白色背景?有什么想法吗?

可以使用themelegend.key参数。来自 ?theme:

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

也就是

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

一个例子:

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"))

产生: