R为多层添加图例

时间:2019-01-14 03:18:55

标签: r add legend

我想为剧情添加一个图例,但它不起作用, 谁能帮我看看问题出在哪里。

这是代码。

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"))

1 个答案:

答案 0 :(得分:0)

通常可以通过在一个geom图层中设置aes(color = column_title)来生成图例。这段代码没有特别的意义,因为您要引用多个y轴而不创建第二个y轴(如果这样做,这是一个坏习惯)。有没有办法发布更相关的代码或可复制的示例,以便人们可以准确地看到您要执行的操作?