Home > front end >  Add two legends in ggplot
Add two legends in ggplot

Time:10-20

x <- c(1:3)
y1 <- c(2/28,13/35,43/65)
y2 <- c(1/2, 2/13, 5/43)
df <- data.frame(x, y1, y2)

x <- c(rep(1 , 5) , rep(2 , 5) , rep(3 , 5))
condition <- rep(c("1", "2", "3", "4", "5") , 3)
y3 <- c(1,  0,  0,  0,  0, 6,  4,  1,  0,  0, 12, 13,  5,  5,  3)
df2 <- data.frame(x, y3, condition)

cols <- c("1"="grey80","2"="grey70","3"="grey60","4"="grey50","5"="grey40")
ggplot()   
  geom_bar(aes(fill=df2$condition, y=df2$y3, x=df2$x), position="stack", stat="identity", width = 0.5)  
  geom_line(mapping = aes(x = df$x, y = df$y1*50),linetype=1, size = 1, color = "black")   
  geom_point(mapping = aes(x = df$x, y = df$y1*50), size = 2, color = "black")  
  geom_text(aes(x=df$x, y=df$y1*50, label = round(df$y1, 3)),vjust = "outward", hjust = "outward",show.legend = FALSE, color="black")  
  geom_line(mapping = aes(x = df$x, y = df$y2*50), linetype=2, size = 1, color = "black")  
  geom_point(mapping = aes(x = df$x, y = df$y2*50), size = 2, color = "black")  
  geom_text(aes(x=df$x, y=df$y2*50, label = round(df$y2, 3)),vjust = "outward", hjust = "outward",show.legend = FALSE, color="black")  
  scale_y_continuous(name = "Number of cases", sec.axis = sec_axis(~./50, name = "Percentage"))  
  scale_x_continuous(breaks = 1:3, labels=c("2008-2011","2012-2015","2016-2019"))   xlab(NULL)  
  scale_fill_manual(name = "Resection Index",values = cols)  
  theme_minimal()

I have added legends for the colors (Resection Index). But I do not know how to add one more legend for two lines (solid and dashed). May I ask how to add one more legend for the line types? Thank you very much!!

CodePudding user response:

Basically it's the same as for the fill. As a general rule: If you want to have a legend then you have to map on aesthetics, i.e. move linetype = ... inside aes(), then set your desired linotypes via e.g. scale_linetype_manual.

Note: Using df$... is not recommended in ggplot2. Instead pass your data via the dataargument and simply map variable names on aesthetics. This also works with multiple dataframes:

cols <- c("1" = "grey80", "2" = "grey70", "3" = "grey60", "4" = "grey50", "5" = "grey40")
ltys <- c("1" = 1, "2" = 2)

library(ggplot2)

ggplot(data = df)  
  geom_bar(data = df2, aes(fill = condition, y = y3, x = x), position = "stack", stat = "identity", width = 0.5)  
  geom_line(mapping = aes(x = x, y = y1 * 50, linetype = "1"), size = 1, color = "black")  
  geom_point(mapping = aes(x = x, y = y1 * 50), size = 2, color = "black")  
  geom_text(aes(x = x, y = y1 * 50, label = round(y1, 3)), vjust = "outward", hjust = "outward", show.legend = FALSE, color = "black")  
  geom_line(mapping = aes(x = x, y = y2 * 50, linetype = "2"), size = 1, color = "black")  
  geom_point(mapping = aes(x = x, y = y2 * 50), size = 2, color = "black")  
  geom_text(aes(x = x, y = y2 * 50, label = round(y2, 3)), vjust = "outward", hjust = "outward", show.legend = FALSE, color = "black")  
  scale_y_continuous(name = "Number of cases", sec.axis = sec_axis(~ . / 50, name = "Percentage"))  
  scale_x_continuous(breaks = 1:3, labels = c("2008-2011", "2012-2015", "2016-2019"))  
  xlab(NULL)  
  scale_fill_manual(name = "Resection Index", values = cols)  
  scale_linetype_manual(name = "NAME", values = ltys)  
  theme_minimal()

  • Related