在 R 中的直方图上绘制累积频率曲线

时间:2021-03-04 08:29:05

标签: r plot histogram axis curve

我想在直方图上绘制累积频率曲线,虽然我设法正确绘制了两个图,但我无法使第二个轴以正确的比例(累积频率百分比)工作。这就是我所做的,但我在第二个 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)

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解这个问题,如果它要求一个直方图密度图,或者它是否要求一个带有累积相对频率线的频率直方图。

首先是一些数据,因为问题中没有。

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

1.密度和频率图

接下来的两个图分别是密度图(左侧)和频率图(右侧),分别带有累积密度线和频率线。
注意两个图上的图形参数 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)

enter image description here

2.带有相对频率线的频率图。

现在是叠加了累积相对频率线的频率图,其第二个轴位于图的右侧。

第二个轴的位置是累积的频率,但它的标签是累积的相对频率(密度)。

注意 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))
)

enter image description here