How do I add a straight line to a qq plot?


I’ve created a set of values using a gamma distribution and I’m trying to plot a qq plot for the data. I would like to have a straight line against the qq plot for comparison but can’t figure out how to add this to the qq plot. This is the code I am using at the moment:

Z <- rgamma(1000, shape = 0.1, scale = 10)
params <- as.list(fitdistr(Z, “gamma”)$estimate)

qplot(sample = Z, geom = ‘blank’) +
stat_qq(distribution = qgamma, dparams = params)


you can use stat_qq_line() like this:


Z <- rgamma(1000, shape = 0.1, scale = 10) 
params <- as.list(MASS::fitdistr(Z, "gamma")$estimate)

qplot(sample = Z, geom = "blank") +
  stat_qq(distribution = qgamma, dparams = params) +
  stat_qq_line(distribution = qgamma, dparams = params)


Hi, I tried running your code but get the following error after the second line:
In densfun(x, parm[1], parm[2], …) : NaNs produced

and this error after the last line:
Error in match(x, table, nomatch = 0L) :
‘match’ requires vector arguments


Are you trying to use the fitdistr function from the MASS package? I assumed you were. The code runs on my machine. Can you provide a reprex of the code that generated the error?


Hi, when I try to install the development version it fails to install, something to do with R being confused by E:/UNI PROGRAMS and not understanding the space? It’s just the name of a folder but it looks like it loses the PROGRAMS part?

And then when using reprex() I highlighted and copied the relevant code but reprex() said no input and no access to clipboard.
I took a picture here as I can’t really think of a better way to show you sorry.

The part of your code I am running looks like this with error messages shown at the bottom.

I should say that in order for qplot() to work I had to install qqplotr as you can see as it didn’t find it just from using library(tidyverse)

Sorry this is all a bit of a mess but I’m not sure what the issue with reprex is.


The current version of reprex on CRAN has some issues.

Until CRAN catches up with the latest version install reprex with