R:ggplot手动添加图例

时间:2017-02-18 13:20:05

标签: r ggplot2

我试图在同一图表上绘制多个正态分布。下面的代码有效,但如何在图表中添加显示相应均值和标准差的图例。

ggplot(data.frame(x = c(-4, 4)), aes(x)) + 
stat_function(fun = dnorm, args = list(mean = 0, sd = 1), col='red') +
stat_function(fun = dnorm, args = list(mean = 0, sd = .5), col='blue')

1 个答案:

答案 0 :(得分:1)

你可以尝试:

ggplot(data.frame(x = c(-4, 4)), aes(x)) + 
  stat_function(fun = dnorm, args = list(mean = 0, sd = 1), aes(colour = "mean = 0 / sd =1")) +
  stat_function(fun = dnorm, args = list(mean = 0, sd = .5),aes(colour = "mean = 0 / sd =0.5"))+
  scale_colour_manual("Parameters",values=c("blue","red"))

enter image description here