ggplot在使用`facet_wrap`时添加正态分布 [英] ggplot add Normal Distribution while using `facet_wrap`

查看:115
本文介绍了ggplot在使用`facet_wrap`时添加正态分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找以下直方图:

 库(palpenpenguins)图书馆(tidyverse)企鹅%&%;%ggplot(aes(x = bill_length_mm,填充=种类))+geom_histogram()+facet_wrap(〜species) 

对于每个直方图,我想为每个直方图添加一个正态分布,并带有每个物种的均值和标准差.

当然,我知道在开始使用 ggplot 命令之前,我可以计算出特定于组的均值和SD,但是我想知道是否有更聪明/更快的方法来实现这一点.

>

我尝试过:

 企鹅%>%ggplot(aes(x = bill_length_mm,填充=种类))+geom_histogram()+facet_wrap(〜species)+stat_function(fun = dnorm) 

但这只会在底部给我一个细线:

有什么想法吗?谢谢!

修改我想我要重新创建的是来自 Stata 的简单命令:

hist bill_length_mm,由(物种)正常

这给了我这个:

我了解这里有一些建议:(v0.3.0)创建于2021年1月27日

如果这太麻烦了,您总是可以将直方图转换为密度,而不是将密度转换为计数.

 企鹅%>%ggplot(aes(x = bill_length_mm,填充=种类))+geom_histogram(aes(y = after_stat(density)))+stat_theodensity()+facet_wrap(〜species) 

I'm looking to plot the following histograms:

library(palmerpenguins)
library(tidyverse)

penguins %>% 
  ggplot(aes(x=bill_length_mm, fill = species)) +
  geom_histogram() + 
  facet_wrap(~species)

For each histogram, I would like to add a Normal Distribution to each histogram with each species mean and standard deviation.

Of course I'm aware that I could compute the group specific mean and SD before embarking on the ggplot command, but I wonder whether there is a smarter/faster way to do this.

I have tried:

penguins %>% 
  ggplot(aes(x=bill_length_mm, fill = species)) +
  geom_histogram() + 
  facet_wrap(~species) + 
  stat_function(fun = dnorm)

But this only gives me a thin line at the bottom:

Any ideas? Thanks!

Edit I guess what I'm trying to recreate is this simple command from Stata:

hist bill_length_mm, by(species) normal

which gives me this:

I understand that there are some suggestions here: using stat_function and facet_wrap together in ggplot2 in R

But I'm specifically looking for a short answer that does not require me creating a separate function.

解决方案

A while I ago I sort of automated this drawing of theoretical densities with a function that I put in the ggh4x package I wrote, which you might find convenient. You would just have to make sure that the histogram and theoretical density are at the same scale (for example counts per x-axis unit).

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).

You can vary the bin size of the histogram, but you'd have to adjust the theoretical density count too. Typically you'd multiply by the 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).

Created on 2021-01-27 by the reprex package (v0.3.0)

If this is too much of a hassle, you can always convert the histogram to density instead of the density to counts.

penguins %>% 
  ggplot(aes(x=bill_length_mm, fill = species)) +
  geom_histogram(aes(y = after_stat(density))) + 
  stat_theodensity() +
  facet_wrap(~species)

这篇关于ggplot在使用`facet_wrap`时添加正态分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆