• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Analysis Factor

The Analysis Factor

Statistical Consulting, Resources, and Statistics Workshops for Researchers

  • Home
  • About
    • Our Programs
    • Our Team
    • Our Core Values
    • Our Privacy Policy
    • Employment
    • Guest Instructors
  • Membership
    • Statistically Speaking Membership Program
    • Login
  • Workshops
    • Online Workshops
    • Login
  • Consulting
    • Statistical Consulting Services
    • Login
  • Free Webinars
  • Contact
  • Login

R Graphics: Multiple Graphs and par(mfrow=(A,B))

by guest 6 Comments

by David Lillis, Ph.D.

Today we see how to set up multiple graphs on the same page. We use the syntax  par(mfrow=(A,B))

. . . where A refers to the number of rows and B to the number of columns (and where each cell will hold a single graph). This syntax sets up a plotting environment of A rows and B columns.

First we create four vectors, all of the same length.

X <- c(1, 2, 3, 4, 5, 6, 7)

Y1 <- c(2, 4, 5, 7, 12, 14, 16)

Y2 <- c(3, 6, 7, 8, 9, 11, 12)

Y3 <- c(1, 7, 3, 2, 2, 7, 9)

Now we set up a plotting environment of two rows and three columns (in order to hold six graphs), using par(mfrow())

par(mfrow=c(2,3))

Now we plot six graphs on the same plotting environment. We use the plot() command six times in succession, each time graphing one of the Y vectors against the X vector.

plot(X,Y1, pch = 1)

plot(X,Y2, pch = 2)

plot(X,Y3, pch = 3)

plot(X,Y1, pch = 4)

plot(X,Y2, pch = 15)

plot(X,Y3, pch = 16)

Out plot looks like this:
image001
To see more of the R is Not So Hard! tutorial series, visit our R Resource page.

About the Author: David Lillis has taught R to many researchers and statisticians. His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. David holds a doctorate in applied statistics.

Bookmark and Share

Tagged With: graphing, graphs, plotting, R

Related Posts

  • R is Not So Hard! A Tutorial, Part 22: Creating and Customizing Scatter Plots
  • R Graphics: Plotting in Color with qplot Part 2
  • Doing Scatterplots in R
  • R Is Not So Hard! A Tutorial, Part 7: More Plotting in R

Reader Interactions

Comments

  1. Shailaja Chadha says

    October 29, 2019 at 12:44 pm

    Hello,
    Is there a way to do this for a very large data set so that you don’t have to type in
    plot(X,Y3, pch = 16)…. and so on.

    I want a way to generate scatter plots for all columns (y=column heading) against the treatments (x=treatments).

    Thanks so much.

    Reply
  2. Mohammad says

    January 31, 2018 at 11:19 pm

    Thanks.
    Is there any way we can change the margin of the plots? i mean They all have same border and margins atm, I want one of my plots to have less space.

    Reply
  3. Kul says

    January 5, 2017 at 3:48 pm

    Is there a way to write a name for all plots? E.g. in your example how could one write “Two rows of Y plots” on top of the plot/file?

    Reply
    • Mohammad says

      January 31, 2018 at 11:18 pm

      You can add
      main = “your proposed title”
      within your plot command
      e.g.:
      plot(X,Y, main=”title”)

      Reply
  4. Sam Jake says

    March 2, 2016 at 6:40 am

    Thank you. Is there a way, where multiple graphs can be plotted in the same x & y axis?

    Reply
    • Cindy says

      September 16, 2016 at 6:56 am

      You want

      par(new=F)

      This will not erase the existing plot before drawing the new one. Note however that it does not attempt to scale the overlaid plot to the existing one – you can either use different y-axes on either side of the plot, or scale the overlaid plot yourself. Otherwise your new plot will be the right shape, but appear to have the wrong y-values.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Please note that, due to the large number of comments submitted, any questions on problems related to a personal study/project will not be answered. We suggest joining Statistically Speaking, where you have access to a private forum and more resources 24/7.

Primary Sidebar

This Month’s Statistically Speaking Live Training

  • January Member Training: A Gentle Introduction To Random Slopes In Multilevel Models

Upcoming Workshops

  • Logistic Regression for Binary, Ordinal, and Multinomial Outcomes (May 2021)
  • Introduction to Generalized Linear Mixed Models (May 2021)

Read Our Book



Data Analysis with SPSS
(4th Edition)

by Stephen Sweet and
Karen Grace-Martin

Statistical Resources by Topic

  • Fundamental Statistics
  • Effect Size Statistics, Power, and Sample Size Calculations
  • Analysis of Variance and Covariance
  • Linear Regression
  • Complex Surveys & Sampling
  • Count Regression Models
  • Logistic Regression
  • Missing Data
  • Mixed and Multilevel Models
  • Principal Component Analysis and Factor Analysis
  • Structural Equation Modeling
  • Survival Analysis and Event History Analysis
  • Data Analysis Practice and Skills
  • R
  • SPSS
  • Stata

Copyright © 2008–2021 The Analysis Factor, LLC. All rights reserved.
877-272-8096   Contact Us

The Analysis Factor uses cookies to ensure that we give you the best experience of our website. If you continue we assume that you consent to receive cookies on all websites from The Analysis Factor.
Continue Privacy Policy
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Non-necessary

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.