• 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 Is Not So Hard! A Tutorial, Part 13: Box Plots

by guest 4 Comments

by David Lillis, Ph.D.

In Part 13, let’s see how to create box plots in R. Let’s create a simple box plot using the boxplot() command, which is easy to use. First, we set up a vector of numbers and then we plot them.

Box plots can be created for individual variables or for variables by group. The syntax is boxplot(x, data=), where x is a formula and data denotes the data frame providing the data. An example of a formula is: y~group, where you create a separate box plot for each value of group.

Use varwidth=TRUE to make box plot widths proportional to the square root of the sample sizes. Use horizontal=TRUE to reverse the axis orientation.

The Standard Box Plot does not indicate outliers. The Modified Box Plot highlights outliers. The Modified Box plot is the default in R.

A <- c(3, 2, 5, 6, 4, 8, 1, 2, 3, 2, 4)
boxplot(A)

image001[Click image to see it larger]

Look at the built-in dataset mtcars.

print(mtcars)

Now create a box plot for vehicle weight for each type of car.

boxplot(wt~cyl, data=mtcars, main=toupper("Vehicle Weight"), font.main=3, cex.main=1.2, xlab="Number of Cylinders", ylab="Weight", font.lab=3, col="darkgreen")

image002

Let’s create a notched box plot of miles per gallon for each type of car, with different colours for each box. Box plots help us to to make a visual comparison across levels and check for equality of medians.

boxplot(mpg~cyl, data=mtcars, main= toupper("Fuel Consumption"), font.main=3, cex.main=1.2, col=c("red","blue", "yellow"), xlab="Number of Cylinders", ylab="Miles per Gallon", font.lab=3, notch=TRUE, range = 0)

image003[Click image to see it larger]

The notches do not overlap, so we have evidence for a difference in the medians.

NOTE: The range argument determines how far the plot whiskers extend out from the box. If range is positive, the whiskers extend to the datum that is no more than range times the interquartile range from the box. The argument range = 0 ensures that the whiskers extend to the data extremes. The argument horizontal=TRUE creates horizontal bars.

That wasn’t so hard! In Part 14 we will look at further plotting techniques in R.

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

Getting Started with R
Kim discusses the use of R statistical software for data manipulation, calculation, and graphical display.

Tagged With: box plot, graphics, R, statistics

Related Posts

  • R is Not So Hard! A Tutorial, Part 12: Creating Histograms & Setting Bin Widths
  • R is Not So Hard! A Tutorial, Part 22: Creating and Customizing Scatter Plots
  • R is Not So Hard! A Tutorial, Part 21: Pearson and Spearman Correlation
  • What R Commander Can do in R Without Coding–More Than You Would Think

Reader Interactions

Comments

  1. Tom says

    September 22, 2016 at 4:23 am

    I’ve spent a day and a half trying ot show all the ticks on the y axis of my boxplot. How can this be so hard?

    Reply
  2. Md. Abdul Kader says

    August 28, 2016 at 8:36 am

    Could you tell me, How to set two set data range (scale) in a boxplot.

    Reply
  3. heidi says

    February 17, 2016 at 3:08 pm

    Hi,

    Can you show how to have data labels in each of these plots. I would like to see the quartile values in the plot.

    thanks
    heidi

    Reply
  4. Rafael says

    August 5, 2015 at 2:03 am

    font.main=3 is for an italic type, but thats not visible in the picture. why?

    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

Free Webinars

Binary, Ordinal, and Multinomial Logistic Regression for Categorical Outcomes (Signup)

This Month’s Statistically Speaking Live Training

  • April Member Training: Statistical Contrasts

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.

SAVE & ACCEPT