2017-06-02 54 views
0

我想知道如何垂直填充頻率直方圖的每個矩形(即列/欄),並用R中的點來填充?如何用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

回答

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)