簡體   English   中英

如何用R中的點填充頻率直方圖的每一列?

[英]How to fill up each column of a Frequency histogram with points in 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) ) )

在此處輸入圖片說明

快速解決方案,創建一個數據框來存儲點坐標:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM