3

I am trying to manually add legend to my ggplot, however, am getting nowhere. Did a few searches but couldn't find a relevant solution. Here is my code so far

library(tidyverse)
library(zoo)

FakeData = data.frame(A = runif(184, 50,100), B = A + 20, C = A + 40, D = A -20, E = A-10, F = A-15, G = seq(1:184))

lab=c("May", "Jun","July", "Aug","Sep","Oct", "Nov")
ggplot(FakeData, aes(G, ymin=A, ymax=B))+geom_ribbon(fill= "grey75")+geom_line(aes(G, C),linetype = "dashed", col = "red", size = 1.3)+
  geom_line(aes(G, D),linetype = "dashed", colour = "black", size = 1.3)+
  geom_line(aes(G, E), colour = "darkblue", size = 1.3)+ geom_point(aes(G, F), col = "blue", size = 1.3)+ 
  scale_x_continuous(breaks = c(0,31,61,92,123,153,184), labels = lab)+ xlab("Month")+ ylab("Daily Cumulative Precipitation (mm)")+ theme_bw()

I would like to add below legends for the Figure

 Leg = c("Upper and lower Quartile", "Maximum", "Minimum", "Median", "Precipitation")

Edits to ggplot

I modified the ggplot code based on suggestions, however, the data color does not correspond to its legend. For example, the Max data which is color coded as red should be at the top of all other data in the figure, however, it is not- Likewise for other legends.

ggplot(FakeData)+geom_ribbon(aes(G, ymin=A, ymax=B, col = "cyan"), alpha = 0.3)+geom_line(aes(G, C, col = "red"),linetype = "dashed", size = 1.3)+
  geom_line(aes(G, D, col = "black"),linetype = "dashed", size = 1.3)+
  geom_line(aes(G, E, col = "darkblue"), size = 1.3)+ geom_point(aes(G, F, col = "blue"), size = 1.3)+ 
  scale_x_continuous(breaks = c(0,31,61,92,123,153,184), labels = lab)+ xlab("Month")+ ylab("Daily Cumulative Precipitation (mm)")+ theme_bw()+
  scale_color_manual(labels =  c("Upper and lower Quartile", "Maximum", "Minimum", "Median", "Precipitation"), values = c("cyan", "red","black", "darkblue","blue") )

Here is the plot for the updated code where the legends are not directing to the right data plot. enter image description here

The final product should be a figure somewhat like this (the legend is displayed on the left top cornor. enter image description here

CForClimate
  • 335
  • 5
  • 19
  • If you move `col=` and `colour=` to inside of `aes` in `geom_line` it will build a legend for you. You can use `theme(legend.position =` to set location of legend. To add ribbon to legend, see possible options here: https://stackoverflow.com/questions/28714492/legend-with-geom-line-and-geom-ribbon --- hope this helps. – Ben Sep 19 '19 at 02:27
  • Thanks @Ben, I modified the code based on your suggestions, which generated the legends. However, the legends are directing to the wrong data source. I am close but not close enough. – CForClimate Sep 19 '19 at 15:04
  • I'll provide a more detailed answer - let me know if this is closer to what you need. – Ben Sep 19 '19 at 15:39

1 Answers1

0

To clarify different colors in your legend, you can modify your Leg vector to assign colors to specific labels (renamed to CType). Then, you can provide the label directly in your geom_line and geom_point statements which will be the same as what shows up in your legend. As mentioned in the comment, I moved color to inside the aesthetic for the legend. To show the legend, you can use scale_color_manual.

Edit: To get the different linetypes - made a similar vector LType to indicate which linetype (dashed, solid, etc.). Instead of using geom_point for precipitation, used a dotted linetype - otherwise, it would be difficult combining color and linetype legends into one legend (unless you specify linetype for geom_point which would raise a warning but then ignore). Additional lines were added for the ribbon bounds so it would show up in legend.

library(tidyverse)
library(zoo)

set.seed(1)
N=184
A = runif(N, 50, 100)
G = seq(1:N)
FakeData = data.frame(A, B = A + 20, C = A + 40, D = A - 20, E = A + 10, F = A - 15, G)

lab=c("May", "Jun", "July", "Aug", "Sep", "Oct", "Nov")

CType = c("Upper and lower Quartile" = "grey75", "Maximum" = "red", "Minimum" = "black", "Median" = "darkblue", "Precipitation" = "blue")
LType = c("Upper and lower Quartile" = "solid", "Maximum" = "dashed", "Minimum" = "dashed", "Median" = "solid", "Precipitation" = "dotted")

ggplot(FakeData, aes(x = G))+
  geom_ribbon(aes(ymin = A, ymax = B), fill= "grey75")+
  geom_line(aes(y = A, colour = "Upper and lower Quartile", linetype = "Upper and lower Quartile"), size = 1.3)+
  geom_line(aes(y = B, colour = "Upper and lower Quartile", linetype = "Upper and lower Quartile"), size = 1.3)+
  geom_line(aes(y = C, colour = "Maximum", linetype = "Maximum"), size = 1.3)+
  geom_line(aes(y = D, colour = "Minimum", linetype = "Minimum"), size = 1.3)+
  geom_line(aes(y = E, colour = "Median", linetype = "Median"), size = 1.3)+
  geom_line(aes(y = F, colour = "Precipitation", linetype = "Precipitation"), size = 1.3)+ 
  scale_x_continuous(breaks = c(0,31,61,92,123,153,184), labels = lab)+ 
  xlab("Month")+ 
  ylab("Daily Cumulative Precipitation (mm)")+ 
  theme_bw()+
  scale_color_manual(name = "legend", values = CType)+
  scale_linetype_manual(name = "legend", values = LType)+
  theme(legend.position = c(.2, .8), legend.title = element_blank(), legend.key.width = unit(2, "cm")) 

plot with custom legend

Ben
  • 28,684
  • 5
  • 23
  • 45