'How do I create one legend for a multiple layer plot?
I'm making a water balance that has to show montlhy values for the amount of evaporation, the amount of discharge and the amount of precipitation. The evaporation and the discharge are outgoing values and are plotted using a stacked barplot. The precipitation is an ingoing term and is plotted using geom_point. However, I'm not able to get the geom_point in the same legend as the legend for the barplot. I'm not even able to create a legend for the geom_point at all. To be honest, the code that I'm using right now for creating a legend for the barplot is also not completely clear to me. Is there somebody who knows how to create a legend for the geom_point and how to merge this one into the legend for the geom_bar?
This is my code so far:
ggplot() + labs(x = "Months", y = "Average amount (mm/month)", color = "Water balance terms:") +
scale_y_continuous(expand = c(0,0)) +
geom_bar(data, mapping = aes(fill=condition, y=value, x=months_3), position="stack", stat="identity") +
geom_point(linedata, mapping = aes(x = months_V, y = prec_data, shape = "precipitation"),
shape = 95, size = 20, fill = "#3498DB", color = "#3498DB") +
scale_fill_manual(name = "Water balance terms:",
labels = c("evaporation ", "discharge by drainage ", "discharge by rain sewage ", "precipitation"),
values = c("#F1C40F", "#D35400" ,"#E67E22", "#3498DB")) +
scale_shape_manual(name = "Water balance terms:",
labels = c("evaporation ", "discharge by drainage ", "discharge by rain sewage ", "precipitation"),
values=c("#F1C40F", "#D35400" ,"#E67E22", "#3498DB")) +
theme(legend.position = "bottom", legend.justification = "left",
panel.grid.major.y = element_line(colour = "grey"), panel.grid.minor.y = element_line(colour = "grey"),
panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(),
panel.background = element_rect(fill = "white"),
axis.line = element_line())```
I already found on the internet that for merging legends, scale_fill_manual and scale_shape_manual should have the same name and labels, so that's what I did. However, I'm also not really sure how scale_fill_manual differs from scale_shape_manual. I hope someone can help me out!
Cheers, Anna
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
