Biggiez - 11 months ago 61

R Question

I need help with this issue.

When I plot out this exponential distribution of this code:

`x<-runif(100)`

y<--log(1-x)

hist(y)

I get this plot:

But when I add the exponential curve on my histogram, the interval for the y-values seems to be divided with 100?

The only difference in the code are these 2 new lines:

`hist(y,prob=TRUE)`

curve(dexp(x, rate = 0.8), col = 2, lty = 2, lwd = 2, add = TRUE)

Im a beginnner to R, so would appreciate if someone could help me out with this issue. I want the second plot to also have the same interval as the first plot.

I think the interval for the first plot is the correcct one, since in the first line of my code we generate 100 values x. So the sum of the y-values of our "bars" should be equal to 100, right? Or am I thinking wrong?

Answer Source

You could draw a blank density histogram on top of the regular one and consequently add the curve:

```
hist(y, border = "black")
par(new = TRUE) # allows us to draw new plot on top of first one
hist(y, probability = TRUE,
axes = FALSE, border = "white",
xlab = "", ylab = "", main = "")
curve(dexp(x, rate = 0.8),
col = 2, lty = 2,
lwd = 2, add = TRUE)
```

Note that the plot looks different because of the randomness in `runif()`

.