Home > Software engineering >  Force ggplot2 to show legends (barchar and geom_line)
Force ggplot2 to show legends (barchar and geom_line)

Time:11-23

Easy question: I'm creating a barchart in ggplot2 with both a stacked barchart and a geom_line referring to different parts of the same dataset.

No matter what I do, I'm unable to show the legends (one for the stacked barchart and another one for the geom_line), but I need these as neither of the axis should be labelled.

What do I do?

This is my dataset:

> dput(par_brood_2020)
structure(list(`Prevalence-host.age.relationship` = c("<7 days", 
"7-10 days", "10-12 days", "12-15 days", "15-20 days", "20-25 days"
), Broods = c(6, 6, 3, 2, 4, 1), Parasites = c(92, 66, 32, 9, 
27, 5)), row.names = c(NA, 6L), class = "data.frame")

This is my code:

ggplot(par_nest_2020, aes(x=par_nest_2020$"Prevalence- 
host.age.relationship",y=par_nest_2020$"Nestlings"))   geom_col()  
geom_bar(fill="#808080", stat="identity")  
xlab(" ")   ylab(" ")  
ggtitle("2020 - HOST-AGE RELATIONSHIP")  
scale_fill_manual(name=" ",values="#808080")  
theme_classic()  
theme(legend.key.size = unit (10, "pt"))    
theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'),
      axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid'))  
theme(title = element_text(face="plain", size = 12))  
theme(plot.title = element_text(hjust = 0.5, vjust =2))  
theme(axis.text.x = element_text(angle = 45, hjust=1))  
theme(panel.background = element_blank())  
geom_line(data=par_nest_2020,aes(x =par_nest_2020$"Prevalence-host.age.relationship", y 
=par_nest_2020$"Parasites"), group = 1, col = "#E60000", show.legend=TRUE)

CodePudding user response:

If you want to have a legend you have to map on aesthetics, i.e. you have to map on color and fill inside aes(). In your case you could simply use some constant values like e.g. color="Broods" which will then automatically show up as labels. Additionally, as a general rule when using ggplot2 don't use par_nest_2020$... to map on aesthetics. Simply use the column names.

library(ggplot2)

par_brood_2020 <- structure(list(
  `Prevalence-host.age.relationship` = c("<7 days", "7-10 days", "10-12 days", "12-15 days", "15-20 days", "20-25 days"),
  Broods = c(6, 6, 3, 2, 4, 1), Parasites = c(92, 66, 32, 9, 27, 5)
), row.names = c(NA, 6L), class = "data.frame")

par_brood_2020$`Prevalence-host.age.relationship` <- factor(par_brood_2020$`Prevalence-host.age.relationship`, c("<7 days", "7-10 days", "10-12 days", "12-15 days", "15-20 days", "20-25 days"))

ggplot(par_brood_2020, aes(x = `Prevalence-host.age.relationship`, y = Broods))  
  geom_col(aes(fill = "Broods"))  
  geom_line(aes(y = Parasites, color = "Parasites"), group = 1)  
  labs(x = NULL, y = NULL, title = "2020 - HOST-AGE RELATIONSHIP", color = NULL, fill = NULL)  
  scale_fill_manual(values = "#808080")  
  scale_color_manual(values = "#E60000")  
  theme_classic()  
  theme(legend.key.size = unit(10, "pt"))  
  theme(
    axis.line.x = element_line(colour = "black", size = 0.5, linetype = "solid"),
    axis.line.y = element_line(colour = "black", size = 0.5, linetype = "solid")
  )  
  theme(title = element_text(face = "plain", size = 12))  
  theme(plot.title = element_text(hjust = 0.5, vjust = 2))  
  theme(axis.text.x = element_text(angle = 45, hjust = 1))  
  theme(panel.background = element_blank())
#> Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.

  • Related