我正在绘制以下直方图:
library(palmerpenguins)
library(tidyverse)
penguins %>%
ggplot(aes(x=bill_length_mm, fill = species)) +
geom_histogram() +
facet_wrap(~species)
Run Code Online (Sandbox Code Playgroud)
对于每个直方图,我想为每个直方图添加一个正态分布,其中包含每个物种的平均值和标准差。
当然,我知道我可以在开始命令之前计算组特定的均值和 SD ggplot,但我想知道是否有更智能/更快的方法来做到这一点。
我努力了:
penguins %>%
ggplot(aes(x=bill_length_mm, fill = species)) +
geom_histogram() +
facet_wrap(~species) +
stat_function(fun = dnorm)
Run Code Online (Sandbox Code Playgroud)
但这只在底部给了我一条细线:
有任何想法吗?谢谢!
编辑 我想我想要重新创建的是来自Stata的这个简单命令:
hist bill_length_mm, by(species) normal
我知道这里有一些建议:using stat_function and facet_wrap Together in ggplot2 in R
但我专门寻找一个简短的答案,不需要我创建单独的函数。
不久前,我用一个函数自动绘制了理论密度,该函数放入我编写的 ggh4x 包中,您可能会觉得很方便。您只需确保直方图和理论密度具有相同的比例(例如每个 x 轴单位的计数)。
library(palmerpenguins)
library(tidyverse)
library(ggh4x)
penguins %>%
ggplot(aes(x=bill_length_mm, fill = species)) +
geom_histogram(binwidth = 1) +
stat_theodensity(aes(y = after_stat(count))) +
facet_wrap(~species)
#> Warning: Removed 2 rows containing non-finite values (stat_bin).
Run Code Online (Sandbox Code Playgroud)

您可以改变直方图的箱大小,但您也必须调整理论密度计数。通常您会乘以 binwidth。
penguins %>%
ggplot(aes(x=bill_length_mm, fill = species)) +
geom_histogram(binwidth = 2) +
stat_theodensity(aes(y = after_stat(count)*2)) +
facet_wrap(~species)
#> Warning: Removed 2 rows containing non-finite values (stat_bin).
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2021-01-27 创建
如果这太麻烦,您始终可以将直方图转换为密度,而不是将密度转换为计数。
penguins %>%
ggplot(aes(x=bill_length_mm, fill = species)) +
geom_histogram(aes(y = after_stat(density))) +
stat_theodensity() +
facet_wrap(~species)
Run Code Online (Sandbox Code Playgroud)