【问题标题】:R: histogram plot, number of bins = number of classesR:直方图,箱数 = 类数
【发布时间】:2015-07-13 10:24:49
【问题描述】:

我很惊讶这个问题似乎毫无疑问。至少我还没有找到任何一个准确的答案。

假设掷两个骰子并添加所示点数的简单情况。可能的结果范围从 2 到 12。现在我想绘制此事件的直方图,即每个可能的数字一个 bin。这将产生 11 个垃圾箱 (2,3,4,5...12)

# Example dataset: how often did we get "2","3", "4"(1x2, 3x3, 2x4, 4x5, 8x6, 14x7, ...)
Dice <- c(2,rep(3,3),rep(4,2),rep(5,4),rep(6,8),rep(7,14),rep(8,9),rep(9,5),rep(10,4),rep(11,1),rep(12,2))

hist(Dice,breaks=seq(2,12)) # custom breaks return 10 bins (9 breaks)
hist(Dice,breaks=11) # same for automatic breaks (and for breaks=12 or 13...)

我需要的是一个带有 11 个 bin 的直方图 - 每个可能的结果是一个 bin。我怎样才能欺骗 R 这样做?

谢谢!

【问题讨论】:

  • 试试hist(Dice,breaks=seq(1.5,12.5))

标签: r plot histogram


【解决方案1】:
hist(Dice,breaks=seq(1.5,12.5))

【讨论】:

  • 好的,这也解释了 R 是如何排列 bin 的。感谢您的简短回答!
【解决方案2】:

这本身不是直方图,但您可以试试这个:

 barplot(table(Dice))

【讨论】:

  • 这也是个好主意,因为它表明骰子上的点数总和是一个离散变量。
猜你喜欢
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
相关资源
最近更新 更多