Using legend with stat_function in ggplot2

后端 未结 1 1865
遇见更好的自我
遇见更好的自我 2020-12-15 06:45

I am using scale_colour_manual to specify the possible keys in the legend. However, if I use stat_function to plot custom function, the legend is m

相关标签:
1条回答
  • 2020-12-15 07:24

    Put colour= inside the aes() and then provide name for particular line as is should appear in legend. Legend is made for aesthetics that are only inside aes() call.

    ggplot(my.df, aes(x=x)) +
      stat_function(fun = MyFun, n = 1000, args = list(p = 10), aes(colour = "line1")) +
      stat_function(fun = MyFun, n = 1000, args = list(p = 3), aes(colour = "line2")) +
      stat_function(fun = MyFun, n = 1000, args = list(p = 2), aes(colour = "line3")) +
      stat_function(fun = MyFun, n = 1000, args = list(p = 1), aes(colour = "line4")) +
      scale_colour_manual("Lgend title", values = c("red", "blue", "green", "orange"))
    

    enter image description here

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