具有经验密度和范数函数的直方图叠加

时间:2018-06-26 13:10:19

标签: r ggplot2

我想用经验和正常密度曲线覆盖ggplot直方图(y轴=计数)。我尝试过:

library(ggplot2) 
set.seed(1234) 
v <- as_tibble(rnorm(1000, 10, 2.5)) 
ggplot(v, aes(x = value)) +
        geom_histogram(aes(y = ..density..), 
                       bins = 40,  colour = "black", fill = "white") +
        geom_line(aes(y = ..density.., color = 'Empirical'), stat = 'density') +     
        stat_function(fun = dnorm, aes(color = 'Normal'),
                         args = list(mean = 10, sd = 2.5)) +
        scale_colour_manual(name = "Colors", values = c("red", "blue"))

enter image description here

但是它的密度为y刻度,我希望频率为y轴。

我的第二次试验制作了以频率(计数)为y轴但仅以经验密度为图的图。

library(ggplot2)
set.seed(1234)
v <- as_tibble(rnorm(1000, 10, 2.5))
b  <- seq(0, 20, by = 0.5)
p1 <- ggplot(v, aes(x = value)) +
    geom_histogram(aes(y = ..count..), 
                   breaks = b,
                   binwidth = 0.5,  
                   colour = "black", 
                   fill = "white") +
    geom_line(aes(y = ..density.. * (1000 * 0.5),
                    color = 'Empirical'),
                    stat = 'density') +
    scale_colour_manual(name = "Colors", values = c("red", "blue"))

我无法在同一图中显示一条标准曲线。例如,当我尝试下一行时,我在x轴上得到了密度曲线(蓝线)。

p2 <- p1 + stat_function(fun = dnorm, aes(color = 'Normal'),
                     args = list(mean = 10, sd = 2.5))
p2  

enter image description here

我假设我必须按照二进制宽度(与经验线一样)调整曲线,但是我不知道该怎么做。

我在SO中搜索了此问题,并且可以找到许多类似的问题。但是所有这些都解决了我的第一个试验(密度为y轴),带有计数轴的经验叠加(第二个试验)或使用了我不熟悉的其他(基本)绘图命令的问题。

1 个答案:

答案 0 :(得分:2)

我按照来自@ user20650的链接重写了代码,并将@PatrickT的答案应用于我的问题。

library(ggplot2)
n = 1000
mean = 10
sd = 2.5
binwidth = 0.5
set.seed(1234)
v <- as_tibble(rnorm(n, mean, sd))
b  <- seq(0, 20, by = binwidth)
ggplot(v, aes(x = value, mean = mean, sd = sd, binwidth = binwidth, n = n)) +
    geom_histogram(aes(y = ..count..), 
           breaks = b,
           binwidth = binwidth,  
           colour = "black", 
           fill = "white") +
    geom_line(aes(y = ..density.. * n * binwidth, colour = "Empirical"),
           size = 1, stat = 'density') +
    stat_function(fun = function(x) 
           {dnorm(x, mean = mean, sd = sd) * n * binwidth}, 
           aes(colour = "Normal"), size = 1) +
    labs(x = "Score", y = "Frequency") +
    scale_colour_manual(name = "Line colors", values = c("red", "blue"))

决定性的变化在stat-function行中,其中提供了对n和binwidth的必要适应。此外,我不知道有人可以将参数传递给aes()。

enter image description here