• 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 7: More Plotting in R

by guest 1 Comment

by David Lillis, Ph.D.

In Part 7, let’s look at further plotting in R. Try entering the following three commands together (the semi-colon allows you to place several commands on the same line).

Let’s take an example with two variables and enhance it.

X <- c(3, 4, 6, 6, 7, 8, 9, 12)
B1 <- c(4, 5, 6, 7, 17, 18, 19, 22)
B2 <- c(3, 5, 8, 10, 19, 21, 22, 26)

Graph B1 using a y axis from 0 to 25.

plot(X, B1, type="o", pch = 17, cex=1.2, col="darkgreen", ylim=c(0, 25))

(Note: when a larger version of a graphic is available, you can click on the graphics below to see the larger version.)

Now superpose a plot of B2.

lines(B2, type="o", pch=16, lty=2, col="blue")

Notice how we plotted the first curve and then added the second using the lines command. Let’s create a title using the title command.

title(main="MY FIRST R PLOT", col.main="blue", font.main=2)

IntroToR7_003

You can look up the various arguments for colour and font type on-line. Note the default labels for the horizontal and vertical axes. However, notice that the second curve did not fit into the plot.

Here is another example, in which we automate the calculation of the y-axis limits. Let’s define three vectors.

B1 <- c(1, 2, 4, 5, 7, 12, 14, 16, 19)
B2 <- c(2, 3, 6, 7, 8, 9, 11, 12, 18)
B3 <- c(0, 1, 7, 3, 2, 2, 7, 9, 13)

Calculate the maximum value of B1, B2 and B3.

yaxismax<- max(B1, B2, B3)
yaxismax
[1] 19

We plot on a vertical axis from 0 to yaxismax. Then we disable the default axes and their annotations, using the arguments axes = FALSE, ann=FALSE, so that we can create our own axes. This approach (disabling the default axes) is very important in plotting with R.

plot(B1, pch = 15, type="o", col="blue", ylim=c(0, yaxismax),axes=FALSE, ann=FALSE)
axis(1, at=1:9, lab=c("A","B","C","D","E", "F", "G","H", "I"))

The first argument in the axis command (1) specifies the horizontal axis. The at argument specifies where to place the axis labels. The vector called lab stores the actual labels. Now create a y axis with horizontal labels that display ticks every 2 marks.

axis(2, las=1, at=2*0: yaxismax)

The argument las controls the orientation of the axis labels. Try entering other values. Now create a
box around the plot. Then add in the two new curves using the lines command.

box()
lines(B2, pch = 16, type="o", lty=2, col="red")
lines(B3, pch = 17, type="o", lty=3, col="darkgreen")

Create a title.

title(main="MY LINE PLOTS IN R", col.main="red", font.main=2)

Label the x and y axes.

title(xlab=toupper("ALPHABET"), col.lab="purple")
title(ylab="NUMERALS", col.lab="brown")

Note the toupper command which ensures that text is upper case. The tolower command ensures that text is in lower case.

Create a legend at the location (1, yaxismax).

legend(1, yaxismax, c(“B1″,”B2”, “B3”), cex=0.7, col=c(“blue”, “red”, “darkgreen”),
pch=c(15, 16, 17), lty=1:3)

The cex argument gives you control over the size of fonts, where the default is 1. You can check out the parameters pch, type and lty for yourselves. You can find these arguments easily on Google by entering R pch, for example. Here is your plot:

That wasn’t so hard! However, you did have to know some syntax and be ready to experiment. In blog 8 we will look at some analytic 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.


Bookmark and Share

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

Tagged With: plotting, R, Statistical Software

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 Graphics: Multiple Graphs and par(mfrow=(A,B))

Reader Interactions

Comments

  1. Amul Mary says

    May 21, 2020 at 5:40 am

    please tell me how to draw graph by taking an Excel file

    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

  • February Member Training: Choosing the Best Statistical Analysis

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