How to plot an exponential distribution

Chubing picture Chubing · Sep 22, 2015 · Viewed 24.8k times · Source

I want to plot an exponential distribution, something like this for example: enter image description here

But I only know how to simulate a data frame that follow a exponential distribution and plot it.

data =  data.frame(x=rexp(n = 100000, rate = .65))
m <- ggplot(data, aes(x=data$x))
m + geom_density()

From which I get: enter image description here

How can I plot the true exponential distribution instead of a sampled version of the distribution?

Answer

josliber picture josliber · Sep 22, 2015

The exponential distribution can be obtained with the dexp function, so you can plot it by sampling x values and processing them with that function:

x <- seq(0, 20, length.out=1000)
dat <- data.frame(x=x, px=dexp(x, rate=0.65))
library(ggplot2)
ggplot(dat, aes(x=x, y=px)) + geom_line()

enter image description here