Home > Software engineering >  How to add legends when using secondary axis
How to add legends when using secondary axis

Time:09-18

I am working with a graph consisting of raw numbers by year plotted as a bar chart with a secondary line graph indicating proportion.

specie <- c(rep("sorgho" , 6) , rep("poacee" , 6) , rep("banana" , 6) , rep("triticum" , 6) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 8)
year <- sample(2010:2015, 24, T)
value <- abs(rnorm(24 , 0 , 15))
test_data <- data.frame(specie,condition,year,value) %>% 
  arrange(year, condition)


smrydata <- 
  test_data %>%
  group_by(year) %>%
  mutate(total = sum(value)) %>%
  group_by(condition, year) %>%
  summarise(value = sum(value),
            prop = value/total, 
            .groups = "drop") %>% 
  arrange(year, condition) %>% 
  distinct()

Max <- max(smrydata$value)


ggplot(smrydata, aes(year, value, group = 1))  
  scale_linetype_manual("My Legend", values = 1)  
  geom_col(fill = "steelblue")  
  geom_line(aes(y = prop * Max,  linetype = "%"), colour = "orange", size = 2)  
  scale_y_continuous(sec.axis = sec_axis( ~ ./Max, labels = scales::percent))  
  theme_bw()  
  facet_wrap(~condition)  
  theme(legend.position = "bottom")

When I run this code, I can produce my graph with a legend for the proportion line, but I'm not sure how to produce the legend for the bar chart. Ideally they should appear together grouped with the legend title ("My Legend"), and I would prefer the bar chart legend to appear on top of the line legend (blue "n", orange "%").

enter image description here

(This is not the real data I'm working with, so I am not concerned about some issues with the current plot caused by incomplete data, such as the proportions line not being "0%" when there are no corresponding values)

CodePudding user response:

You should mention the fill in your aes and you could mention legend.box='vertical' in your theme like this:

library(dplyr)
specie <- c(rep("sorgho" , 6) , rep("poacee" , 6) , rep("banana" , 6) , rep("triticum" , 6) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 8)
year <- sample(2010:2015, 24, T)
value <- abs(rnorm(24 , 0 , 15))
test_data <- data.frame(specie,condition,year,value) %>% 
  arrange(year, condition)


smrydata <- 
  test_data %>%
  group_by(year) %>%
  mutate(total = sum(value)) %>%
  group_by(condition, year) %>%
  summarise(value = sum(value),
            prop = value/total, 
            .groups = "drop") %>% 
  arrange(year, condition) %>% 
  distinct()

Max <- max(smrydata$value)

library(ggplot2)
ggplot(smrydata)  
  scale_linetype_manual("My Legend", values = 1)  
  geom_col(aes(year, value, group = 1, fill = "steelblue"))  
  geom_line(aes(x = year, y = prop * Max,  linetype = "%"), colour = "orange", size = 2)  
  scale_y_continuous(sec.axis = sec_axis( ~ ./Max, labels = scales::percent))  
  scale_fill_manual('', values = 'steelblue', label = 'n')  
  theme_bw()  
  facet_wrap(~condition)  
  theme(legend.position = "bottom", legend.box = 'vertical')

Created on 2022-09-18 with reprex v2.0.2

CodePudding user response:

From doing some extra digging, I found a nifty response on https://community.rstudio.com/t/adding-a-legend-to-an-overlay-bar-and-line-plot/69331 that shows how to add a legend in the way I'm looking for

  • Related