Add legend to geom_vline

后端 未结 1 1855
灰色年华
灰色年华 2020-12-17 09:16

I know that this question has been asked before but the solutions don\'t seem to work for me.

What I want to do is represent my median, mean, upper and lower quanti

相关标签:
1条回答
  • 2020-12-17 09:51

    You need to map the color inside the aes:

    ggplot(aes(x = Sepal.Length), data = iris) + 
      geom_histogram(color = 'black', fill = NA) + 
      geom_vline(aes(xintercept=median(iris$Sepal.Length),
                     color="median"), linetype="dashed",
                 size=1) +
      geom_vline(aes(xintercept=mean(iris$Sepal.Length),
                     color="mean"), linetype="dashed",
                 size=1) +
      scale_color_manual(name = "statistics", values = c(median = "blue", mean = "red"))
    

    0 讨论(0)
提交回复
热议问题