How to change background colour of legend in ggplot2?

一个人想着一个人 提交于 2020-06-27 06:52:10

问题


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?


回答1:


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:



来源:https://stackoverflow.com/questions/32826156/how-to-change-background-colour-of-legend-in-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!