简体   繁体   English

密度 plot 和 ggplot2 中的直方图

[英]Density plot and histogram in ggplot2

I have the following data frame我有以下数据框

        x1<-data.frame(n = rnorm(1000000, mean=0, sd=1), nombre= "x1")

        x2<-data.frame(n=rnorm(1500000, mean=3, sd=1), nombre= "x2")

        x<-rbind(x1, x2)

        ggplot(x, aes(n, fill=nombre))+
          geom_histogram(alpha=0.5, binwidth=0.25, position = "identity")+
          geom_density()

I would like to overlay the density plot to the histogram, but it just appears like a thin line in 0我想将密度 plot 叠加到直方图上,但它看起来就像 0 中的细线

在此处输入图像描述

You'll need to get geom_histogram and geom_density to share the same axis.您需要让geom_histogramgeom_density共享同一轴。 In this case, I've specified both to plot against density by adding the aes(y=..density) term to geom_histogram .在这种情况下,我通过将aes(y=..density)项添加到 geom_histogram 来针对密度指定geom_histogram Note also some different aesthetics to avoid overplotting and so that we are able to see both geoms a bit more clearly:还要注意一些不同的美学以避免过度绘制,以便我们能够更清楚地看到两个几何:

ggplot(x, aes(n, fill=nombre))+
    geom_histogram(aes(y=..density..), color='gray50',
        alpha=0.2, binwidth=0.25, position = "identity")+
    geom_density(alpha=0.2)

在此处输入图像描述

As initially specified, the aesthetics fill= applies to both, so you have the histogram and density geoms showing you distribution grouped according to "x1" and "x2".正如最初指定的那样,美学fill=适用于两者,因此您有直方图和密度几何显示您根据“x1”和“x2”分组的分布。 If you want the density geom for the combined set of x1 and x2, just specify the fill= aesthetic for the histogram geom only:如果您想要 x1 和 x2 的组合集的密度几何,只需为直方图几何指定fill=美学:

ggplot(x, aes(n))+
    geom_histogram(aes(y=..density.., fill=nombre),
        color='gray50', alpha=0.2,
        binwidth=0.25, position = "identity")+
    geom_density(alpha=0.2)

在此处输入图像描述

I have come up with an idea that allow you to scale your density plot according to your histogram.我想出了一个想法,允许您根据直方图缩放密度 plot。

You can get density data using stat:density function and scale them manually, then plot them using geom_line :您可以使用stat:density function 获取密度数据并手动缩放它们,然后使用geom_line plot 它们:

ggplot(x, aes(n, fill=nombre))+
  geom_histogram(alpha=0.5, binwidth=0.25, position = "identity") +
  geom_line(aes(x,y, group=nombre),
    ~ .x %>% group_by(nombre) %>%
      mutate(n_cut = cut(n, seq(min(n), max(n), 0.25))) %>% 
      summarize(
        y = density(n)$y * max(table(n_cut)) / max(density(n)$y),
        x = density(n)$x
      )
  )

结果

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

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