簡體   English   中英

在同一圖中繪制多個法線曲線

[英]Plot multiple normal curves in same plot

我對創建一個示例圖(最好使用ggplot)感興趣,該示例圖將顯示兩條具有不同均值和不同標准偏差的法線。 我發現了ggplot的stat_function()參數,但不確定如何在同一圖上獲得第二條曲線。

此代碼產生一條曲線:

ggplot(data.frame(x = c(-4, 4)), aes(x)) + stat_function(fun = dnorm)

關於獲得第二條曲線的方法有什么建議嗎? 還是在基本封裝圖中更簡單?

以防萬一您也想在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')
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM