【问题标题】:Histogram binwidth (in)consistency between base graphics and ggplot2基本图形和ggplot2之间的直方图binwidth(in)一致性
【发布时间】:2017-02-16 05:01:27
【问题描述】:

我遇到了以下场景...

以下代码生成 2 个相同的直方图:

library(ggplot2)
data("diamonds")    
ggplot(diamonds, aes(x=price)) + geom_histogram(binwidth=1000)
qplot(price, data = diamonds, binwidth = 1000)

但是,我不明白他们如何推导出左边的第一个条形图/bin,事实上,整个直方图对我来说似乎是错误的。

另一方面:

hist(diamonds$price,breaks = seq(0,20000, by=1000))

生成以下对我来说似乎正确的图表:

为了验证我运行此代码的数据:

br = seq(0,20000,by=1000)

ranges = paste(head(br,-1), br[-1], sep=" - ")
freq = hist(diamonds$price, breaks = br, include.lowest=TRUE, plot=FALSE)

data.frame(range = ranges, frequency = freq$counts)

它会产生:

           range frequency
1       0 - 1000     14524
2    1000 - 2000      9683
3    2000 - 3000      6129
4    3000 - 4000      4225
5    4000 - 5000      4665
...

那么ggplotqplot 中的第一个bar/bin 来自哪里?

【问题讨论】:

    标签: r ggplot2 histogram


    【解决方案1】:

    ggplot 直方图条以 0、1000、2000 等为中心,而基本直方图条以 500、1500、2500 等为中心。要检查这一点,请将每个直方图中的条数与表计数进行比较,其中我们明确设置了休息时间。

    table(cut(diamonds$price, breaks=seq(-500,20000,1000)))
    
    (-500,500]       (500,1.5e+03]   (1.5e+03,2.5e+03]   (2.5e+03,3.5e+03]   (3.5e+03,4.5e+03] 
          1749               18261                7532                4958                4535
    etc.
    
    theme_set(theme_classic())
    ggplot(diamonds, aes(x=price)) + 
      geom_histogram(binwidth=1000, fill="grey80", colour="black", lwd=0.2) +
      stat_bin(binwidth=1000, geom="text", aes(label=..count..),
               position=position_stack(vjust=0.5), size=3)
    

    对于基础直方图:

    freq = hist(diamonds$price)$counts
    tab = unname(table(cut(diamonds$price, breaks=seq(0,19000,1000))))
    
    cbind(freq, tab)
    
           freq   tab
     [1,] 14524 14524
     [2,]  9683  9683
     [3,]  6129  6129
     [4,]  4225  4225
     [5,]  4665  4665
     [6,]  3163  3163
     [7,]  2278  2278
     [8,]  1668  1668
     [9,]  1307  1307
    [10,]  1076  1076
    [11,]   934   934
    [12,]   825   825
    [13,]   701   701
    [14,]   603   603
    [15,]   504   504
    [16,]   513   513
    [17,]   425   425
    [18,]   405   405
    [19,]   312   312
    

    要在 ggplot 直方图中获得相同的中断,除了 binwidth 之外,您还可以使用 center 参数:

    ggplot(diamonds, aes(x=price)) + 
      geom_histogram(binwidth=1000, center=500, fill="grey80", colour="black", lwd=0.2) +
      stat_bin(binwidth=1000, center=500, geom="text", aes(label=..count..), 
               position=position_stack(vjust=0.5), size=3)
    

    【讨论】:

    • 谢谢@eipi10。但是,ggplot 似乎并不总是以第一个条形图为中心。如果我这样做:ggplot(diamonds, aes(x=price)) + geom_histogram(binwidth=500),绘图从 0 开始(中心在 250)。这似乎不一致,这背后有什么原因吗?
    • 当我设置 binwidth=500 时,第一个 bin 以 500 为中心,从 250 变为 750。我不确定 ggplot bin-setting 算法的工作原理,但它似乎试图如果在以零为中心的 bin 中至少有一个值,则将 bin 居中于零(给定用户选择的任何 binwidth 或默认 binwidth(根据默认的 30 个 bin 计算),否则)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多