简体   繁体   中英

R add legend for multiple layers

I want to add a legend for the plot, but it doesn't work, can anyone please help me to see where it went wrong.

this is the code.

 ggplot(data = dfNorm1, aes(x = X)) + geom_col(aes(y = Government_suppliment), fill = "#0000FF", color = "white", alpha = 0.8) + geom_smooth(data = subset(dfNorm1,X >= 24), aes(y = Government_suppliment), method = "lm", se = FALSE, color = "#FF4040", linetype = "dashed", size = 0.7) + geom_smooth(data = subset(dfNorm1, X <= 24), aes(y = Government_suppliment), method = "lm", se = FALSE, color = "#FF4040", linetype = "dashed", size = 0.7) + geom_vline(xintercept = 24.5, size = 0.8, alpha = 0.8) + geom_line(aes(y = Poverty_funds), color = "#FF0000", size = 1, alpha = 0.7) + geom_line(aes(y = MLI), color = "#EF3EFF", size = 1, alpha = 0.8) + scale_fill_manual(name = "",values = c("bar.label" = "#0000FF")) + scale_color_manual(name = "", values = c("line.label1" = "#FF0000", "line.label2" = "#EF3EFF", "line.labeld" = "#FF4040"))

You usually can produce a legend by setting aes(color = column_title) in one of your geom layers. This code doesn't particularly make sense because you are referencing more than one y-axis without creating a second y-axis (a bad habit if you are trying to do so). Is there a way you can post more relevant code or a reproducible example so people can see exactly what you're trying to do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM