我感兴趣的是创建一个示例图(最好是使用ggplot),它将以不同的平均值和不同的标准差显示两条正常曲线。我已经发现了ggplot的stat_function()参数,但不知道如何在同一图上获得第二条曲线。
此代码生成一条曲线:
ggplot(data.frame(x = c(-4, 4)), aes(x)) + stat_function(fun = dnorm)对如何获得第二条曲线有什么建议吗?或者在基本包绘图中做得更简单?
发布于 2014-11-19 09:07:40
万一您也想在ggplot中这样做(这也是3行.)。
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 = 1, sd = .5), col='blue')如果您有两条以上的曲线,最好使用mapply。这使它变得更加困难。但对于许多功能来说,这可能是值得的。
ggplot(data.frame(x = c(-4, 4)), aes(x)) +
mapply(function(mean, sd, col) {
stat_function(fun = dnorm, args = list(mean = mean, sd = sd), col = col)
},
# enter means, standard deviations and colors here
mean = c(0, 1, .5),
sd = c(1, .5, 2),
col = c('red', 'blue', 'green')
)https://stackoverflow.com/questions/27009641
复制相似问题