2
votes

I need a legend for my plot that shows the black and grey line and both grey fill colors. I tried scale_fill_manual and scale_color_manual, doesn't show anything. What am i doing wrong?

Here's my plot and here's the code

ggplot(dataset, aes(x=FirstOfMonth)) +
    ggtitle(title) +
    xlab("") +
    ylab("") +
    geom_line(aes(y = AC), color = "black") +
    geom_line(aes(y = FC), color="grey") +
    geom_line(aes(y = Hi80), color="grey") +
    geom_line(aes(y = Lo80), color="grey") +
    geom_line(aes(y = Hi95), color="grey") +
    geom_line(aes(y = Lo95), color="grey") +
    geom_ribbon(aes(ymin=Lo80, ymax=Hi80), fill="grey", alpha=0.5) +
    geom_ribbon(aes(ymin=Lo95, ymax=Hi95), fill="grey", alpha=0.25) +
    theme_classic() + #background color, panel background color and grid lines
    scale_x_date(date_breaks = "6 months", date_labels="%d.%m.%Y") +
    scale_y_continuous(labels = scales::number_format(accuracy = 1, decimal.mark = '.'))

and the original dataframe called dataset:

   FirstOfMonth        AC       FC      Hi80       Lo80         Lo95      Hi95         Model Period
1    2017-12-01 76438.219       NA        NA         NA           NA        NA                   12
2    2018-01-01  2100.328       NA        NA         NA           NA        NA                   12
3    2018-02-01 12195.594       NA        NA         NA           NA        NA                   12
4    2018-03-01 17563.799       NA        NA         NA           NA        NA                   12
5    2018-04-01 15233.833       NA        NA         NA           NA        NA                   12
6    2018-05-01 14504.022       NA        NA         NA           NA        NA                   12
7    2018-06-01 14727.422       NA        NA         NA           NA        NA                   12
8    2018-07-01 14464.197       NA        NA         NA           NA        NA                   12
9    2018-08-01 15239.294       NA        NA         NA           NA        NA                   12
10   2018-09-01 15077.799       NA        NA         NA           NA        NA                   12
11   2018-10-01 17487.724       NA        NA         NA           NA        NA                   12
12   2018-11-01 17346.364       NA        NA         NA           NA        NA                   12
13   2018-12-01 13441.042       NA        NA         NA           NA        NA                   12
14   2019-01-01 13124.846       NA        NA         NA           NA        NA                   12
15   2019-02-01 10286.554       NA        NA         NA           NA        NA                   12
16   2019-03-01 10241.349       NA        NA         NA           NA        NA                   12
17   2019-04-01 15533.372       NA        NA         NA           NA        NA                   12
18   2019-05-01  9994.203       NA        NA         NA           NA        NA                   12
19   2019-06-01 11617.312       NA        NA         NA           NA        NA                   12
20   2019-07-01  9717.043       NA        NA         NA           NA        NA                   12
21   2019-08-01 19140.597       NA        NA         NA           NA        NA                   12
22   2019-09-01  9233.094       NA        NA         NA           NA        NA                   12
23   2019-10-01 11126.801       NA        NA         NA           NA        NA                   12
24   2019-11-01  9138.605       NA        NA         NA           NA        NA                   12
25   2019-12-01 16485.743       NA        NA         NA           NA        NA                   12
26   2020-01-01  7362.088       NA        NA         NA           NA        NA                   12
27   2020-01-01        NA 7362.088  7362.088  7362.0880   7362.08800  7362.088 Exp Smoothing     12
28   2020-02-01        NA 8780.329 13122.052  4438.6063   2140.23712 15420.421 Exp Smoothing     12
29   2020-03-01        NA 8780.329 14559.384  3001.2738    -57.97311 17618.631 Exp Smoothing     12
30   2020-04-01        NA 8780.329 15808.778  1751.8794  -1968.75708 19529.415 Exp Smoothing     12
31   2020-05-01        NA 8780.329 16965.737   594.9209  -3738.17232 21298.830 Exp Smoothing     12
32   2020-06-01        NA 8780.329 18073.493  -512.8352  -5432.33910 22992.997 Exp Smoothing     12
33   2020-07-01        NA 8780.329 19156.316 -1595.6582  -7088.37402 24649.032 Exp Smoothing     12
34   2020-08-01        NA 8780.329 20229.746 -2669.0884  -8730.04383 26290.702 Exp Smoothing     12
35   2020-09-01        NA 8780.329 21304.706 -3744.0485 -10374.05335 27934.711 Exp Smoothing     12
36   2020-10-01        NA 8780.329 22389.444 -4828.7860 -12033.01621 29593.674 Exp Smoothing     12
37   2020-11-01        NA 8780.329 23490.557 -5929.8991 -13717.02339 31277.681 Exp Smoothing     12
38   2020-12-01        NA 8780.329 24613.582 -7052.9245 -15434.54249 32995.200 Exp Smoothing     12
1
Welcome to SO! I advice you to put your data from wide to long format, then feed ggplot with the data in that shape.s__

1 Answers

0
votes

To get a legend use the color and fill aesthetics. The legends can then be adjusted using scale_xxxx_manual (to set the correct colors) and or guide_legend (to set the alpha's for the fill colors) like so:

dataset %>% 
  ggplot(aes(x=FirstOfMonth)) +
  ggtitle("title") +
  xlab("") +
  ylab("") +
  geom_line(aes(y = AC, color = "AC")) +
  geom_line(aes(y = FC, color="FC")) +
  geom_line(aes(y = Hi80, color="FC")) +
  geom_line(aes(y = Lo80, color="FC")) +
  geom_line(aes(y = Hi95, color="FC")) +
  geom_line(aes(y = Lo95, color="FC")) +
  geom_ribbon(aes(ymin=Lo80, ymax=Hi80, fill="grey80"), alpha=0.5) +
  geom_ribbon(aes(ymin=Lo95, ymax=Hi95, fill="grey95"), alpha=0.25) +
  theme_classic() + #background color, panel background color and grid lines
  scale_x_date(date_breaks = "6 months", date_labels="%d.%m.%Y") +
  scale_y_continuous(labels = scales::number_format(accuracy = 1, decimal.mark = '.')) +
  scale_color_manual(values = c("FC" = "grey", "AC" = "black")) +
  scale_fill_manual(values = c("grey80" = "grey", "grey95" = "grey")) +
  guides(fill = guide_legend(override.aes = list(alpha = c(0.5, 0.25))))