Search code examples
rplotlayoutpar

How to put a combined plot in a single panel


The problem is that I managed to graph this:

enter image description here

by writing this:

par(mar=c(0,4,2,1)+.1)

shap <- shapiro.test(as.numeric(residuals.arima))$p.value
qqnorm(residuals.arima, main=c("Normal Q-Q Plot", paste("Shapiro p=", prettyNum(shap, digits = 2))))
qqline(residuals.arima)

op <- par(fig=c(.02,.5,.5,.98), new=TRUE)
hist(residuals.arima, breaks=22, probability=T,
 col="grey", xlab="", ylab="", main="", axes=F)
lines(a,dnorm(a,mean(residuals.arima), sd(residuals.arima)), lty=1, col="darkblue", lwd=2)
box()
par(op)

Now, this is exactly the way I'd like the two plots to be visualized together. I do not want to split them up.

However I'd like to put everything in the right panel (2) of a structure like the following, so that I can add another plot on panel (1) without everything messing up:

layout(matrix(c(1,2), nr=1, byrow = TRUE))

enter image description here

How can I do this?


Solution

  • If I understand you correctly you want to simply do this:

    par(mfrow=c(1,2))
    qqplot(...)
    qqline(...)
    
    hist(...)
    lines(...)
    

    Alternative interpretation of what you are after is to put everything you have on that second side and leave the first side blank (to be used for something else). If that is the case you can use screen:

    figs <- rbind(c(0, 0.5, 0, 1),    # Screen1
                  c(0.5, 1, 0, 1),    # Screen2
                  )
    colnames(figs) <- c("W", "E", "S", "N")
    rownames(figs) <- c("Screen1", "Screen2")
    
    screenIDs <- split.screen(figs)
    names(screenIDs) <- rownames(figs)
    
    screen(screenIDs["Screen1"])
    
    # Everything that should go on the left side goes here
    
    screen(screenIDs["Screen2"])
    
    # The current plots you have go here