简体   繁体   English

在 ggplot2 中添加图例

[英]Adding a Legend in ggplot2

I have the following code.我有以下代码。

  Financial_Wealth.lq,Financial_Wealth.uq,Total_Wealth.lq,Total_Wealth.uq,time=seq(0,(sPar.dNN),1))
ggplot(data, aes(x=time)) +
  geom_line(aes(y = Human_Capital.mean), color="red", size=1) +
  geom_line(aes(y = Financial_Wealth.mean), color="goldenrod3", size=1) +
  geom_ribbon(aes(ymin=Financial_Wealth.lq, ymax = Financial_Wealth.uq), alpha=0.4, fill="goldenrod3") +
  geom_line(aes(y = Total_Wealth.mean), color="dodgerblue", size=1)+
  geom_ribbon(aes(ymin=Total_Wealth.lq, ymax=Total_Wealth.uq), alpha=0.4, fill = "dodgerblue") +
  scale_x_continuous(name = 'Age',
                     breaks=(c(seq(0,(sPar.dNN),4))))+
  scale_y_continuous(name = 'Wealth Level',
                     breaks = seq(0,100,10))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        legend.title = element_text(size=12, face="bold"),
        legend.text = element_text(size=12),
        axis.title = element_text(size=12),
        axis.text = element_text(size=10)) +
  coord_cartesian(xlim = c(0,45), ylim = c(0,100), expand = TRUE)+
  scale_fill_manual(name="Median",values=c("goldenrod3", "red","dodgerblue"),
                    labels = c("Financial Wealth", "Human Capital", "Total Wealth"))+
  ggtitle('Optimal Wealth Development') 

You can interpret each data input as a vector of numbers of equal length.您可以将每个数据输入解释为等长数字的向量。 Can someone please tell me why the legend is not appearing?有人能告诉我为什么传说没有出现吗? What do I need to do differently: Thanks in advance :) I have attached the image that it is producing so you get an idea of what I am trying to achieve.我需要做些什么不同的事情:在此先感谢 :) 我附上了它正在制作的图像,以便您了解我想要实现的目标。

图片

In order to add a legend, you need to specify one of the aesthetics within aes() .为了添加图例,您需要在aes()中指定一种美学。 In this case, take all of your geom_line() calls and place for each one the color= inside of aes() .在这种情况下,请进行所有geom_line()调用,并为每个调用放置color=内部aes() The value assigned to color= within aes() will be the text of the label in the legend: not the color.aes()中分配给color=的值将是图例中 label 的文本:不是颜色。 To assign color, you need to add scale_color_manual() and set values= a named vector.要分配颜色,您需要添加scale_color_manual()并设置values=命名向量。

See below for the following changes that should solve your problem, although in the absence of your dataset or a reprex, I'm unable to test the function of the new code.请参阅下面的以下更改,这些更改应该可以解决您的问题,尽管在没有您的数据集或 reprex 的情况下,我无法测试新代码的 function。

# original code
... +
geom_line(aes(y = Human_Capital.mean), color="red", size=1) +
geom_line(aes(y = Financial_Wealth.mean), color="goldenrod3", size=1) +
geom_line(aes(y = Total_Wealth.mean), color="dodgerblue", size=1)+

# new plot code
... +
geom_line(aes(y = Human_Capital.mean, color="Human Capital Mean"), size=1) +
geom_line(aes(y = Financial_Wealth.mean, color="Financial Wealth Mean"), size=1) +
geom_line(aes(y = Total_Wealth.mean, color="Total Wealth Mean"), size=1) +
scale_color_manual(values=c(
  "Human Capital Mean"="red",
  "Financial Wealth Mean"="goldenrod3",
  "Total Wealth Mean"="dodgerblue"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM