0

I'd like to make an object that has a QQ chart

This is my code

    qqnorm(titanic$age) 
    qqline(titanic$age)

In ggplot, I can layer geoms on top of each other, so they can be in one object

What's the equivalent for this case?

Cauder
  • 2,157
  • 4
  • 30
  • 69
  • `qqnorm` and `qqplot` using base graphics (look at the [source](https://github.com/wch/r-source/blob/trunk/src/library/stats/R/qqnorm.R) and see that it calls `plot`), so anything that "layers" (adds components to) base graphics will work here. (For instance, you can add `abline(h=mean(titanic$age),col="red")` to this.) You say you want *"in one object"*, but this code currently *is* in one object. What are you trying to do? – r2evans Oct 23 '20 at 14:23
  • Your title of *"assign the output to an object"* suggests [`recordPlot`](https://stat.ethz.ch/R-manual/R-patched/library/grDevices/html/recordplot.html), is that what you mean? – r2evans Oct 23 '20 at 14:25
  • Base plots can't really be assigned. They're just drawn one layer at a time as you call commands that draw. – Gregor Thomas Oct 23 '20 at 14:48
  • You can, however, draw qq plots with ggplot. [See this question](https://stackoverflow.com/q/42678858/903061), or apparently the [qqplotr package](https://cran.r-project.org/web/packages/qqplotr/vignettes/introduction.html) is built for this. – Gregor Thomas Oct 23 '20 at 14:51

1 Answers1

2

Here's some code as an example. I had to use a different dataset, as the "Titanic" dataset's age column is non-numeric:

data("AirPassengers")

qqnorm(AirPassengers) 
qqline(AirPassengers)
lines(x = 1:length(AirPassengers), rep(300, 144))

p <- recordPlot()
p

edit: to disable the plot:

dev.control('inhibit')
plot(rnorm(10))
p <- recordPlot()
dev.off()

in a loop:

for(i in 1:10){
  # dev.control('inhibit')
  plot(rnorm(10))
  p <- recordPlot()
  # dev.off()
  l_plots[[i]] <- p
}

Somehow it seems difficult to combine the approaches. How about you just delete the plots in the plotting window after creating them?

tester
  • 1,662
  • 1
  • 10
  • 16
  • Can I do this without showing the chart on qqnorm? I want to have this in a function without showing it – Cauder Oct 24 '20 at 02:10
  • Yes, you can disable the device, then the plot will not show up in the window. I'll edit the answer. – tester Oct 24 '20 at 08:53