Multiple Plots In One Graph In R

Hi there. With R, you are able to have multiple plots in one graph with the use of the gridExtra package. The main reference is http://lightonphiri.org/blog/ggplot2-multiple-plots-in-one-graph-using-gridextra.

Before starting the main R code, the ggplot2 and gridExtra packages need to be loaded into R with the use of library().

Simulating Normal Random Variables Plot

For the first plot, I simulate 10000 standard normal random variables (mean of 0 and variance of 1) in R. Most of the values will lie within +3 standard deviations from the mean of 0. Values outside of 3 standard deviations are extreme cases or outliers.

The results are plotted in ggplot2 in the form of a histogram.

 

 

 

Simulating Exponential Random Variables Plot

In the second plot, I simulate 10000 exponential random variables in R with the rexp() function. I have set the rate of 3.

 

 

 

Multiple Graphs In One With grid.arrange()

From the gridExtra package in R, multiple plots can be put into one graph with the use of the grid.arrange() function. I put both the normal and exponential random variables plot into one with ncol = 2.

 

 

2 thoughts on “Multiple Plots In One Graph In R

  1. Michael Theobald

    It appears as though you have made two scripts, each of which generates a chart. The following code will take two R scripts and combine the image that each creates into a single png, here name testDualPlt.png.

    png(file = “testDualPlt.png”, width = 1536/2, height = 768/2)
    par(mfrow = c(1, 2))
    source(“Rfiles/mkGDPImageL.R”) # generates an image.
    source(“Rfiles/mkGDPImageR.R”) # generates an image.
    dev.off()

    -Michael

    Reply
    1. dkmathstats Post author

      I’ll take a look at this. Thank you.

      Reply

Leave a Reply