【问题标题】:R hist vs geom_hist break pointsR hist vs geom_hist 断点
【发布时间】:2015-08-19 18:07:14
【问题描述】:

我在 R 中同时使用 geom_hist 和 histogram 并具有相同的断点,但我得到不同的图表。我做了一个快速搜索,有人知道定义中断是什么以及为什么它们会有所不同

这会产生两个不同的图。

set.seed(25)
data <- data.frame(Mos=rnorm(500, mean = 25, sd = 8))
data$Mos<-round(data$Mos)    

pAge <- ggplot(data, aes(x=Mos))
pAge + geom_histogram(breaks=seq(0, 50, by = 2))

hist(data$Mos,breaks=seq(0, 50, by = 2))

谢谢

【问题讨论】:

标签: r graph ggplot2


【解决方案1】:

要在ggplot2 中获得相同的直方图,请在scale_x_continuous 中指定breaks,在geom_histogram 中指定binwidth

此外,histggplot2 中的直方图使用不同的默认值来创建间隔:

hist:右闭(左开)区间。默认值:right = TRUE

stat_bin (ggplot2):左闭(右开)区间。默认值:right = FALSE

        **hist**    **ggplot2**
         freq1 Freq   freq2 Freq
    1    (0,2]    0   [0,2)    0
    2    (2,4]    2   [2,4)    2
    3    (4,6]    2   [4,6)    1
    4    (6,8]    1   [6,8)    2
    5   (8,10]    6  [8,10)    2
    6  (10,12]    9 [10,12)    7
    7  (12,14]   24 [12,14)   17
    8  (14,16]   27 [14,16)   26
    9  (16,18]   39 [16,18)   31
    10 (18,20]   48 [18,20)   46
    11 (20,22]   52 [20,22)   43
    12 (22,24]   38 [22,24)   57
    13 (24,26]   44 [24,26)   36
    14 (26,28]   46 [26,28)   52
    15 (28,30]   39 [28,30)   39
    16 (30,32]   31 [30,32)   33
    17 (32,34]   30 [32,34)   26
    18 (34,36]   24 [34,36)   29
    19 (36,38]   18 [36,38)   27
    20 (38,40]    9 [38,40)   12
    21 (40,42]    5 [40,42)    6
    22 (42,44]    4 [42,44)    0
    23 (44,46]    1 [44,46)    5
    24 (46,48]    1 [46,48)    0
    25 (48,50]    0 [48,50)    1

我包含了参数right = FALSE,因此直方图间隔是左闭(右开)的,就像它们在ggplot2 中一样。我在两个图中都添加了标签,因此更容易检查间隔是否相同。

ggplot(data, aes(x = Mos))+
  geom_histogram(binwidth = 2, colour = "black", fill = "white")+
  scale_x_continuous(breaks = seq(0, 50, by = 2))+
  stat_bin(binwidth = 2, aes(label=..count..), vjust=-0.5, geom = "text")

hist(data$Mos,breaks=seq(0, 50, by = 2), labels =TRUE, right =FALSE)

检查每个 bin 中的频率:

freq <- cut(data$Mos, breaks = seq(0, 50, by = 2), dig.lab = 4, right = FALSE) 
as.data.frame(table(frecuencias))

【讨论】:

  • 嗨@megv,如果这个或任何答案已经解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 1970-01-01
  • 2014-07-17
  • 1970-01-01
  • 2020-12-06
相关资源
最近更新 更多