• 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
  • Our Programs
    • Membership
    • Online Workshops
    • Free Webinars
    • Consulting Services
  • About
    • Our Team
    • Our Core Values
    • Our Privacy Policy
    • Employment
    • Collaborate with Us
  • Statistical Resources
  • Contact
  • Blog
  • Login

R Is Not So Hard! A Tutorial, Part 6: Basic Plotting in R

by guest contributer Leave a Comment

by David Lillis, Ph.D.

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

x <- seq(-4, 4, 0.2) ;  y <- 2*x^2 + 4*x - 7
plot(x, y)

image001

This is a very basic plot, but we can do much better. Let’s build a nicer plot in several steps.

plot(x, y, pch = 16, col = "red")

image002

The argument pch = 16 gave us solid circles, while col = “red” coloured those circles red.

Now try:

plot(x, y, pch = 16, col = "red", xlim = c(-8, 8), ylim = c(-20, 50),
main = "MY PLOT", xlab = "X VARIABLE" , ylab = "Y VARIABLE")

You can see what the main argument and the xlim and ylim arguments achieved. Now try:

plot(x, y, pch = 16, col = "red", xlim = c(-8, 8), ylim = c(-20, 50),
main = "MY PLOT", xlab = "X VARIABLE" , ylab = "Y VARIABLE")
lines(x, y)

image003

Note that the lines command is used after the plot command. Now enter the following syntax.

abline(h = 0)
abline(v = 0)
abline(-10, 2) # Draws a line of intercept -10 and slope 2.
text(4, 20, "TEXT") # Now your text is located at the point (4, 20) on your plot.

legend(-8,36,"This is my Legend")

Your legend is now centered on the point (-4, 36)

rug(x)

The rug command indicates the location of points on the horizontal axis. Here is the final plot:

image004

That wasn’t so hard! In blog 7 we will look at some more sophisticated 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: plotting, R, Statistical Software, statistics

Related Posts

  • R Is Not So Hard! A Tutorial, Part 7: More Plotting in R
  • Member Training: What’s the Best Statistical Package for You?
  • The Advantages of RStudio
  • What Really Makes R So Hard to Learn?

Reader Interactions

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

  • Member Training: Analyzing Pre-Post Data

Upcoming Free Webinars

Poisson and Negative Binomial Regression Models for Count Data

Upcoming Workshops

  • Analyzing Count Data: Poisson, Negative Binomial, and Other Essential Models (Jul 2022)
  • Introduction to Generalized Linear Mixed Models (Jul 2022)

Copyright © 2008–2022 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