簡體   English   中英

在 R 的直方圖上繪制累積頻率曲線

[英]plotting a cumulative frequency curve on a histogram in R

我想 plot 在直方圖上繪制累積頻率曲線,雖然我設法正確繪制了這兩個圖,但我無法使第二個軸以正確的比例(累積頻率百分比)工作。 這就是我所做的,但我在第二個 y 軸上的范圍為 0 到 600。你能幫我解決這個問題嗎? 我究竟做錯了什么?

hist(all_final_braking_events$Mean)->h
plot(h, col="red")
par(new=TRUE)
all_final_braking_events$Mean->y
plot(sort(y), 1:length(y), xaxt="n", yaxt="n", ann=FALSE)
axis(side=4, ylim=c(0,100), las=1)

我不確定我是否理解這個問題,如果它要求直方圖密度 plot 或者它是否要求具有累積相對頻率線的頻率直方圖。

首先是一些數據,因為問題中沒有數據。

set.seed(2021)    # Make the results reproducible
n <- 1e4
x <- rgamma(n, shape = 10, scale = 0.5)

1. 密度和頻率圖

接下來的兩個圖是左側的密度和右側的頻率 plot,分別具有累積密度和頻率線。
注意兩個圖上的圖形參數ylim設置。

old_par <- par(mfrow = c(1, 2))

h <- hist(x, freq = FALSE, col = "red", ylim = c(0, 1))
lines(h$mids, cumsum(h$density))

h <- hist(x, plot = FALSE)
plot(h, col = "red", ylim = c(0, sum(h$counts)))
lines(h$mids, cumsum(h$counts))

par(old_par)

在此處輸入圖像描述

2. 頻率 plot 與相對頻率線。

現在是頻率的 plot 疊加了累積相對頻率線,其第二軸位於 plot 的右側。

第二個軸位置是累積頻率,但其標簽是累積相對頻率(密度)。

注意ylim圖形參數設置為頻率的總和。

h <- hist(x, plot = FALSE)
plot(h, col = "red", ylim = c(0, sum(h$counts)))
lines(h$mids, cumsum(h$counts))
axis(
  side = 4,
  at = pretty(cumsum(h$counts)),
  labels = pretty(cumsum(h$density))
)

在此處輸入圖像描述

暫無
暫無

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

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