本文介绍了如何将多条曲线和曲线与R和ggplot结合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过在 ggplot
中绘制多个 dnorm
曲线来模拟色谱图,
I would like to simulate a chromatogram by plotting multiple dnorm
curves in ggplot
similar to this:
ggplot(data.frame(x = 0), aes(x = x)) +
mapply(function(mean, sd, col) {
stat_function(fun = dnorm, args = list(mean = mean, sd = sd), col = col)
},
mean = c(0, 1, .5),
sd = c(1, .5, 2),
col = c('red', 'blue', 'green')) +
xlim(-5, 5) +
theme_classic()
但是,我希望通过将x轴上每个点处的曲线求和(即 dnorm(x,0,1)+ dnorm(x,1,0.5)+dnorm(x,0.5,2)
其中-5< x< 5):
However, instead of plotting separately I would like to combined them by summing the curves at each point on the x-axis (i.e. dnorm(x, 0, 1) + dnorm(x, 1, 0.5) + dnorm(x, 0.5, 2)
where -5 < x < 5):
我可以用数字方式执行此操作,但如果可能的话,最好使用 stat_function()
(或类似方法).请告知.
I could do this numerically, but would prefer to use stat_function()
(or similar) if possible. Please advise.
推荐答案
ggplot(data.frame(x = 0), aes(x = x)) +
stat_function(fun = function(x) rowSums(mapply(dnorm, mean = c(0, 1, .5),
sd = c(1, .5, 2), MoreArgs = list(x = x)))) +
xlim(-5, 5) +
theme_classic()
这篇关于如何将多条曲线和曲线与R和ggplot结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!