繁体   English   中英

如何在 r 中具有相同均值但方差不同的 plot 正态分布

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

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

    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")
  
}

Output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM