0

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.

image

duckmayr
  • 16,303
  • 3
  • 35
  • 53
Shem Katz
  • 19
  • 6
  • Hi. Post your data with `dput(data)`, or provide a minimal reproducible example. See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – MKR Aug 13 '20 at 12:45
  • Does this https://stackoverflow.com/questions/54543101/r-ggplot2-legend-not-appearing-for-line-graph or https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot help to solve your issue? – stefan Aug 13 '20 at 12:53
  • use `scale_color_manual()` – YBS Aug 13 '20 at 13:02
  • Does this answer your question? [Adding a legend to a combined line and bargraph ggplot](https://stackoverflow.com/questions/63384151/adding-a-legend-to-a-combined-line-and-bargraph-ggplot) – chemdork123 Aug 13 '20 at 13:12

1 Answers1

0

In order to add a legend, you need to specify one of the aesthetics within aes(). In this case, take all of your geom_line() calls and place for each one the color= inside of aes(). The value assigned to color= within aes() will be the text of the label in the legend: not the color. To assign color, you need to add scale_color_manual() and set values= a named vector.

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.

# 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"))
chemdork123
  • 12,369
  • 2
  • 16
  • 32