将直方图条转换为 R 中的符号

converting histogram bars to symbols in R

在我下面的函数中,时不时(请运行多看几次),我从[=11=里面得到一个错误信息] 表示 xy 相差 2 行。

我想知道如何修复这种偶发错误?

 x = rnorm(1e2)
 h = hist(x = x, plot = F)

DF = data.frame(
 x = unlist(sapply(1:length(h$mids), function(i) rep(h$mids[i], each = h$counts[i]))),
 y = unlist(sapply(h$counts, function(c) 1:c)))

plot(DF$x, DF$y)

Error in data.frame(x = unlist(sapply(1:length(h$mids), function(i) rep(h$mids[i], : arguments imply differing number of rows: 100, 102

你得到一些 h$counts 作为 0,当你 运行 unlist(sapply(h$counts, function(c) 1:c))) 它从 1:0 生成一个不需要的序列。您可以修改创建数据框的方式,它应该可以正常工作。

DF1 <- data.frame(x = rep(h$mids, h$counts),y = sequence(h$counts))