在直方图中添加曲线?

时间:2012-11-16 04:45:14

标签: r plot histogram

我想添加一条曲线,它的x和y坐标已经定义为直方图。考虑下面的直方图:

set.seed(38593)
expRandom <- rexp(10000)
x <- seq(from = 0.05, to = 10, by = 0.001)
y <- exp(-x)
### Now I'd like to first draw my histogram and then 
### add my plot(x,y) to my existing histogram:
hist(expRandom, freq = FALSE)

### ?? How to add my plot(x,y) to my histogram above?

谢谢,

1 个答案:

答案 0 :(得分:4)

设置freq = FALSE以绘制直方图中的密度而不是频率,并使用points(或lines)来添加数据。

hist(expRandom, freq = FALSE)
points(x,y)

您还可以使用y添加曲线来避免预先计算curve

hist(expRandom, freq = FALSE)
curve(dexp, from = 0, to  = 10, add = TRUE)

enter image description here