繁体   English   中英

密度 plot 和 ggplot2 中的直方图

[英]Density plot and histogram in ggplot2

我有以下数据框

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

我想将密度 plot 叠加到直方图上,但它看起来就像 0 中的细线

在此处输入图像描述

您需要让geom_histogramgeom_density共享同一轴。 在这种情况下,我通过将aes(y=..density)项添加到 geom_histogram 来针对密度指定geom_histogram 还要注意一些不同的美学以避免过度绘制,以便我们能够更清楚地看到两个几何:

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)

在此处输入图像描述

正如最初指定的那样,美学fill=适用于两者,因此您有直方图和密度几何显示您根据“x1”和“x2”分组的分布。 如果您想要 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)

在此处输入图像描述

我想出了一个想法,允许您根据直方图缩放密度 plot。

您可以使用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