# graph – How to plot exponential function on barplot R?

## graph – How to plot exponential function on barplot R?

If youre trying to fit density of an exponential function, you should probably plot density histogram (not frequency). See this question on how to plot distributions in R.

This is how I would do it.

``````x.gen <- rexp(1000, rate = 3)
hist(x.gen, prob = TRUE)

library(MASS)
x.est <- fitdistr(x.gen, exponential)\$estimate

curve(dexp(x, rate = x.est), add = TRUE, col = red, lwd = 2)
`````` One way of visually inspecting if two distributions are the same is with a Quantile-Quantile plot, or Q-Q plot for short. Typically this is done when inspecting if a distribution follows standard normal.

The basic idea is to plot your data, against some theoretical quantiles, and if it matches that distribution, you will see a straight line. For example:

``````x <- qnorm(seq(0,1,l=1002))  # Theoretical normal quantiles
x <- x[-c(1, length(x))]  # Drop ends because they are -Inf and Inf
y <- rnorm(1000)  # Actual data. 1000 points drawn from a normal distribution
l.1 <- lm(sort(y)~sort(x))
qqplot(x, y, xlab=Theoretical Quantiles, ylab=Actual Quantiles)
abline(coef(l.1), coef(l.1))
`````` Under perfect conditions you should see a straight line when plotting the theoretical quantiles against your data. So you can do the same plotting your data against the exponential function you think it will follow.