【问题标题】:Adding a curve to a histogram?在直方图中添加曲线?
【发布时间】:2012-11-16 04:45:13
【问题描述】:

我想添加一条曲线,它的 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?

谢谢,

【问题讨论】:

标签: r plot histogram


【解决方案1】:

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

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

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

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

【讨论】:

  • 感谢您的回答@mnel。
猜你喜欢
  • 2021-02-04
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 2021-03-14
相关资源
最近更新 更多