R:直方图,箱数 = 类 的数量

R: histogram plot, number of bins = number of classes

奇怪这个问题好像没有问题。至少我还没有找到任何准确的答案。

假设简单的情况是掷两个骰子并添加所示的点数。可能的结果范围从 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))

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

 barplot(table(Dice))