0% found this document useful (0 votes)
43 views3 pages

Plots in R

The document discusses plotting in R. It describes high-level and low-level plotting functions, with high-level functions creating complete plots and low-level adding to existing plots. Some common high-level functions are plot(), hist(), barplot(), and boxplot(). Low-level functions like lines(), points(), and text() add elements to existing plots. The par() command controls plot layouts, and mfrow() is used to define multi-panel figures. The document provides examples of creating and customizing various types of plots in R.

Uploaded by

smh1678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views3 pages

Plots in R

The document discusses plotting in R. It describes high-level and low-level plotting functions, with high-level functions creating complete plots and low-level adding to existing plots. Some common high-level functions are plot(), hist(), barplot(), and boxplot(). Low-level functions like lines(), points(), and text() add elements to existing plots. The par() command controls plot layouts, and mfrow() is used to define multi-panel figures. The document provides examples of creating and customizing various types of plots in R.

Uploaded by

smh1678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

R Tutorial on plotting in R Plots in R

Plots in R
There are three basic plotting functions in R: high-level plots, low-level plots, and the layout command
par. Basically, a high-level plot function creates a complete plot and a low-level plot function adds to an
existing plot, that is, one created by a high-level plot command.
High-Level Plot Functions
Some of the basic plot functions include:
Function Description
plot scatter/line plot
hist histogram
barplot barplot
boxplot boxplot
qqnorm normal-quantile
Read in the example data set States03.
> States03 <- read.csv("States03.csv")
> barplot(table(States03$Region))
> hist(States03$Poverty)
High-level functions may also take optional arguments that enhance the plot.
> hist(States03$Poverty, main = "Poverty", xlab = "percent",
xlim = c(0, 24), ylim = c(0, 20))
> plot(1:19, 1:19, pch = 1:19, xlab = "x", ylab = "y")
> pie(rep(1, 8), col = 1:8)
Option Description
pch point character (pch=1, 2, ...)
lty line type (lty=1, 2, ...)
lwd line thickness (lwd= 1, 2,...)
col color (col=red, blue,...)
xlim x-axis limits: xlim=c(min,max)
ylim y-axis limits
xlab x-axis label: xlab=my label
ylab y-axis label
main main title
sub sub title
1
R Tutorial on plotting in R Plots in R
To plot smooth curves, use the curve command. The rst argument must be an expression in terms of x:
> curve(x^2, from = 0, to = 2)
> curve(cos(x), from = 0, to = pi)
> curve(cos(x), from = 0, to = pi, lty = 4, col = "red")
Low-level Plot Functions
Low-level plot functions can be executed only after a high-level plot has been created. For example,
> plot(States03$Unemp, States03$Poverty, xlab = "Unemployment", ylab = "Poverty")
> abline(v = mean(States03$Unemp), lty = 2) #vertical line at mean unemployment rate,
> text(30, 18, "mean unemployment rate") #text at (30, 18)
> title("My plot")
The abline function has several options:
abline(3, 5) adds the straight line y = 3 + 5x
abline(v = 2) adds the vertical line, x = 2
abline(h = 0) adds the horizontal line, y = 0
> plot(States03$ColGrad,States03$Poverty, col = "blue", pch = 19, xlab = "College grad (%)",
ylab = "Poverty (%)")
> points(States03$ColGrad, States03$Uninsured, col = "red", pch = 19)
> mtext("Percent uninsured", side = 4)
> legend("bottomleft", legend = c("Y: Poverty","Y: Uninsured"), col = c("blue","red"),
pch = c(19, 19))
> curve(cos(x), from = 0, to = 2*pi)
> curve(sin(x), add = TRUE, col = "blue", lty = 2)
Function Description
lines add a line plot
points add points
text add text
mtext margin text
abline add a straight line
qqline add line to qqnorm
title add a title
2
R Tutorial on plotting in R Plots in R
The par Command
The par command controls the layout of the graphics device. The option you will use most often will
probably be mfrow (multi-figure, by row), or mfcol. For example, to have a 3x2 layout where the plots
are added by row, set
This setting will exist throughout the life of the graphics device unless you change it back to the default
mfrow=c(1,1).
You can also change the default color, plot character, etc. for the graphs created on the graphics device.
> par(mfrow=c(2, 2)) #2x2 layout
> curve(3*x^2)
> curve(cos(x))
> hist(States03$Population)
> qqnorm(States03$Population)
> qqline(States03$Population)
> par(mfrow = c(1, 1)) #reset to default layout
Misc.
Type colors() at the command line to see the list of colors available to the plotting commands.
You can export to some common le formats (jpg, pdf, ps). With the graph in focus, go to the
menu, in Windows, File > Save As... and save to jpg, pdf, ps, png or bmp. On the Macintosh,
File > Save as to pdf only.
Or, at the command line, for instance
> postscript(file = "MyPlot.eps") #open graphics device
> hist(States03$Births, main = "Number of births") #create graph
> dev.off() #close graphics device
See the help le for postscript, jpeg, png, tiff or pdf.
Modied: June 2011, L. Chihara
3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy