如何用R中的点填充频率直方图的每一列?

时间:2017-06-02 05:05:18

标签: r plot histogram

我想知道如何用R中的点垂直填充频率直方图的每个矩形(即列/条)?

下面的R代码提供了一个示例。为清楚起见,每个直方图列中这些点的数量需要等于该列的频率?

h = hist( x = rnorm(1e3), axes = F, labels = T )

axis(2, at = as.integer(seq(0, max(h$counts), len = 5)), las = 1 )

axis(1, at = seq(min(h$breaks), max(h$breaks), len = length(h$breaks) ) )

enter image description here

1 个答案:

答案 0 :(得分:0)

创建数据框以存储点坐标的快速解决方案:

set.seed(555)
x = rnorm(1e3)
h = hist( x = x, axes = F, labels = T )
point_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))
)
points(point_df$x, point_df$y)