0

I am plotting a series of lines using ggplot and shading between them. There are 10 lines, but only 5 shaded regions. I want the legend for the 5 shaded regions. At the moment no legend is showing. I thought the line

scale_color_manual(name="test",labels = c("90%","70%","50%","30%","10%"), 
                 values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))  

would add a manual colour bar with the shaded region, but nothing shows. The full plot code is below. Thanks

Exposure_by_Month_10perc=Exposure_by_Month[,c(1,seq(3,length(monthQuant[1,])+1,4))]
dfm <- melt(Exposure_by_Month_10perc, id.vars = "Date")
dfm$value=dfm$value/1000000
colnames(dfm)=c("Date","Confidence Interval","value")

ggplot(Exposure_by_Month_10perc, aes(x=Date, y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.15",3])) + 
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.25",3])) + 
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.35",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.45",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.55",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.65",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.75",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.85",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.95",3])) +
  geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.95",3], ymax = dfm[dfm$`Confidence Interval`== "0.05",3]), fill = "Darkolivegreen1", alpha = .5) +
  geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.85",3], ymax = dfm[dfm$`Confidence Interval`== "0.15",3]), fill = "Darkolivegreen2", alpha = .5) +
  geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.75",3], ymax = dfm[dfm$`Confidence Interval`== "0.25",3]), fill = "Darkolivegreen3", alpha = .5) +
  geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.65",3], ymax = dfm[dfm$`Confidence Interval`== "0.35",3]), fill = "Darkolivegreen4", alpha = .5) +
  geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.55",3], ymax = dfm[dfm$`Confidence Interval`== "0.45",3]), fill = "Darkolivegreen", alpha = .5) +
  scale_x_date(labels = date_format("%b-%Y"),breaks='2 months') +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "XXXX TITLE", subtitle = "XXX SUBTITLE") +
  xlab("Month") +
  ylab("Value, $M") +
  scale_y_continuous(label=dollar_format(),breaks = round(seq(min(dfm$value), max(dfm$value), by = 1),0)) +
  geom_line(aes(y = 0*dfm[dfm$`Confidence Interval`== "0.95",3]),linetype = "dashed") +
  scale_color_manual(name="test",labels = c("90%","70%","50%","30%","10%"), 
                     values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))
  ###why no legend

> head(Exposure_by_Month_10perc)
# A tibble: 6 x 11
  Date         `0.05`   `0.15`   `0.25`   `0.35`   `0.45`   `0.55`   `0.65`   `0.75`   `0.85`   `0.95`
  <date>        <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
1 2018-10-01  -1.44e6  -2.58e6  -3.42e6  -4.27e6  -5.15e6  -6.01e6  -7.01e6  -8.11e6  -9.51e6  -1.21e7
2 2018-11-01  -5.36e4  -1.42e6  -2.35e6  -3.35e6  -4.40e6  -5.67e6  -7.09e6  -8.66e6  -1.07e7  -1.50e7
3 2018-12-01   6.63e5  -7.65e5  -1.87e6  -3.05e6  -4.32e6  -5.82e6  -7.62e6  -9.68e6  -1.25e7  -1.87e7
4 2019-01-01   8.34e5  -9.67e5  -1.90e6  -2.68e6  -3.64e6  -4.73e6  -5.59e6  -6.94e6  -8.66e6  -1.19e7
5 2019-02-01   1.16e6  -4.70e5  -1.52e6  -2.13e6  -3.10e6  -4.15e6  -5.13e6  -6.53e6  -8.31e6  -1.20e7
6 2019-03-01   1.88e6  -6.02e3  -1.40e6  -2.10e6  -3.26e6  -4.49e6  -5.72e6  -7.46e6  -9.74e6  -1.38e7

[Plot without legend]1

AC1981
  • 11
  • 4
  • can you post the `Exposure_by_Month` dataframe or a part of it? – Shreyas Sep 21 '18 at 19:57
  • added to post. thanks – AC1981 Sep 21 '18 at 20:04
  • Possible duplicate of [Add legend to ggplot2 line plot](https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot) – divibisan Sep 21 '18 at 20:12
  • The problem is that your data is in a wide format, when `ggplot` is designed to work with data in a long format. Take a look at how, in the linked question, the data was reshaped so instead of two separate `geom_line` statements, they used one with `color=variable` in the `aes`. – divibisan Sep 21 '18 at 20:13
  • You want to make is so that, instead of one variable for each `geom_ribbon`, all the data is in one variable with another variable that distinguishes each interval. You'll want to use `tidyr::gather` or `reshape2::melt` or something like that to reshape your data into long format – divibisan Sep 21 '18 at 20:15
  • Also, the color of the area of a `geom_ribbon` is controlled by `fill`, `color` controls the line color – divibisan Sep 21 '18 at 20:16
  • thanks ... i'll give those a go. appreciate your help – AC1981 Sep 21 '18 at 20:19
  • i should add that my plots are using dfm, which is a long form of Exposure_by_Month_10perc – AC1981 Sep 21 '18 at 20:25
  • Is there no way to create manual legends with labels and colours etc. of my choosing? – AC1981 Sep 21 '18 at 20:27
  • divibisan ... i cannot think of a way of reshaping the data that will allow multiple line to be plotted, along with shading between certain pairs of lines. i am using long format to plot currently, so i only have 3 columns: Date (x axis), value (y axis) and Confidence Interval (0.05 - 0.95 in 0.1 intervals. For the geom_ribbon i want the shaded area between CI 0.05 and 0.95, 0.15 and 0.85 etc. ... How can this be done? – AC1981 Sep 21 '18 at 22:20

2 Answers2

0

Try this:

scale_color_manual(guide="legend", labels = c("90%","70%","50%","30%","10%"), 
                 values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))
iod
  • 7,412
  • 2
  • 17
  • 36
0

This was solved by placing the fill=XXX inside the aes bracket of the geom_ribbon functions, as per code below:

ggplot(Exposure_by_Month_10perc, aes(x=Date, y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.15",3])) + 
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.25",3])) + 
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.35",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.45",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.55",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.65",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.75",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.85",3])) +
  geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.95",3])) +
  geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.95",3], ymax = dfm[dfm$`Confidence Interval`== "0.05",3], fill = "Darkolivegreen1"), alpha = .5) +
  geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.85",3], ymax = dfm[dfm$`Confidence Interval`== "0.15",3], fill = "Darkolivegreen2"), alpha = .5) +
  geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.75",3], ymax = dfm[dfm$`Confidence Interval`== "0.25",3], fill = "Darkolivegreen3"), alpha = .5) +
  geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.65",3], ymax = dfm[dfm$`Confidence Interval`== "0.35",3], fill = "Darkolivegreen4"), alpha = .5) +
  geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.55",3], ymax = dfm[dfm$`Confidence Interval`== "0.45",3], fill = "Darkolivegreen"), alpha = .5) +
  scale_x_date(labels = date_format("%b-%Y"),breaks='2 months') +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Tullow Hedging Portfolio", subtitle = "Value at Risk (VAR) Analysis") +
  xlab("Month") +
  ylab("Value at Risk, $M") +
  scale_y_continuous(label=dollar_format(),breaks = round(seq(min(dfm$value), max(dfm$value), by = 1),0)) +
  geom_line(aes(y = 0*dfm[dfm$`Confidence Interval`== "0.95",3]),linetype = "dashed") +
  scale_fill_manual(name="Confidence Interval",labels = c("90%","70%","50%","30%","10%"), 
                     values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))
AC1981
  • 11
  • 4