A Crash R Course On Statistical Graphics
A Crash R Course On Statistical Graphics
on Statistical Graphics
2
Outline - Continued
8. Basic R Graphics
9. Customizing Basic R Graphics
10. Advanced R Graphics
11. Customizing Advanced R Graphics
12. R Graphics Housekeeping
13. Summary
14. References
3
Learning Goals
4
Overarching Learning Goals
After attending this course, you will be able to:
Organize your work in R by creating and saving R
scripts;
Import/export data using R;
Produce standard statistical plots using the R
package graphics;
Produce advanced statistical plots using the R
package lattice;
Customize basic and advanced statistical plots;
Save basic and advanced statistical plots in a
variety of formats (e.g., jpeg, pdf).
5
Overview of R
Learning Goal:
6
Overview of R
R is an open-source software environment and
programming language for statistical computing and
graphics.
R’s use is governed by the GNU general Public License.
R was created in the mid 90’s by Ross Ihaka and Robert
Gentleman (also known as “R & R”) of the Statistics
Department at the University of Auckland, New Zealand.
Some people claim that R was created by academics for
academics. This may explain the steep learning curve
some learners face when switching to R.
7
Overview of R
R gets updated several times a year and each upgrade
includes new functionality. It’s good to keep up with the
latest upgrades by installing the latest version of R.
However, it is also important to keep all previously
installed versions of R, as sometimes old R code will no
longer work with recent versions of R.
You can check the website http://cran.stat.ucla.edu/ for
R upgrades.
R is supported by all major operating systems: Windows,
Mac, Linux and Unix.
R is developed at present by the R Development Core
Team, a group of researchers with write access to the R
8
source code.
Overview of R
R has its own dedicated website:
http://www.r-project.org/
The R website provides access to a variety of
resources, including:
- R Mailing Lists (e.g., R-help)
- R Conferences (e.g., UseR!)
- CRAN (i.e., go to website for installing R)
- Search resources
- R Manuals
- R Books
- R Journal
9
Overview of R
To cite R in publications, use the following:
10
Overview of R
The original R is focused on function
rather than form and its graphical user
interface reflects this focus.
• RStudio (http://www.rstudio.com)
• Revolution R (http://www.revolutionanalytics.com)
11
Overview of R
R offers a powerful and versatile platform for:
See http://www.r-project.org/doc/bib/R-books.html
for additional R book references.
14
Things To Know
About R
Learning Goal:
15
Things to know about R
R is case sensitive!
e.g.: anova is different from Anova
R uses the assignment operator <-
to assign names or create new data objects.
e.g.: m <- 1 + 2
R provides access to help files via the
question mark.
e.g.: ?mean
16
Things to know about R
data frames 2 20 5 1 10
2 20
lists 3 30 17
Data Frame:
1 M 1.5
2 F 1.8
3 M 1.7 18
Things to know about R
• R uses the symbol NA to denote missing
values (i.e., Not Available).
e.g.: 1
NA
3
f <- function(x){
plot(x)
return(summary(x))
}
20
Things to know about R
R uses packages to bundle up functions useful for
performing certain data processing tasks, producing
certain types of graphs or performing specialized
statistical analyses. R packages may also include data
sets and help documentation.
Thousands of R packages are available on CRAN
(Comprehensive R Archive Network) and can be installed
in R with the command:
install.packages("package_name")
Once installed in R, packages need to be
attached to the current R working session:
require(package_name)
For a list of R package available on CRAN, see:
http://cran.r-project.org/web/packages/
21
Things to know about R
For the purpose of creating graphs or
implementing statistical analyses, R uses formulas
such as:
y~x (y as a function of x)
28
Launching R
• If you have an R icon on your desktop, double click
on it to launch R.
Example of
R Icon
29
Taking Stock of R’s Interface
Notes:
R has an R Console
window, where we
can either type
commands directly
or send commands
stored in script files
for execution.
getwd()
35
Data Import
Learning Goal:
36
R Functions for Data Import
R offers a variety of functions for importing data files. Two
of these functions are shown below.
39
read.csv()
The command read.csv() can also be used with
the following arguments:
browse interactively
for the import data file
browse interactively
for the import data file
head(ozone)
Learning Goal:
47
Data Export Notes:
50
Exercise on Data Export
Export the data frame ozone in your working space as a
comma delimited file (.csv). For your convenience, the
R command for data export is given below.
51
Graphical
Systems in R
Learning Goal:
Trellis Graphics
Grid Graphics
53
Graphical Systems in R
Graphical System R Package Book Reference
Base Graphics graphics “Graphics for Statistics and Data Analysis
with R”, by Kevin J. Keen (CRC Press, 2010)
Trellis Graphics lattice “Lattice: Multivariate Data Visualization
with R”, by Deepayan Sarkar (Springer,
2008)
Grammar of Graphics ggplot2 “ggplot2: Elegant Graphics for Data
Analysis”, by Hadley Wickham (Springer-
Verlag, 2009)
Grid Graphics grid “R Graphics”, 2nd Edition, by Paul Murrell
(Chapman & Hall/CRC, 2006)
Note: The graphics packages comes with the default installation of R. The other
packages need to be installed in R one time only and then required for each R session.
install.packages(c("lattice", "ggplot2", "grid"))
require("lattice")
require("ggplot2")
require("grid") 54
Example of Graph Produced in R
2011 BATON ROUGE/CAPITOL
0.14
graphics package
0.12
0.10
Daily Max Ozone (ppm)
0.08
0.06
0.04
0.02
0.00
January February March April May June July August September October November December
Month
55
Example of Graph Produced in R
lattice package
0.10
Daily Max Ozone (ppm)
0.05
0.00
January February March April May June July August September October November December
Month
56
Example of Graph Produced in R
2011 BATON ROUGE/CAPITOL
ggplot2 package
0.10
Daily Max Ozone (ppm)
0.05
0.00
January February March April May June July August September October November December
Month
57
Getting Help on R Graphical Systems
To access the R help files associated with each of the
three graphical systems, type the following commands in
the R Console window:
help(package="graphics")
help(package="lattice")
help(package="ggplot2")
help(package="grid")
58
Getting Help on R Graphical Systems
To access the R help files associated with specific
functions within a particular graphical system package,
use commands similar to the ones below:
help(bwplot, package="lattice")
help(qplot, package="ggplot2")
help(arrowsGrob, package="grid") 59
Basic R Graphics
Learning Goal:
60
Basic R Graphics
R offers a collection of functions for producing
standard graphics that are useful when
conducting exploratory data analysis.
61
Basic R Graphics
Graph Type R Command
Histogram hist(x)
x = quantitative variable
62
Basic R Graphics
Graph Type R Command
Bar chart barplot(table(f))
63
Basic R Graphs
Graph Type R Command
Scatter plot plot(y ~ x)
Time series plot plot(y ~ date)
Coplot coplot(y ~ x|z)
Line Plot matplot(x, cbind(y,z))
Pairs plot pairs(cbind(y,x,z))
Side-by-side boxplots boxplot(y ~ f)
Side-by-side bar charts barplot(table(f1, f2))
x, y, z = quantitative variables
f, f1, f2 = qualitative variables (i.e., factors)
date = time variable
64
Histogram
Histogram
80
?hist
60
Frequency
40
20
0
Air Quality in Baton Rouge in 2011 0.00 0.02 0.04 0.06 0.08 0.10 0.12 0.14
65
R Code for Histogram
air <- read.csv("Air Quality Baton Rouge 2011.csv", as.is=TRUE)
View(air)
str(air)
require(lubridate)
str(air)
names(air)
attach(air)
66
R Code for Histogram
hist(Ozone,
xlab="Daily Max Ozone (ppm)",
main="Histogram",
col="lightblue",
sub="2011 BATON ROUGE/CAPITOL",
col.sub="red")
67
Density Plot
Kernel Density Plot
25
20
?density
15
Density
10
5
0
68
R Code for Density Plot
plot(density(Ozone),
main="Kernel Density Plot")
69
Boxplot
70
R Code for Boxplot
boxplot(Ozone,
ylab="Daily Max Ozone (ppm)",
main="Boxplot",
col="lightblue",
sub="2011 BATON ROUGE/CAPITOL",
col.sub="red")
71
Side-by-Side Boxplots
Side-by-Side Boxplots
0.14
?boxplot
0.12
0.10
Daily Max Ozone (ppm)
0.08
0.06
0.04
0.02
0.00
January February March April May June July August September October November December
Month
2011 BATON ROUGE/CAPITOL
72
R Code for Side-by-Side Boxplots
boxplot(Ozone ~ Month,
xlab="Month",
ylab="Daily Max Ozone (ppm)",
main="Side-by-Side Boxplots",
col="lightblue",
sub="2011 BATON ROUGE/CAPITOL",
col.sub="red")
73
Empirical CDF
plot.ecdf(Ozone,
xlab="Daily Max Ozone (ppm) ",
main="Empirical Cumulative Distribution Function")
1.0
0.8
0.6
Fn(x)
0.4
0.2
?plot.ecdf
0.0
74
Barchart
Percent Change in Hispanic Population from Previous Decade
United States
70
1990 53%
60
58%
53%
50
2000 58% 43%
40
2010 43%
30
20
10
0
Decade
?barplot
75
Barchart
Decade <- c(1990, 2000, 2010)
b <- barplot(Pct.Change.Hispanic.US,
col=c("#599ad3"),
xlab="Decade",
ylab="Percent Change in Hispanic Population from Previous Decade",
ylim=c(0,70),
main="United States")
abline(h=0)
text(b, Pct.Change.Hispanic.US + 2,
paste(Pct.Change.Hispanic.US,"%",sep=""))
76
Side-by-Side Barcharts
Percent Change in Hispanic Population from Previous Decade
70
United States
New Orleans Metro
60 58% 57%
53%
50
43%
40
30
20
9%
10
6%
?barplot
0
Pct.Change.Hispanic
78
R Code for Side-by-Side Barcharts
b <- barplot(t(Pct.Change.Hispanic),
beside=TRUE,
col=c("#599ad3", "#79c36a"),
xlab="Decade",
ylab="Percent Change in Hispanic Population from Previous Decade",
ylim=c(0,70))
abline(h=0)
text(b[1,], Pct.Change.Hispanic.US + 2,
paste(Pct.Change.Hispanic.US,"%",sep=""))
text(b[2,], Pct.Change.Hispanic.NewOrleansMetro + 2,
paste(Pct.Change.Hispanic.NewOrleansMetro,"%",sep=""))
aged
aged 81
R Code for Stacked Barcharts
82
R Code for Stacked Barcharts
sb <- barchart(prop.table(aged, margin=1), xlab="Proportion",
par.settings=colorset,
panel=function(...) {
panel.barchart(...)
tmp <- list(...)
tmp <- data.frame(x=tmp$x, y=tmp$y)
# calculate positions of text labels
df <- ddply(tmp, .(y),
function(x) {
data.frame(x, pos=cumsum(x$x)-x$x/2)
})
panel.text(x=df$pos, y=df$y,
label=sprintf("%.02f", df$x),
cex=0.7)
},
auto.key=list(columns=4, space="bottom",
cex=0.8, size=1.4, adj=1,
between=0.2, between.colums=0.1))
plot(sb)
83
Comparative Pie Charts
Share of Hispanic Population by Nationality in Three New Orleans Parishes in 2010
(Expressed as a Count)
Mexican
Mexican
Mexican
Puerto Rican
Other
Puerto Rican
Cuban
Other
Cuban
Other
Puerto Rican
Cuban
?pie 84
R Code for Comparative Piecharts
PopulationShare <- c(10194, 2682, 3840, 36986,
4298, 948, 1285, 11520,
3593, 933, 816, 5628)
PopulationShare
PopulationShare
85
R Code for Comparative Piecharts
layout(matrix(c(1,2,3),1,3, byrow=TRUE))
86
Line Charts 150000
130,896
152,42
143,793
104,349
Population Living in Poverty
100000
110,179
105,687
82,469
50000
62,114
?matplot
?matpoints
Year
87
R Code for Line Charts
Year <- c(1979, 1989, 1999, 2009)
89
R Code for Line Charts
segments(Year[1],RestNewOrleansMetro[1]-10000, Year[1], RestNewOrleansMetro[1])
segments(Year[2],RestNewOrleansMetro[2]-10000, Year[2], RestNewOrleansMetro[2])
segments(Year[3],RestNewOrleansMetro[3]+10000, Year[3],
RestNewOrleansMetro[3])
segments(Year[4],RestNewOrleansMetro[4]-10000, Year[4], RestNewOrleansMetro[4])
90
R Code for Line Charts
legend("bottomright",
c("Orleans Parish","Rest of the New Orleans Metro"),
col=c("darkgreen","orange"),
lty=1,
lwd=2,
bty="n"
)
box()
91
Time Series Plot
Used for plotting the values of a quantitative
variable Y versus a time variable T.
e.g.: Y = Ozone
T = Date ?plot
0.06
0.06
0.05
0.05
0.05
0.04
0.04
0.04
Y
Y
0.03
0.03
0.03
0.02
0.02
0.02
Jan 01 Jan 06 Jan 11 Jan 16 Jan 21 Jan 26 Jan 31 Jan 01 Jan 06 Jan 11 Jan 16 Jan 21 Jan 26 Jan 31 Jan 01 Jan 06 Jan 11 Jan 16 Jan 21 Jan 26
92Jan 31
T T T
Time Series Plot (v.1)
Time Series Plot
0.14
plot(Ozone ~ Date,
0.12
ylab="Daily Max Ozone (ppm)",
main="Time Series Plot",
0.10
Daily Max Ozone (ppm)
sub="2011 BATON ROUGE/CAPITOL",
0.08
col.sub="red")
0.06
0.04
0.02
0.00
Date
2011 BATON ROUGE/CAPITOL
93
Time Series Plot (v.2)
plot(Ozone ~ Date,
type="l",
ylab="Daily Max Ozone (ppm)",
main="Time Series Plot",
sub="2011 BATON ROUGE/CAPITOL",
col.sub="red")
Time Series Plot
0.14
0.12
0.10
Daily Max Ozone (ppm)
0.08
0.06
0.04
0.02
0.00
Date
2011 BATON ROUGE/CAPITOL
94
Time Series Plot (v.3)
plot(Ozone ~ Date,
type="h",
ylab="Daily Max Ozone (ppm)",
main="Time Series Plot",
sub="2011 BATON ROUGE/CAPITOL",
col.sub="red")
Time Series Plot
0.14
0.12
0.10
Daily Max Ozone (ppm)
0.08
0.06
0.04
0.02
0.00
Date 95
2011 BATON ROUGE/CAPITOL
Time Series Plot (v.4)
plot(Ozone ~ Date,
type="h", Time Series Plot
0.14
ylab="Daily Max Ozone (ppm)",
col=bad,
0.12
main="Time Series Plot", 0.10
0.10
sub="2011 BATON ROUGE/CAPITOL",
0.08
col.sub="blue")
0.06
abline(h=0.10, lty=2, col="red") 0.04
0.02
text(locator(1),"0.10")
0.00
Date
2011 BATON ROUGE/CAPITOL
96
Scatterplot (v.1)
xlab="Temperature (°F)",
0.14
ylab="Daily Max Ozone (ppm)",
0.12
main="Scatterplot")
0.10
Daily Max Ozone (ppm)
0.08
0.06
0.04
0.02
0.00
30 40 50 60 70 80 90 100
Temperature (°F)
97
Scatterplot (v.2)
Scatterplot
0.14
require(car)
0.12
scatterplot(Ozone ~ Temp,
xlab="Temperature (°F)",
0.10
Daily Max Ozone (ppm)
ylab="Daily Max Ozone (ppm)",
0.08
smooth=FALSE,
0.06
reg.line=FALSE,
main="Scatterplot")
0.04
0.02
0.00
30 40 50 60 70 80 90 100
Temperature (°F)
help(scatterplot, package="car")
98
Coplot
coplot(Ozone ~ Temp | RelativeHumidity,
panel = panel.smooth)
Given : RelativeHumidity
60 70 80 90 100
30 40 50 60 70 80 90 100 30 40 50 60 70 80 90 100
0.12
0.08
?coplot
0.04
Ozone
0.00
0.12
0.08
0.04
0.00
30 40 50 60 70 80 90 100
99
Temp
Pairs Plot
pairs(cbind(Ozone, Temp, WindSpeed))
30 40 50 60 70 80 90
0.12
0.08
Ozone
0.04
0.00
90
80
60 70
Temp
50
40
30
15
10
WindSpeed
5
0
0.00 0.04 0.08 0.12 0 5 10 15
100
Exercise on Basic R Graphics
For this exercise, refer to the ozone data frame
available in your R working space and follow the
instructions below to create a variety of basic R
graphs using variables from this data frame.
101
Exercise on Basic R Graphics
5. Create a scatter plot of maxO3 versus T12.
6. Create a time series plot of maxO3.
7. Create side-by-side boxplots of maxO3 for the
four wind directions stored in the wind
variable).
8. Create a bar chart for the variable rain.
9. Create a bar chart for rain according to wind.
102
Customizing
Basic R Graphics
Learning Goal:
103
Customizing Basic R Graphics
Adding a main title: BATON ROUGE
80
e.g.: hist(Ozone,
Frequency
40
main="BATON ROUGE")
0
0.00 0.04 0.08 0.12
Ozone
e.g.: hist(Ozone,
80
Frequency
sub="Year 2011")
40
0
0.00 0.04 0.08 0.12
Ozone
Year 2011
104
Customizing Basic R Graphics
Adding x-axis and y- axis labels:
e.g.: hist(Ozone,
xlab="Ozone",
ylab="Frequency") Histogram of Ozone
80
Frequency
40
0
0.00 0.04 0.08 0.12
Ozone
105
Customizing Basic R Graphics
Adding a legend:
hist(Ozone, freq=FALSE, ylim=c(0,30))
lines(density(Ozone))
legend("topright", "Density Curve", lty=1,bty="n")
Histogram of Ozone
30
Density Curve
25
20
Density
15
10
5
0
hist(Ozone)
text(locator(1), "BATON ROUGE")
text(0.12, 20, "Year 2011") Histogram of Ozone
Notes:
80
BATON ROUGE
Frequency
60
When using the text() function:
40
• locator(1) places text wherever we click on
20
Year 2011
the current graph;
0
• text(x,y, "some text") places text at graph 0.00 0.04 0.08 0.12
Adding colors:
80
Frequency
60
hist(Ozone, col="violet")
40
20
0
0.00 0.02 0.04 0.06 0.08 0.10 0.12 0.14
Ozone
Note:
To see the list of 600+ colors available in R, type the following command in the
R Console window:
colors()
See also the help files for functions such as rainbow(), heat.colors(),
terrain.colors() and palette().
108
Customizing Basic R Graphics
Adding graphical symbols:
pch=1 pch=19
0.12
0.12
0.08
0.08
Ozone
Ozone
0.04
0.04
0.00
0.00
30 40 50 60 70 80 90 30 40 50 60 70 80 90
Temp Temp
par(mfrow=c(1,2))
plot(Ozone ~ Temp, pch=1, main="pch=1") Note:
plot(Ozone ~ Temp, pch=19, main="pch=19") To see the graphical symbols
available in R, use the command:
example(pch) 109
Customizing Basic R Graphics
Controlling the size of graphical symbols:
par(mfrow=c(1,3)) Options for cex:
cex = 1 (default size)
plot(Ozone ~ Temp, cex=1, main="cex=1")
cex = 0.5 (half default)
plot(Ozone ~ Temp, cex=0.5, main="cex=0.5") cex = 2 (twice default)
plot(Ozone ~ Temp, cex=2, main= "cex=2")
0.12
0.12
0.08
0.08
0.08
Ozone
Ozone
Ozone
0.04
0.04
0.04
0.00
0.00
0.00
30 40 50 60 70 80 90 100 30 40 50 60 70 80 90 100 30 40 50 60 70 80 90 100
110
Temp Temp Temp
Customizing Basic R Graphics
Options for line width:
Adding lines: lwd = 1 (default)
lwd = 0.5 (half default)
hist(Ozone, freq=FALSE, ylim=c(0,30)) lwd= 2 (twice default)
lines(density(Ozone))
30
lines(density(Ozone), lty=2, lwd=2)
25
20
Options for line type:
Density
15
lty=1 (solid) lty=5 (longdash)
10
lty=2 (dashed) lty=6 (twodash)
5
lty=3 (dotted)
0
lty=4 (dotdash) 0.00 0.02 0.04 0.06 0.08 0.10 0.12 0.14
Ozone 111
Exercise on Customizing Basic R Graphics
Create a scatter plot of maxO3 versus T12 using
the variables in the ozone data frame. Enhance
this scatter plot by adding the following
elements to it:
112
Advanced
R Graphics
Learning Goal:
113
Advanced R Graphics
Recall that advanced R graphics can be produced
using any of the following R packages:
Note:
• grid (not covered in this course) The lattice package
can replicate most of
the basic graphics.
However, the lattice
• trellis (covered in this course) package is particularly
helpful for visualizing
data conditional on
the values of one or
• ggplot2 (not covered in this course) more variables.
114
Lattice Functions
lattice function graphics function Description
histogram() hist() Histogram
densityplot() plot(density()) Density Plot
bwplot() boxplot() Boxplot
stripplot() stripchart() Strip Plot
xyplot() plot() Scatter Plot
dotplot() dotchart() Dot Plot
barchart() barplot() Bar Chart
splom() pairs() Pairwise Scatterplot
cloud() persp() 3-D Scatterplot
115
Lattice Formulas
The functions in the lattice package rely on a
formula framework. For instance:
histogram(~ Y)
Symbol interpretation
histogram(~ Y|F)
~ as a function of
histogram(~Y|F1*F2)
| conditional on
xyplot(Y ~ X)
* crossed with
xyplot(Y ~ X |F)
xyplot(Y ~ X|F1*F2)
116
Histogram 2011 BATON ROUGE/CAPITOL
require(lattice)
100
Count
histogram(~Ozone,
50
xlab="Daily Max Ozone (ppm)",
type="count",
main="2011 BATON ROUGE/CAPITOL") 0
Note:
For the histogram function, we can also use type= "density" to obtain a density
histogram or type="percent" to obtain a percent of total histogram.
117
Conditional Histograms
histogram(~Ozone | Month,
xlab="Daily Max Ozone (ppm)",
type="count",
main="2011 BATON ROUGE/CAPITOL")
2011 BATON ROUGE/CAPITOL
0.00 0.05 0.10 0.15 0.00 0.05 0.10 0.15
10
5
0
January February March April
20
15
10
5
0
densityplot(~Ozone,
xlab="Daily Max Ozone (ppm)",
main="2011 BATON ROUGE/CAPITOL")
2011 BATON ROUGE/CAPITOL
25
20
15
Density
10
30
20
10
0
September October November December
50
40
30
20
10
0
0.10
Daily Max Ozone (ppm)
0.05
0.00
January February March April May June July August September October November December
122
Month
Conditional Boxplots
bwplot(~Ozone | Month,
xlab="Daily Max Ozone (ppm)",
main="2011 BATON ROUGE/CAPITOL")
stripplot(Ozone ~ Month,
xlab="Month",
0.00
January February March April May June July August September October November December
Month
stripplot(Ozone ~ Month,
xlab="Month",
ylab="Daily Max Ozone (ppm)",
Daily Max Ozone (ppm)
0.10
0.00
January February March April May June July August September October
125
November December
Month
Conditional Strip Plots
stripplot(~ Ozone | Month,
xlab="Daily Max Ozone (ppm)",
main="2011 BATON ROUGE/CAPITOL",
as.table=TRUE)
2011 BATON ROUGE/CAPITOL
0.00 0.05 0.10 0.00 0.05 0.10
0.10
0.05
0.00
Date
127
Scatterplot
xyplot(Ozone ~ Temp,
xlab="Temperature (°F)",
ylab="Daily Max Ozone (ppm)",
main="2011 BATON ROUGE/CAPITOL")
2011 BATON ROUGE/CAPITOL
0.10
0.05
0.00
40 60 80 100
Temperature (°F)
128
Conditional Scatterplots
xyplot(Ozone ~ Temp | Month,
xlab="Temperature (°F)",
ylab="Daily Max Ozone (ppm)",
as.table=TRUE,
main="2011 BATON ROUGE/CAPITOL")
2011 BATON ROUGE/CAPITOL
40 60 80 100 40 60 80 100
0.10
0.05
Daily Max Ozone (ppm)
0.00
May June July August
0.10
0.05
0.00
September October November December
0.10
0.05
0.00
October
September
August
July
June
May
require(plyr) April
March
January
View(air)
0.03 0.04 0.05 0.06
Median Value of Daily Max Ozone on a Given Month (ppm)
OzoneMonthlySummary
130
Dot Plot (v.1)
131
Dot Plot (v.2)
December
November
October
September
August
July
June
May
April
March
February
January
133
Bar Chart (v.1)
barchart(Median ~ Month, data = OzoneMonthlySummary,
xlab="Month",
ylab="Median Value of Daily Max Ozone on a Given Month (ppm)",
main="2011 BATON ROUGE/CAPITOL")
0.05
0.04
0.03
January February March April May June July August September October November December
Month 134
Bar Chart (v.2)
barchart(Median ~ Month, data = OzoneMonthlySummary,
xlab="Month",
ylab="Median Value of Daily Max Ozone on a Given Month (ppm)",
main="2011 BATON ROUGE/CAPITOL",
panel=function(x, y, ...) {
panel.barchart(x, y, ...)
ltext(x=x, y=y+0.001, labels=y)
}
) 2011 BATON ROUGE/CAPITOL
0.068
0.06
0.0565
0.054
0.052
0.05
0.0445 0.045
0.044
0.043
0.041
0.04
0.0375
0.036
0.03
0.028
January February March April May June July August September October November December
Month 135
Bar Chart (v.3)
barchart(Month ~ Median, data = OzoneMonthlySummary,
xlab="Month",
xlim=c(0,0.08),
ylab="Median Value of Daily Max Ozone on a Given Month (ppm)",
main="2011 BATON ROUGE/CAPITOL",
panel=function(x, y, ...) {
panel.barchart(x, y, ...)
ltext(x=x+0.003, y=y, labels=x) 2011 BATON ROUGE/CAPITOL
} December 0.028
October 0.052
September 0.054
August 0.068
July 0.043
June 0.0565
May 0.045
April 0.0445
March 0.044
February 0.041
January 0.036
Month 136
Splom Plots
splom( ~ cbind(Ozone, Temp, RelativeHumidity))
100
80 90 100
90
80 RelativeHumidity 80
70
60 70 80
60
100 70 80 90 100
90
80
70
Temp
60
50
40
30 40 50 60
30
0.14
0.080.100.120.14
0.12
0.10
0.08
Ozone
0.06
0.04
0.02
0.000.020.040.06
0.00
137
Cloud Plot
cloud(Ozone ~ Temp*RelativeHumidity)
Ozone
RelativeHumidity
Temp
138
Conditional Cloud Plot
RelHum <- RelativeHumidity
140
Enhancing Lattice Graphs
Lattice graphs can be enhanced by:
• Adding basic features (e.g., titles, x-axis and
y-axis labels, colors)
• Using panel functions
• Using additional lattice graphics via the
latticeExtra package
141
Basic Enhancement of Lattice Graphs
histogram( ~ Ozone,
xlab = "Daily Max Ozone (ppm)",
main= "2011 BATON ROUGE/CAPITOL",
col= "lightblue") 2011 BATON ROUGE/CAPITOL
30
Percent of Total
20
10
} 20
15
Density
)
10
Ozone
143
Panel Enhancements of Lattice Graphs
Ozone
0.05
0.00
Date
144
The latticeExtra Package
The latticeExtra package extends the lattice package and has its own
dedicated website:
http://latticeextra.r-forge.r-project.org/
install.packages("latticeExtra")
require(latticeExtra)
145
latticeExtra: marginal.plot()
General Syntax:
January
marginal.plot( ~ Y) February
March
marginal.plot( ~ Y, groups = G, Ozone Temp
auto.key=list(lines=TRUE))
require(latticeExtra)
2 4 6 40 60 80
0. 0 0. 0 0. 0
air$Month <- Month RelativeHumidity SolarRadiation
marginal.plot(air2[ ,-1])
marginal.plot(air2[,-1], groups=air2$Month,
auto.key=list(lines=TRUE)) 146
latticeExtra: xyplot()
General Syntax:
xyplot( Y~ X,
xlab="x-axis label",
ylab="y-axis label",
main="Main Title",
panel = function(...) {
panel.xyplot(...)
panel.smoother(..., span = 0.5)
}
)
xyplot(Ozone~Date,
xlab="Year 2011",
ylab="Daily Max Ozone (ppm)",
main="BATON ROUGE/CAPITOL",
panel = function(...) {
panel.xyplot(...)
panel.smoother(..., span = 0.9)
}
) 147
latticeExtra: ecdfplot() Empirical Cumulative Distribution Plot
1.0
0.8
Empirical CDF
0.6
require(latticeExtra) 0.4
0.2
ecdfplot(~Ozone, 0.0
Empirical CDF
May June July August
1.0
xlab="Daily Max Ozone (ppm)", 0.8
0.6
0.4
0.2
0.0
main="Empirical Cumulative Distribution Plot") 1.0
January February March April
0.8
0.6
0.4
0.2
0.0
0.000.05 0.10 0.00 0.05 0.10
require(lattice) 149
Exercise on Lattice Graphics
Use the functions in the lattice package to
create the following graphs.
1) Create a histogram of maxO3.
2) Create conditional histograms of maxO3 for
each month.
3) Create a density plot of maxO3.
4) Create conditional density plots of maxO3 for
each month.
5) Create a boxplot of maxO3.
6) Create side-by-side boxplots of maxO3 for each
month. 150
Exercise on Lattice Graphics
7) Create a scatter plot of maxO3 vs. T12 and
enhance it by adding a title and axes labels.
8) Create conditional scatter plots of maxO3 vs.
T12 given month.
9) Create a time series plot of maxO3.
10) Create a separate time series plot of maxO3
for each month.
11) Create a splom plot of the variables maxO3, T12
and Wx12.
12) Create a cloud plot visualizing the dependency
of maxO3 on T12 and Wx12.
151
R Graphics
Housekeeping
Learning Goal:
152
Exporting R Graphics
R Graphics can be exported in a variety of formats:
• metafile (.wmf)
• postscript (.ps)
• pdf (.pdf)
• png (.png)
• bmp (.bmp)
• TIFF (.tiff)
• JPEG (.jpeg)
153
Exporting R Graphics
Graphics can be exported from R in one of three
ways:
154
Exporting R Graphics
To save graphs from the R command line, use any of
the following R functions:
Accessing Help Files for R Graphics
win.metafile() Export Functions:
postscript()
pdf() ?win.metafile
?postscript
png() ?pdf
bmp() ?png
tiff() ?bmp
?tiff
jpeg() ?jpeg
win.metafile("graph.wmf")
hist(rnorm(100))
dev.off()
156
R Graphics Housekeeping
To create a postscript file from the R
command line, use:
postscript("graph.ps")
hist(rnorm(100))
dev.off()
pdf("graph.pdf")
hist(rnorm(100))
dev.off()
158
R Graphics Housekeeping
To create a png file from the R command line,
use:
pdf("graph.png")
hist(rnorm(100))
dev.off()
159
R Graphics Housekeeping
bmp("graph.bmp")
hist(rnorm(100))
dev.off()
160
R Graphics Housekeeping
tiff("graph.tiff")
hist(rnorm(100))
dev.off()
161
R Graphics Housekeeping
To create a jpeg file from the R command line,
use:
jpeg("graph.jpeg")
hist(rnorm(100))
dev.off()
162
Exercise on R Graphics Housekeeping
With reference to the ozone data frame, create a
histogram of the variable maxO3 using either the
hist() function in the graphics package or the
histogram() function in the lattice package.
164
Summary
R provides 4 different graphical systems for producing
elegant, publication-quality graphics: graphics, grid,
lattice, ggplot2.
In this course, we explored in more detail some of the
functionality available in the graphics and lattice packages.
The lattice package relies heavily on the grid package.
Once you get comfortable with the graphics and lattice
packages, you can start exploring the ggplot2 package.
The ggplot2 package purports to combine the best
features of the graphics and lattice packages,
but has a completely different, more abstract syntax.
165
References on
Graphics in R
166
References on Graphics in R
Books:
• “Graphics for Statistics and Data Analysis with
R”, by Kevin J. Keen (CRC Press, 2010)
• “Lattice: Multivariate Data Visualization with
R”, by Deepayan Sarkar (Springer, 2008)
• “ggplot2: Elegant Graphics for Data Analysis”,
by Hadley Wickham (Springer-Verlag, 2009)
• “R Graphics”, 2nd Edition, by Paul Murrell
(Chapman & Hall/CRC, 2006)
167
References on Graphics in R
Websites:
Website Address Website Description
http://www.r-project.org R Project
http://www.statmethods.net Quick-R
http://www.r-bloggers.com R Bloggers
http://lmdvr.r-forge.r-project.org Lattice Website
http://ggplot2.org Ggplot2 Website
http://www.stat.auckland.ac.nz/~paul/grid/grid.html Grid Website
http://gallery.r-enthusiasts.com R Graph Gallery
168
Thank you
Thank you very much for attending this course.