也有点晚了,这是另一个解决方案,它用第二个 y 轴扩展了 @Christoph 的解决方案。
par(mar = c(5,5,2,5))
set.seed(15)
dt <- rnorm(500, 50, 10)
h <- hist(
dt,
breaks = seq(0, 100, 1),
xlim = c(0,100))
par(new = T)
ec <- ecdf(dt)
plot(x = h$mids, y=ec(h$mids)*max(h$counts), col = rgb(0,0,0,alpha=0), axes=F, xlab=NA, ylab=NA)
lines(x = h$mids, y=ec(h$mids)*max(h$counts), col ='red')
axis(4, at=seq(from = 0, to = max(h$counts), length.out = 11), labels=seq(0, 1, 0.1), col = 'red', col.axis = 'red')
mtext(side = 4, line = 3, 'Cumulative Density', col = 'red')
诀窍如下:不要在绘图中添加线,而是在顶部绘制另一个绘图,这就是我们需要par(new = T) 的原因。然后您必须稍后添加 y 轴(否则它将绘制在左侧的 y 轴上)。
学分去here(@tim_yates 回答)和there。