简体   繁体   English

使用ggplot2在R中具有正态分布的直方图

[英]Histogram with normal Distribution in R using ggplot2 for illustrations

I'm trying to plot a histogram with ggplot2. 我正在尝试使用ggplot2绘制直方图。

I wrote a simple code for this in R 我在R中为此编写了一个简单的代码

dnorm.count <- function(x, mean = 0, sd = 1, log = FALSE, n = 1, binwidth = 1){
  n * binwidth * dnorm(x = x, mean = mean, sd = sd, log = log) 
}
mtcars %>% 
  ggplot(aes(x =  mpg)) +
  geom_histogram(bins =60,color = "white", fill = "#9FE367",boundary = 0.5) +
  geom_vline(aes(xintercept = mean(mpg)), 
             linetype="dashed",
             size = 1.6, 
             color = "#FF0000")+
  geom_text(aes(label = ..count..), stat= "count",vjust = -0.6)+
  stat_function(fun = dnorm.count, color = "#6D67E3", 
                args = list(mean= mean(mtcars$mpg), 
                            sd = sd(mtcars$mpg),
                            n = nrow(mtcars)), 
                lwd = 1.2) +
  scale_y_continuous(labels = comma, name = "Frequency") +
  scale_x_continuous(breaks=seq(0,max(mtcars$mpg)))+
  geom_text(aes(label = paste0("mean = ", round(mean(mtcars$mpg), 2)), 
                x = mean(mtcars$mpg)*1.2, 
                y  = mean(mtcars$mpg)/5))+
  geom_vline(aes(xintercept = sd(mpg)), linetype="dashed",size = 1.6, color = "#FF0000") 

What I got is this! 我得到的是这个!

在此处输入图片说明

The question is how do I Plot the histogram similar to this 问题是如何绘制类似于此的直方图 在此处输入图片说明

using ggplot2 and is it possible to convert the code to R function? 使用ggplot2,是否可以将代码转换为R函数?

Edit: For the better explanation of what I'm trying to do: 编辑:为了更好地解释我要做什么:


I wanna create a Histogram exactly the same as the one attached for reference using ggplot2 and then I wanna create a function for the same to reduce the coding. 我想创建一个与使用ggplot2附加供参考的直方图完全相同的直方图,然后我想为其创建一个函数以减少编码。 Use any package+ggplot2 you like. 使用任何您喜欢的package + ggplot2。 The histograms should have lines depicting the standard deviation & mean like the one in reference. 直方图应有描述标准偏差和平均值的线,如参考线所示。 If possible depict the standard deviation in the plot as the reference image, that's what I'm trying to achieve. 如果可能的话,将绘图中的标准偏差描述为参考图像,这就是我要达到的目标。

If your question how to plot histograms like the one you attached in your last figure, this 9 lines of code produce a very similar result. 如果您的问题是如何像上图中那样绘制直方图,那么这9行代码会产生非常相似的结果。

library(magrittr) ; library(ggplot2)
set.seed(42)
data <- rnorm(1e5)
p <- data %>%
  as.data.frame() %>%
  ggplot(., aes(x = data)) +
  geom_histogram(fill = "white", col = "black", bins = 30 ) +
  geom_density(aes( y = 0.3 *..count..)) +
  labs(x = "Statistics", y = "Probability/Density") +
  theme_bw() + theme(axis.text = element_blank())

You could use annotate() to add symbols or text and geom_segment to show the intervals on the plot like this: 您可以使用annotate()添加符号或文本以及geom_segment以在绘图上显示间隔,如下所示:

p + annotate(x = sd(data)/2 , y = 8000, geom = "text", label = "σ", size = 10) +
    annotate(x = sd(data) , y = 6000, geom = "text", label = "2σ", size = 10) +
    annotate(x = sd(data)*1.5 , y = 4000, geom = "text", label = "3σ", size = 10) +     
    geom_segment(x = 0, xend = sd(data), y = 7500, yend = 7500) +
    geom_segment(x = 0, xend = sd(data)*2, y = 5500, yend = 5500) +
    geom_segment(x = 0, xend = sd(data)*3, y = 3500, yend = 3500) 

This chunk of code would give you something like this: 这段代码将为您提供以下信息: 在此处输入图片说明

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

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