ggplot2为图例添加颜色

问题描述

我正在创建带有图例的多折线图,但是我不确定如何将图例中的正方形强制为与线条相同的颜色。

ggplot(df,aes(x=Month)) +
  geom_line(aes(y=`Less than a high school diploma`/100,color="Less Than HS Diploma"),size=2,alpha=0.5,linetype=1) +
  geom_line(aes(y=`High school graduates,no college`/100,color="HS Diploma"),linetype=1) + 
  geom_line(aes(y=`Some college or associate degree`/100,color="Some College / Associate's Degree"),linetype=1) +
  geom_line(aes(y=`Bachelor's degree and higher`/100,color="Bachelor's Degree and Higher"),linetype=1) +
  scale_color_manual(name="Educational Attainment",values = c("Less Than HS Diploma"="deepskyblue","HS Diploma" = "firebrick1","Some College / Associate's Degree"="mediumpurple","Bachelor's Degree and Higher"="springgreen4")) +
  ggtitle("Unemployment Rate by Educational Attainment") +
  xlab("Time") +
  ylab("Unemployment Rate") +
  scale_y_continuous(labels = scales::percent) +
  theme(plot.title = element_text(hjust = 0.5),legend.position="bottom")

enter image description here

解决方法

主要问题是alpha参数和geom_line。如果希望按键显示为行,请通过guides(color = guide_legend(override.aes = list(alpha = c(1,1,1))))在图例中将alpha设置为1。如果您想为按键使用彩色矩形,可以通过在key_glyph = "rect"图层上添加geom_line来实现

使用economics数据集作为示例数据:

library(ggplot2)

ggplot(economics,aes(x=date)) +
  geom_line(aes(y=`psavert`/100,color="Less Than HS Diploma"),size=2,alpha=0.5,linetype=1) +
  geom_line(aes(y=`uempmed`/100,color="HS Diploma"),linetype=1) + 
  geom_line(aes(y=`psavert`/10,color="Some College / Associate's Degree"),linetype=1) +
  geom_line(aes(y=`uempmed`/10,color="Bachelor's Degree and Higher"),linetype=1) +
  scale_color_manual(name="Educational Attainment",values = c("Less Than HS Diploma"="deepskyblue","HS Diploma" = "firebrick1","Some College / Associate's Degree"="mediumpurple","Bachelor's Degree and Higher"="springgreen4")) +
  guides(color = guide_legend(override.aes = list(alpha = c(1,1)))) +
  ggtitle("Unemployment Rate by Educational Attainment") +
  xlab("Time") +
  ylab("Unemployment Rate") +
  scale_y_continuous(labels = scales::percent) +
  theme(plot.title = element_text(hjust = 0.5),legend.position="bottom")

使用key_glyph="rect"

library(ggplot2)

ggplot(economics,linetype=1,key_glyph = "rect") +
  geom_line(aes(y=`uempmed`/100,key_glyph = "rect") + 
  geom_line(aes(y=`psavert`/10,key_glyph = "rect") +
  geom_line(aes(y=`uempmed`/10,key_glyph = "rect") +
  scale_color_manual(name="Educational Attainment","Bachelor's Degree and Higher"="springgreen4")) +
  ggtitle("Unemployment Rate by Educational Attainment") +
  xlab("Time") +
  ylab("Unemployment Rate") +
  scale_y_continuous(labels = scales::percent) +
  theme(plot.title = element_text(hjust = 0.5),legend.position="bottom")

reprex package(v0.3.0)于2020-10-22创建

,

values中的scale_color_manual自变量应该使用颜色名称而不是行名称,而无需传递这些名称。示例:

scale_color_manual(name="Educational Attainment",values = c("red","yellow","white",...))
,

这应该起作用(不包含任何输出,因为没有共享数据)。如果要填充图例,则还必须在aes()内启用选项fill。之后,您可以缩放颜色以填充scale_fill_manual(),并使用labs()为它们命名。这里的代码:

library(ggplot2)
#Code
ggplot(df,aes(x=Month)) +
  geom_line(aes(y=`Less than a high school diploma`/100,color="Less Than HS Diploma",fill="Less Than HS Diploma"),linetype=1) +
  geom_line(aes(y=`High school graduates,no college`/100,color="HS Diploma",fill="HS Diploma"),linetype=1) + 
  geom_line(aes(y=`Some college or associate degree`/100,color="Some College / Associate's Degree",fill="Some College / Associate's Degree"),linetype=1) +
  geom_line(aes(y=`Bachelor's degree and higher`/100,color="Bachelor's Degree and Higher",fill="Bachelor's Degree and Higher"),"Bachelor's Degree and Higher"="springgreen4")) +
  scale_fill_manual(name="Educational Attainment","Bachelor's Degree and Higher"="springgreen4"))+
  ggtitle("Unemployment Rate by Educational Attainment") +
  xlab("Time") +
  ylab("Unemployment Rate") +
  scale_y_continuous(labels = scales::percent) +
  theme(plot.title = element_text(hjust = 0.5),legend.position="bottom")+
  labs(color='Class',fill='Class')