如何自己设置直方图的 bin 中心值?

how can I set the bin centre values of histogram myself?

假设我有一个如下所示的数据框

mat <- data.frame(matrix(data = rexp(200, rate = 10), nrow = 100, ncol = 10))

然后我可以使用

计算每个列的直方图
matAllCols <- apply(mat, 2, hist)

现在,如果您查看 matAllCols$breaks ,有时会看到 11,有时会看到 12,等等。 我想要的是为它设置一个阈值。例如,它应该始终为 12,并且每个 bin 中心(存储为 matAllCols$mids)之间的距离为 0.01

当时对一列做似乎很简单,但是当我尝试对所有列做时,它不起作用。这也只是休息,如何设置中音也不简单

matAllCols <- apply(mat, 2, function(x) hist(x , breaks = 12))

有没有办法做到这一点?

您正在寻找

set.seed(1)
mat <- data.frame(matrix(data = rexp(200, rate = 10), nrow = 100, ncol = 10))
matAllCols <- apply(mat, 2, function(x) hist(x , breaks = seq(0, 0.5, 0.05)))

或者干脆

x <- rexp(200, rate = 10)
hist(x[x>=0 & x <=0.5] , breaks = seq(0, 0.5, 0.05))

您可以通过将直方图单元格之间的所有断点指定为 breaks 来解决问题。 (但是正如@Colonel Beauvel所说,这是用stat.ethz.ch/R-manual/R-devel/library/graphics/html/hist.html写的)

set.seed(1); mat <- data.frame(matrix(data = rexp(200, rate = 10), nrow = 100, ncol = 10))
# You need to check the data range to decide the breakpoints.
range(mat) # [1] 0.002025041 0.483281274
# You can set the breakpoints manually.
matAllCols <- apply(mat, 2, function(x) hist(x , breaks = seq(0, 0.52, 0.04)))