4

How to get the four graphs generated (one separated from the other/one at a time) from the regression analysis not by using the console / prompt (Hit <Return> to see next plot:)?

if(!require("truncnorm")) install.packages("truncnorm") ; library(truncnorm)


FACTORA <- c(0,1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0)
FACTORB <- c(0,1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0)
FACTORC <- c(0,1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0)
FACTORD <- c(0,1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0)
FACTORE <- c(0,1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0)

planDOE <- data.frame(FACTORA, 
                  FACTORB, 
                  FACTORC, 
                  FACTORD, 
                  FACTORE, 
                  rtruncnorm(n=32, a=0, mean=61.16, sd=31.32))

rg <- lm(planDOE)
plot(rg)

I tried the following ways and did not succeed (either using true or false in the ask argument commands ask).

plot(rg)
par(ask=TRUE)

#

par(ask=FALSE)
plot(rg)

#

plot(rg, ask = FALSE)

#

par(mfrow=c(2,2))
plot(rg)
# In one image it can put together the graphic information.

#

png('test2.png', units="in", width=13, height=7, res=300)
plot(rg)
dev.off()

In this last way, I realize that it is possible to obtain the graph separately, but only saves the graph of residuals.

The answer:

plot(rg, which = 1)
plot(rg, which = 2)
#...
bbiasi
  • 1,549
  • 2
  • 15
  • 31
  • 3
    See `?plot.lm`. You can do `plot(rg, ask = FALSE)`. – Marius May 22 '18 at 03:47
  • @Marius In this way `plot (rg, ask = FALSE)` continues to appear at the prompt. – bbiasi May 22 '18 at 03:59
  • @bbiasi, my guess is you did not clear your `par(ask=TRUE)` before you tried `plot(rg, ask=FALSE)`. When you do `par(ask=TRUE)`, it will always ask whenever a new figure is drawn; try `par(ask=TRUE);plot(1);plot(2);` to see that it always asks, and this behavior has nothing to do with your question involving `plot.lm`. Reading help pages before you come to StackOverflow might have helped here; in this case: [`?par`](http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/par.html) (search for `"ask"`) and [`?plot.lm`](http://stat.ethz.ch/R-manual/R-devel/library/stats/html/plot.lm.html). – r2evans May 22 '18 at 04:15
  • @r2evans https://i.imgur.com/OIy7xqg.png Can u help me? How 'did not clear'? – bbiasi May 22 '18 at 04:18
  • If you start in a fresh "R" session, `par("ask")` will return `FALSE`, indicating its default. When I said *"did not clear"*, I was suggesting (vaguely) that you were not in the default state, instead still under `par(ask=TRUE)`. The option remains until you close the graphic device which may be easier in R than in RStudio's interface (which generally always has the graphic device directly or indirectly available). – r2evans May 22 '18 at 12:13

2 Answers2

3

set par(mfrow = params). using mtcars, here's an example. You can save the plot as a .png file afterwards

rg <- lm(mpg~cyl + wt, data = mtcars)

par(mfrow=c(2,2))
plot(rg)
  • Thanks for the tip Sebastian, in one image it actually manages to gather the graphical information. https://i.imgur.com/7Sbdyj1.png But do you know how to get each image separately? – bbiasi May 22 '18 at 04:03
1

You can actually see this if you had done ?plot.lm

mod <- lm(1:10 ~ rnorm(10))

Now you can plot each of the plots separately specifying the which argument in plot.lm. As in,

png('plot_1.png', units="in", width=13, height=7, res=300)
 plot(mod, which = 1)
dev.off()

png('plot_2.png', units="in", width=13, height=7, res=300)
 plot(mod, which = 2)
dev.off()

plot(mod, which = 3)
plot(mod, which = 5)

These will plot the figure you'd normally get if you use plot(mod).

Naturally, you can save after each plot, or only the plots you prefer.

NOTE: I didn't use which = 4.

kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42