Search code examples
rplotpanelradix

Side by Side Plotting Instead of Panel Plotting on R Base System


Sorry if this question was asked before but I couldn't find a similar post. I am trying to create a panel plot on R's base system. But R Studio shows my plots side by side not on a panel. So I tried to copy some codes from internet and it is still showing charts side by side. For example this codes has to create a panel plot right?

library(datasets)
par(mfrow = c(1, 3), mar = c(4, 4, 2, 1), oma = c(0, 0, 2, 0))
with(airquality, {
   plot(Wind, Ozone, main = "Ozone and Wind")
   plot(Solar.R, Ozone, main = "Ozone and Solar Radiation")
   plot(Temp, Ozone, main = "Ozone and Temperature")
   mtext("Ozone and Weather in New York City", outer = TRUE)
})

But stupid R studio is creating plots side by side. Here is a screenshot: enter image description here

Probably it will be ok if I restart R session but I don't want to loose my loaded variables. Any guesses for this issue? By the way I created some plots using lattice and ggplot2 on same session (I don't know if it effects each other.)


Solution

  • If you run your code in the console, you will get a panel plot. Run this code in console:

    library(datasets)
    par(mfrow = c(1, 3), mar = c(4, 4, 2, 1), oma = c(0, 0, 2, 0))
    with(airquality, {
      plot(Wind, Ozone, main = "Ozone and Wind")
      plot(Solar.R, Ozone, main = "Ozone and Solar Radiation")
      plot(Temp, Ozone, main = "Ozone and Temperature")
      mtext("Ozone and Weather in New York City", outer = TRUE)
    })
    

    Output:

    enter image description here

    If you run this in Rmarkdown, you will also get a panel plot. Here is the code:

    output: pdf_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ```{r}
    library(datasets)
    par(mfrow = c(1, 3), mar = c(4, 4, 2, 1), oma = c(0, 0, 2, 0))
    with(airquality, {
      plot(Wind, Ozone, main = "Ozone and Wind")
      plot(Solar.R, Ozone, main = "Ozone and Solar Radiation")
      plot(Temp, Ozone, main = "Ozone and Temperature")
      mtext("Ozone and Weather in New York City", outer = TRUE)
    })
    ```
    

    Output in pdf:

    enter image description here