繁体   English   中英

在同一图中绘制正态分布和二项分布

[英]Plotting the normal and binomial distribution in same plot

正如标题所示,我正在尝试使用 R 在同一图中绘制正态分布和二项式分布。我的尝试如下所示,是否有任何原因为什么我的正态分布看起来如此偏离? 我已经仔细检查了平均值和标准偏差,一切看起来都很好。

在此处输入图片说明

n <- 151
p <- 0.2409

dev <- 4
mu <- n*p
sigma <- sqrt(n*p*(1 - p))

xmin <- round(max(mu - dev*sigma,0));
xmax <- round(min(mu + dev*sigma,n))
x <- seq(xmin, xmax)
y <- dbinom(x,n,p)

barplot(y, 
     col = 'lightblue',
     names.arg = x,
     main = 'Binomial distribution, n=151, p=.803')

range <- seq(mu - dev*sigma, mu + dev*sigma, 0.01)
height <- dnorm(range, mean = mu, sd = sigma) 
lines(range, height, col = 'red', lwd = 3)

barplot只是您案例的错误功能。 或者,如果您真的想使用它,则必须重新调整barplotlines之间的 x 轴

barplot的默认值是将每个height值放在

head(c(barplot(y, plot = FALSE)))
# [1] 0.7 1.9 3.1 4.3 5.5 6.7

这可以通过您选择的spacewidth或两者的组合来改变

head(c(barplot(y, plot = FALSE, space = 0)))
# [1] 0.5 1.5 2.5 3.5 4.5 5.5

head(c(barplot(y, plot = FALSE, space = 0, width = 3)))
# [1]  1.5  4.5  7.5 10.5 13.5 16.5

你可以使用plot来避免处理这些事情

n <- 151
p <- 0.2409

dev <- 4
mu <- n*p
sigma <- sqrt(n*p*(1 - p))

xmin <- round(max(mu - dev*sigma,0));
xmax <- round(min(mu + dev*sigma,n))
x <- seq(xmin, xmax)
y <- dbinom(x,n,p)

plot(x, y, type = 'h', lwd = 10, lend = 3, col = 'lightblue',
     ann = FALSE, las = 1, bty = 'l', yaxs = 'i', ylim = c(0, 0.08))
title(main = sprintf('Binomial distribution, n=%s, p=%.3f', n, p))
lines(x, dnorm(x, mean = mu, sd = sigma), col = 'red', lwd = 7)

xx <- seq(min(x), max(x), length.out = 1000)
lines(xx, dnorm(xx, mean = mu, sd = sigma), col = 'white')

在此处输入图片说明

此图中的“条”取决于您选择的lwd和您的设备尺寸,但如果您需要更好地控制它,您可以使用rect ,这需要更多的工作。

w <- 0.75
plot(x, y, type = 'n', ann = FALSE, las = 1, bty = 'l', yaxs = 'i', ylim = c(0, 0.08))
rect(x - w / 2, 0, x + w / 2, y, col = 'lightblue')
lines(xx, dnorm(xx, mean = mu, sd = sigma), col = 'red', lwd = 3)
title(main = sprintf('Binomial distribution, n=%s, p=%.3f', n, p))

在此处输入图片说明

您可以使用ggplot2

library(ggplot2)

n <- 151
p <- 0.2409
mean <- n*p
sd <-   sqrt(n*p*(1-p))
binwidth <-   0.005


xmin <- round(max(mu - dev*sigma,0));
xmax <- round(min(mu + dev*sigma,n))
x <- seq(xmin, xmax)
y <- dbinom(x,n,p)

df <- cbind.data.frame(x, y)

ggplot(df, aes(x = x, y = y)) +
  geom_bar(stat="identity", fill = 'dodgerblue3')+
  labs(title = "Binomial distribution, n=151, p=.803",
       x = "",
       y = "") +
  theme_minimal()+
  # Create normal curve, akousting for number of observations and binwidth
  stat_function( 
    fun = function(x, mean, sd, n, bw){ 
      dnorm(x = x, mean = mean, sd = sd)
    }, col = "red", size=I(1.4),  
    args = c(mean = mean, sd = sd, n = n, bw = binwidth))

在此处输入图片说明

您可以使用ggplot2包来做到这ggplot2 (我对正态分布感到惊讶,但用 geom_point 替换 geom_line 说服我具有这种形式(方差是否太高?)):

n <- 151
p <- 0.2409

dev <- 4
mu <- n*p
sigma <- sqrt(n*p*(1 - p))

xmin <- round(max(mu - dev*sigma,0));
xmax <- round(min(mu + dev*sigma,n))

x <- seq(xmin, xmax)
y <- dbinom(x,n,p)

z <- dnorm(x = qnorm(p = seq(0,1, length.out = length(x)), mean = mu, sd = sigma), mean = mu, sd = sigma)

library(magrittr)
library(ggplot2)
data.frame(x, y, z) %>% 
    ggplot(aes(x = x)) +
    geom_col(aes(y = y)) +
    geom_line(aes(x = x, y = z, colour = "red"),
          show.legend = FALSE)

暂无
暂无

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

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