如何在r中绘制均值相同但方差不同的正态分布

how to plot normal distribution with the same mean but different variance in r

我试图绘制 6 个正态分布图来显示均值和方差对此类图的影响,我的代码如下:

    par(mfrow=c(3,2),bty = "n")     # 3 rows by 2 columns, turn off border
    mu <- c(6, 8, 6, 8, 6, 8)       #designate the 6 mean values
    sigma <- c(3, 3, 2, 2, 1, 1)        #designate the 6 sd values
    label <- c("(a)","(b)","(c)","(d)","(e)","(f)") #designate the 6 labels of the 6 figures
    for(i in 1:length(mu))          
    {
     mu.r <- mu[i]          
     sigma.r <- sigma[i]        
     lab.r <- label[i]      
     x <- seq((mu.r - 4*sigma.r), (mu.r + 4*sigma.r), len = 200)
    #designate the starting and ending value of mean
     plot(x, dnorm(x, mean = mu.r, sd = sigma.r),axes = F,
         type="l",lwd = 2, xlab = lab.r, ylab = "",
              main=paste0('mu=',mu.r,', sigma=',sigma.r),
           )
    axis(1, at = (mu.r - 4*sigma.r) : (mu.r + 4*sigma.r))
    abline(v = mu.r, col = "red", lwd = 2.5, lty = "longdash")
         
    }
    the figures generated is as follow:
   [enter image description here][1]


  [1]: https://i.stack.imgur.com/Z4czh.png

你没有说到底是什么问题。我假设你所有的图表看起来都一样。发生这种情况是因为您根据方差设置了 x 轴,您需要将所有图表保留在相同的比例上以便比较它们。我只是在均值附近设置了一个任意的 7 区间:

for(i in 1:length(mu))          
{
  mu.r <- mu[i]          
  sigma.r <- sigma[i]        
  lab.r <- label[i]      
  x <- (mu.r - 7):(mu.r + 7)
  #designate the starting and ending value of mean
  plot(x, dnorm(x, mean = mu.r, sd = sigma.r),axes = F,
       type="l",lwd = 2, xlab = lab.r, ylab = "",
       main=paste0('mu=',mu.r,', sigma=',sigma.r),
  )
  axis(1, at = x)
  abline(v = mu.r, col = "red", lwd = 2.5, lty = "longdash")
  
}

输出: