【问题标题】:GGPLOT relative frequency histogramGGPLOT 相对频率直方图
【发布时间】:2020-07-16 20:32:15
【问题描述】:

我正在尝试绘制来自 3 个集群的一维数据的相对频率。我想要的是一个使用颜色来区分 3 个集群的单个直方图,并且我希望每个 bin 的高度代表特定集群的该值范围的相对频率。

代码如下:

library(mvtnorm)
library(gtools)
library(ggplot2)

K       = 3                    # number of clusters
p_p     = c(0.25, 0.25, 0.5)   # population weights
theta_p = c(2, 5, 15)          # population gamma params - shape
phi_p   = c(2,2, 5)            # population gamma params - scale


N_p = c(25, 25, 50)            # sample size within each cluster
set.seed(1)                    # set seed so that the results are the same each time
y <- numeric()          
## We will now sample data from all three clusters
y[1:N_p[1]]                    <- rgamma(N_p[1], theta_p[1], phi_p[1])
y[(N_p[1]+1): (N_p[1]+N_p[2])] <- rgamma(N_p[2], theta_p[2], phi_p[2])
y[(N_p[1]+N_p[2]+1): sum(N_p)] <- rgamma(N_p[3], theta_p[3], phi_p[3])



Data = data.frame(y = y, source = as.factor(c(rep(1,25), rep(2,25), rep(3,50))))



ggplot(Data, aes(x=y, color = source))+
  geom_histogram(aes(y=..count../sum(..count..)),fill="white", position="dodge", binwidth = 0.5) +
  theme(legend.position="top")+labs(title="Samples against Theoretical Dist",y="Frequency", x="Sample Value")

length(which(y[1:25]<=0.5))/length(y)
length(which(y[1:25]<=0.5))/length(y[0:25])

现在,我想要的是第一个红色直方图条的高度等于长度(which(y[1:25]

但是,我的身高大约为 0.12,与这两个值都不匹配,让我认为我完全误解了 ..count.. 和 sum(..count..)。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    问题不在于您对..count.. 的理解,而在于您对binwidth 工作原理的假设。您假设将其设置为 0.5 会将中断设置为 0、0.5、1、1.5 等,但实际上它将其设置为数据范围的最低值。所以事实上,你的第一个柱的高度是length(which(y[1:25] &lt;= (min(y) + 0.5)))/length(y),也就是13。

    您可以在geom_histogram 中指定breaks 来解决此限制:

    ggplot(Data, aes(x = y, color = source)) +
      geom_histogram(aes(y = stat(count)/length(y)), fill = "white", 
                     position = "dodge", breaks = seq(0, 6, 0.5)) +
      theme(legend.position = "top" +
      labs(title = "Samples against Theoretical Dist",
           y = "Frequency", x = "Sample Value")
    

    现在每个条形都是计数的 1/100,因为向量有 100 长。

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 2015-10-11
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      相关资源
      最近更新 更多