Data Visualization using R
Three R Graphics System
Basic Graphics
◦ Part of R installation and is the fundamental package for visualizing data
◦ Has a lot of good features to create all the basic plots using this package
Lattice
◦ Inspired by Trellis Graphics and was created by Deepayan Sarkar who is
part of the R core group
◦ Is a powerful data visualization system with an emphasis on multivariate data
◦ All parameters supplied in one go
ggplot2
◦ Created by Hadley Wickham based on the Grammar of Graphics
written by Leland Wilkinson
◦ Has a structured approach to data visualization and builds upon the features available
in Graphics and Lattice packages, but totally independent
◦ Designed to work in multiple layers, starting with a layer of raw data, then adding
layers of statistical information
◦ There is a helper function called qplot() (for quick plot) for creating
simple plots
R also has Interactive Visualization through
• Iplots
• Rggobi
• googleVis
• Shiny
Commands :
summary(mtcars$disp)
summary(mtcars$hp)
head(mtcars)
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
View(mtcars)
plot(mtcars$mpg)
---------------------------------------------------------------------------------------------------------
class(mtcars$cyl)
mtcars$cylfact=as.factor(mtcars$cyl)
class(mtcars$cylfact)
mtcars$cylfact
plot(mtcars$cylfact)
---------------------------------------------------------------------------------------------------
plot(mtcars$disp, mtcars$mpg)
---------------------------------------------------------------------------------------------------
mtcars$amfact <- as.factor(mtcars$am)
mtcars$cylfact <- as.factor(mtcars$cyl)
plot(mtcars$amfact, mtcars$cylfact)
---------------------------------------------------------------------------------------------------
mtcars$cylfact <- as.factor(mtcars$cyl)
plot(mtcars$cylfact,mtcars$mpg)
---------------------------------------------------------------------------------------------------
plot(mtcars$disp, mtcars$mp,main="Displacement Vs MPG")
---------------------------------------------------------------------------------------------------
plot(mtcars$disp, mtcars$mp,main="Displacement Vs
MPG",sub=" Subtitle Displacement VS MPG")
---------------------------------------------------------------------------------------------------
plot(mtcars$disp, mtcars$mp,main="Displacement Vs
MPG",sub=" Subtitle Displacement VS
MPG",xlab="Displacement",ylab="MPG")
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
title() Function
plot(mtcars$disp, mtcars$mp,main="Displacement Vs
MPG",sub=" Subtitle Displacement VS
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
MPG",xlab="Displacement",ylab="MPG",xlim=c(0,600),ylim=c(0,50))
---------------------------------------------------------------------------------------------------
plot(mtcars$disp, mtcars$mp)
title(main="Displacement Vs
MPG",sub=" Subtitle
Displacement VS
MPG",xlab="Displacement",ylab="MPG",xlim=c(0,600),ylim=c(0,50))
---------------------------------------------------------------------------------------------------
Colours
plot(mtcars$disp, mtcars$mp,col="red",main="Main
Title",col.main="blue",sub="Subtitle",col.sub="green",xlab="Displacement",ylab="MPG",co
l.lab="red",col.axis=rgb(0,1,1),fg=rgb(1,1,0))
---------------------------------------------------------------------------------------------------
Font
Feature Font Argument Size Argument
Title font.main cex.main
Subtitle font.sub cex.sub
Axis font.axis cex.axis
Labels font.lab cex.lab
Font Value FontType
1 Plain
2 Bold
3 Italic
4 Bold Italic
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
5 Symbol
---------------------------------------------------------------------------------------------------
plot(mtcars$disp,mtcars$mpg,font.axis=2,xlab="Displacement",ylab="MPG",font.lab=3)
plot(mtcars$disp,mtcars$mpg,font.axis=2,xlab="Displacement",ylab="MPG",font.lab=3,
main="Main Title",cex.main=2,sub="Subtitle",cex.sub=1.5,cex.lab=1.25,font.lab=2)
---------------------------------------------------------------------------------------------------
TEXT ANNOTATIONS
Text and Margin Text
text()
For adding text inside the plot
text( x,y,”Text String”)
mtext()
For adding next on margins
The margin where text need to be placed is specified by
side ( 1=Bottom, 2=Left, 3=Top, 4=Right)
Distance from margin is specified by line, default is 0
Positive value puts away from margin outside
Negative value puts away from margin inside
Horizontal alignment is done by adj ( 0 = left aligned , 1= right aligned)
Program
plot(mtcars$disp,mtcars$mpg,font.axis=2,xlab="Displacement",ylab="MPG",font.lab=3,mai
n="MainTitle", cex.main=2,sub="Subtitle",cex.sub=1.5,cex.lab=1.25,font.lab=2)
text(350,20,"This is sample text",col="red",font=2,family="mono")
text(250,25,"This is sample text",col="green",font=2,family="serif",cex=1.5)
mtext("Margin text",side=1,line=0,adj=1)
mtext("Margin text",side=3,line=3,adj=0)
mtext("Margin text", side=4,line=-3,adj=0)
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
---------------------------------------------------------------------------------------------------
layout
The objective is to combine multiple graphs in a single frame
par()
Graphical parameters using mfcol and mfrow
layout()
More flexible than par()
Enables to height and widths of rows and columns
matrix specified the location of plot
widths = width of columns
heights = Height of rows
Programme
1.
nit=par(no.readonly = TRUE)
par(mfrow=c(2,2))
plot(mtcars$mpg)
plot(mtcars$disp,mtcars$mpg)
plot(mtcars$disp)
boxplot(mtcars$mpg)
---------------------------------------------------------------------------------------------------
2.
layout(matrix(c(1,2,3,4),nrow=2,byrow=TRUE))
plot(mtcars$mpg)
plot(mtcars$disp,mtcars$mpg)
plot(mtcars$disp)
boxplot(mtcars$mpg)
---------------------------------------------------------------------------------------------------
3
layout(matrix(c(1,2,3,4),nrow=2,byrow=FALSE))
plot(mtcars$mpg)
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
plot(mtcars$disp,mtcars$mpg)
plot(mtcars$disp)
boxplot(mtcars$mpg)
---------------------------------------------------------------------------------------------------
4
layout(matrix(c(1,2,3,3),nrow=2,byrow=FALSE))
plot(mtcars$mpg)
boxplot(mtcars$mpg)
plot(mtcars$disp)
---------------------------------------------------------------------------------------------------
Managing Width
layout(matrix(c(1,2,3,4,5,6),nrow=2,byrow=TRUE),width=c(1,2,1))
plot(mtcars$disp,mtcars$mpg)
plot(mtcars$disp,mtcars$mpg)
plot(mtcars$disp,mtcars$mpg)
plot(mtcars$disp,mtcars$mpg)
plot(mtcars$disp,mtcars$mpg) Graph Description Formula
Type Example
plot(mtcars$disp,mtcars$mpg)
barchart bar chart x~A or A~x
------------------------------------------ bwplot boxplot x~A or A~x
Lattice Graphs cloud 3D scatterplot z~x*y|A
contourplo 3D contour plot z~x*y
Deepayan Sarkar is the author t
Improve on R graphics by proving better defaults densityplot kernal density plot ~x|A*B
dotplot dotplot ~x|A
Easy to display multivariate relations
histogram histogram ~x
Supports Trellis Graphs levelplot 3D level plot z~y*x
Format:graph_type(formula,data=..) stripplot strip plots A~x or x~A
xyplot scatterplot y~x|A
wireframe 3D wireframe z~y*x
Density Plot graph
library(lattice)
gear.f=factor(gear,levels=c(3,4,5),labels=c("3gears","4gears","5gears"))
cyl.f=factor(cyl,levels=c(4,6,8),labels=c("4cyl","6cyl","8cyl"))
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
densityplot(mtcars$mpg,main="Density Plot",xlab="Miles per Gallon")
Dot Plots
1.
# Simple Dotplot
dotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7, main="Gas Milage for Car Models",
xlab="Miles Per Gallon")
---------------------------------------------------------------------------------------------------
2.
# Dotplot: Grouped Sorted and Colored
# Sort by mpg, group and color by cylinder
x <- mtcars[order(mtcars$mpg),] # sort by mpg
x$cyl <- factor(x$cyl) # it must be a factor
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"
dotchart(x$mpg,labels=row.names(x),cex=.7,groups= x$cyl,
main="Gas Milage for Car Models\ngrouped by cylinder",
xlab="Miles Per Gallon", gcolor="black", color=x$color)
---------------------------------------------------------------------------------------------------
Bar Plot
1.
# Simple Bar Plot
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution",
xlab="Number of Gears")
---------------------------------------------------------------------------------------------------
2.
# Simple Horizontal Bar Plot with Added Labels
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution", horiz=TRUE,
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
names.arg=c("3 Gears", "4 Gears", "5 Gears"))
---------------------------------------------------------------------------------------------------
3.
# Stacked Bar Plot with Colors and Legend
counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and VS",
xlab="Number of Gears", col=c("darkblue","red"),
legend = rownames(counts))
---------------------------------------------------------------------------------------------------
4. # Fitting Labels
par(las=2) # make label text perpendicular to axis
par(mar=c(5,8,4,2)) # increase y-axis margin.
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution", horiz=TRUE, names.arg=c("3 Gears", "4 Gears", "5
Gears"), cex.names=0.8)
---------------------------------------------------------------------------------------------------
Line Charts
1.
x <- c(1:5); y <- x # create some data
par(pch=22, col="red") # plotting symbol and color
par(mfrow=c(2,4)) # all plots on one page
opts = c("p","l","o","b","c","s","S","h")
for(i in 1:length(opts)){
heading = paste("type=",opts[i])
plot(x, y, type="n", main=heading)
lines(x, y, type=opts[i])
}
----------------------------------------------------------------------------------------------------------------
2.
x <- c(1:5); y <- x # create some data
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
par(pch=22, col="blue") # plotting symbol and color
par(mfrow=c(2,4)) # all plots on one page
opts = c("p","l","o","b","c","s","S","h")
for(i in 1:length(opts){
heading = paste("type=",opts[i])
plot(x, y, main=heading)
lines(x, y, type=opts[i])
----------------------------------------------------------------------------------------------------------------
3.
# Create Line Chart
# convert factor to numeric for convenience
Orange$Tree <- as.numeric(Orange$Tree)
ntrees <- max(Orange$Tree)
# get the range for the x and y axis
xrange <- range(Orange$age)
yrange <- range(Orange$circumference)
# set up the plot
plot(xrange, yrange, type="n", xlab="Age (days)",
ylab="Circumference (mm)" )
colors <- rainbow(ntrees)
linetype <- c(1:ntrees)
plotchar <- seq(18,18+ntrees,1)
# add lines
for (i in 1:ntrees) {
tree <- subset(Orange, Tree==i)
lines(tree$age, tree$circumference, type="b", lwd=1.5,
lty=linetype[i], col=colors[i], pch=plotchar[i])
}
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
# add a title and subtitle
title("Tree Growth", "example of line plot")
# add a legend
legend(xrange[1], yrange[2], 1:ntrees, cex=0.8, col=colors,
pch=plotchar, lty=linetype, title="Tree")
----------------------------------------------------------------------------------------------------------------
Pie Charts
1.
# Simple Pie Chart
slices <- c(10, 12,4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pie(slices, labels = lbls, main="Pie Chart of Countries")
----------------------------------------------------------------------------------------------------------------
2.
# Pie Chart with Percentages
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(slices,labels = lbls, col=rainbow(length(lbls)),
main="Pie Chart of Countries")
----------------------------------------------------------------------------------------------------------------
3.
# 3D Exploded Pie Chart
library(plotrix)
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pie3D(slices,labels=lbls,explode=0.1,
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
main="Pie Chart of Countries ")
----------------------------------------------------------------------------------------------------------------
4.
# Pie Chart from data frame with Appended Sample Sizes
mytable <- table(iris$Species)
lbls <- paste(names(mytable), "\n", mytable, sep="")
pie(mytable, labels = lbls,
main="Pie Chart of Species\n (with sample sizes)")
----------------------------------------------------------------------------------------------------------------
BoxPlot
1.
# Boxplot of MPG by Car Cylinders
boxplot(mpg~cyl,data=mtcars, main="Car Milage Data",
xlab="Number of Cylinders", ylab="Miles Per Gallon")
----------------------------------------------------------------------------------------------------------------
2.
# Notched Boxplot of Tooth Growth Against 2 Crossed Factors
# boxes colored for ease of interpretation
boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,
col=(c("gold","darkgreen")),
main="Tooth Growth", xlab="Suppliment and Dose")
----------------------------------------------------------------------------------------------------------------
3.
# Violin Plots
library(vioplot)
x1 <- mtcars$mpg[mtcars$cyl==4]
x2 <- mtcars$mpg[mtcars$cyl==6]
x3 <- mtcars$mpg[mtcars$cyl==8]
vioplot(x1, x2, x3, names=c("4 cyl", "6 cyl", "8 cyl"),
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
col="gold")
title("Violin Plots of Miles Per Gallon")
----------------------------------------------------------------------------------------------------------------
4.
# Example of a Bagplot
library(aplpack)
attach(mtcars)
bagplot(wt,mpg, xlab="Car Weight", ylab="Miles Per Gallon",
main="Bagplot Example")
----------------------------------------------------------------------------------------------------------------
Scatter Plot
1.
# Simple Scatterplot
attach(mtcars)
plot(wt, mpg, main="Scatterplot Example",
xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
----------------------------------------------------------------------------------------------------------------
2. # Simple Scatterplot
attach(mtcars)
plot(wt, mpg, main="Scatterplot Example",
xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
----------------------------------------------------------------------------------------------------------------
3.
# Add fit lines
abline(lm(mpg~wt), col="red") # regression line (y~x)
lines(lowess(wt,mpg), col="blue") # lowess line (x,y)
----------------------------------------------------------------------------------------------------------------
4.
# Scatterplot Matrices from the glus Package
library(gclus)
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
dta <- mtcars[c(1,3,5,6)] # get data
dta.r <- abs(cor(dta)) # get correlations
dta.col <- dmat.color(dta.r) # get colors
# reorder variables so those with highest correlation
# are closest to the diagonal
dta.o <- order.single(dta.r)
cpairs(dta, dta.o, panel.colors=dta.col, gap=.5,
main="Variables Ordered and Colored by Correlation" )
----------------------------------------------------------------------------------------------------------------
5.
# High Density Scatterplot with Binning
library(hexbin)
x <- rnorm(1000)
y <- rnorm(1000)
bin<-hexbin(x, y, xbins=50)
plot(bin, main="Hexagonal Binning")
----------------------------------------------------------------------------------------------------------------
6.
# High Density Scatterplot with Color Transparency
pdf("c:/scatterplot.pdf")
x <- rnorm(1000)
y <- rnorm(1000)
plot(x,y, main="PDF Scatterplot Example", col=rgb(0,100,0,50,maxColorValue=255),
pch=16)
----------------------------------------------------------------------------------------------------------------
7.
# 3D Scatterplot
library(scatterplot3d)
attach(mtcars)
scatterplot3d(wt,disp,mpg, main="3D Scatterplot")
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
----------------------------------------------------------------------------------------------------------------
8.
# 3D Scatterplot with Coloring and Vertical Drop Lines
library(scatterplot3d)
attach(mtcars)
scatterplot3d(wt,disp,mpg, pch=16, highlight.3d=TRUE,
type="h", main="3D Scatterplot")
----------------------------------------------------------------------------------------------------------------
9.
# 3D Scatterplot with Coloring and Vertical Lines
# and Regression Plane
library(scatterplot3d)
attach(mtcars)
s3d <-scatterplot3d(wt,disp,mpg, pch=16, highlight.3d=TRUE,
type="h", main="3D Scatterplot")
fit <- lm(mpg ~ wt+disp)
s3d$plane3d(fit)
----------------------------------------------------------------------------------------------------------------
10.
# Spinning 3d Scatterplot
library(rgl)
plot3d(wt, disp, mpg, col="red", size=3)
----------------------------------------------------------------------------------------------------------------
11.
# Another Spinning 3d Scatterplot
library(Rcmdr)
attach(mtcars)
scatter3d(wt, disp, mpg)
----------------------------------------------------------------------------------------------------------------
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
Graphics
1.
# Set a graphical parameter using par()
par() # view current settings
opar <- par() # make a copy of current settings
par(col.lab="red") # red x and y labels
hist(mtcars$mpg) # create a plot with these new settings
par(opar) # restore original settings
# Set a graphical parameter within the plotting function
hist(mtcars$mpg, col.lab="red")
----------------------------------------------------------------------------------------------------------------
2.
# Type family examples - creating new mappings
plot(1:10,1:10,type="n")
windowsFonts(
A=windowsFont("Arial Black"),
B=windowsFont("Bookman Old Style"),
C=windowsFont("Comic Sans MS"),
D=windowsFont("Symbol")
)
text(3,3,"Hello World Default")
text(4,4,family="A","Hello World from Arial Black")
text(5,5,family="B","Hello World from Bookman Old Style")
text(6,6,family="C","Hello World from Comic Sans MS")
text(7,7,family="D", "Hello World from Symbol")
----------------------------------------------------------------------------------------------------------------
3.
# Example of labeling points
attach(mtcars)
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
plot(wt, mpg, main="Milage vs. Car Weight",
xlab="Weight", ylab="Mileage", pch=18, col="blue")
text(wt, mpg, row.names(mtcars), cex=0.6, pos=4, col="red")
----------------------------------------------------------------------------------------------------------------
4.
# A Silly Axis Example
# specify the data
x <- c(1:10); y <- x; z <- 10/x
# create extra margin room on the right for an axis
par(mar=c(5, 4, 4, 8) + 0.1)
# plot x vs. y
plot(x, y,type="b", pch=21, col="red",
yaxt="n", lty=3, xlab="", ylab="")
# add x vs. 1/x
lines(x, z, type="b", pch=22, col="blue", lty=2)
# draw an axis on the left
axis(2, at=x,labels=x, col.axis="red", las=2)
# draw an axis on the right, with smaller text and ticks
axis(4, at=z,labels=round(z,digits=2),
col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
# add a title for the right axis
mtext("y=1/x", side=4, line=3, cex.lab=1,las=2, col="blue")
# add a main title and bottom and left axis labels
title("An Example of Creative Axes", xlab="X values",
ylab="Y=X")
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
----------------------------------------------------------------------------------------------------------------
5.
# Legend Example
attach(mtcars)
boxplot(mpg~cyl, main="Milage by Car Weight",
yaxt="n", xlab="Milage", horizontal=TRUE,
col=terrain.colors(3))
legend("topright", inset=.05, title="Number of Cylinders",
c("4","6","8"), fill=terrain.colors(3), horiz=TRUE)
----------------------------------------------------------------------------------------------------------------
6.
# 4 figures arranged in 2 rows and 2 columns
attach(mtcars)
par(mfrow=c(2,2))
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")
----------------------------------------------------------------------------------------------------------------
7.
# 3 figures arranged in 3 rows and 1 column
attach(mtcars)
par(mfrow=c(3,1))
hist(wt)
hist(mpg)
hist(disp)
----------------------------------------------------------------------------------------------------------------
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
8.
# One figure in row 1 and two figures in row 2
attach(mtcars)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)
----------------------------------------------------------------------------------------------------------------
9.
# Add boxplots to a scatterplot
par(fig=c(0,0.8,0,0.8), new=TRUE)
plot(mtcars$wt, mtcars$mpg, xlab="Car Weight",
ylab="Miles Per Gallon")
par(fig=c(0,0.8,0.55,1), new=TRUE)
boxplot(mtcars$wt, horizontal=TRUE, axes=FALSE)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mtcars$mpg, axes=FALSE)
mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)
----------------------------------------------------------------------------------------------------------------
Lattices
1.
# Lattice Examples
library(lattice)
attach(mtcars)
# create factors with value labels
gear.f<-factor(gear,levels=c(3,4,5),
labels=c("3gears","4gears","5gears"))
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
cyl.f <-factor(cyl,levels=c(4,6,8),
labels=c("4cyl","6cyl","8cyl"))
# kernel density plot
densityplot(~mpg,
main="Density Plot",
xlab="Miles per Gallon")
# kernel density plots by factor level
densityplot(~mpg|cyl.f,
main="Density Plot by Number of Cylinders",
xlab="Miles per Gallon")
# kernel density plots by factor level (alternate layout)
densityplot(~mpg|cyl.f,
main="Density Plot by Numer of Cylinders",
xlab="Miles per Gallon",
layout=c(1,3))
# boxplots for each combination of two factors
bwplot(cyl.f~mpg|gear.f,
ylab="Cylinders", xlab="Miles per Gallon",
main="Mileage by Cylinders and Gears",
layout=(c(1,3))
# scatterplots for each combination of two factors
xyplot(mpg~wt|cyl.f*gear.f,
main="Scatterplots by Cylinders and Gears",
ylab="Miles per Gallon", xlab="Car Weight")
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
# 3d scatterplot by factor level
cloud(mpg~wt*qsec|cyl.f,
main="3D Scatterplot by Cylinders")
# dotplot for each combination of two factors
dotplot(cyl.f~mpg|gear.f,
main="Dotplot Plot by Number of Gears and Cylinders",
xlab="Miles Per Gallon")
# scatterplot matrix
splom(mtcars[c(1,3,4,5,6)],
main="MTCARS Data")
----------------------------------------------------------------------------------------------------------------
2.
# Customized Lattice Example
library(lattice)
panel.smoother <- function(x, y) {
panel.xyplot(x, y) # show points
panel.loess(x, y) # show smoothed line
}
attach(mtcars)
hp <- cut(hp,3) # divide horse power into three bands
xyplot(mpg~wt|hp, scales=list(cex=.8, col="red"),
panel=panel.smoother,
xlab="Weight", ylab="Miles per Gallon",
main="MGP vs Weight by Horse Power")
----------------------------------------------------------------------------------------------------------------
ggplot2
1.
# ggplot2 examples
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
library(ggplot2)
# create factors with value labels
mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5),
labels=c("3gears","4gears","5gears"))
mtcars$am <- factor(mtcars$am,levels=c(0,1),
labels=c("Automatic","Manual"))
mtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8),
labels=c("4cyl","6cyl","8cyl"))
# Kernel density plots for mpg
# grouped by number of gears (indicated by color)
qplot(mpg, data=mtcars, geom="density", fill=gear, alpha=I(.5),
main="Distribution of Gas Milage", xlab="Miles Per Gallon",
ylab="Density")
# Scatterplot of mpg vs. hp for each combination of gears and cylinders
# in each facet, transmittion type is represented by shape and color
qplot(hp, mpg, data=mtcars, shape=am, color=am,
facets=gear~cyl, size=I(3),
xlab="Horsepower", ylab="Miles per Gallon")
# Separate regressions of mpg on weight for each number of cylinders
qplot(wt, mpg, data=mtcars, geom=c("point", "smooth"),
method="lm", formula=y~x, color=cyl,
main="Regression of MPG on Weight",
xlab="Weight", ylab="Miles per Gallon")
# Boxplots of mpg by number of gears
# observations (points) are overlayed and jittered
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
qplot(gear, mpg, data=mtcars, geom=c("boxplot", "jitter"),
fill=gear, main="Mileage by Gear Number",
xlab="", ylab="Miles per Gallon")
----------------------------------------------------------------------------------------------------------------
2.
library(ggplot2)
p <- qplot(hp, mpg, data=mtcars, shape=am, color=am,
facets=gear~cyl, main="Scatterplots of MPG vs. Horsepower",
xlab="Horsepower", ylab="Miles per Gallon")
# White background and black grid lines
p + theme_bw()
# Large brown bold italics labels
# and legend placed at top of plot
p + theme(axis.title=element_text(face="bold.italic",
size="12", color="brown"), legend.position="top")
----------------------------------------------------------------------------------------------------------------
Probability Plots
1. # Display the Student's t distributions with various
# degrees of freedom and compare to the normal distribution
x <- seq(-4, 4, length=100)
hx <- dnorm(x)
degf <- c(1, 3, 8, 30)
colors <- c("red", "blue", "darkgreen", "gold", "black")
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
labels <- c("df=1", "df=3", "df=8", "df=30", "normal")
plot(x, hx, type="l", lty=2, xlab="x value",
ylab="Density", main="Comparison of t Distributions")
for (i in 1:4){
lines(x, dt(x,degf[i]), lwd=2, col=colors[i])
}
legend("topright", inset=.05, title="Distributions",
labels, lwd=2, lty=c(1, 1, 1, 1, 2), col=colors)
----------------------------------------------------------------------------------------------------------------
2.
# Children's IQ scores are normally distributed with a
# mean of 100 and a standard deviation of 15. What
# proportion of children are expected to have an IQ between
# 80 and 120?
mean=100; sd=15
lb=80; ub=120
x <- seq(-4,4,length=100)*sd + mean
hx <- dnorm(x,mean,sd)
plot(x, hx, type="n", xlab="IQ Values", ylab="",
main="Normal Distribution", axes=FALSE)
i <- x >= lb & x <= ub
lines(x, hx)
polygon(c(lb,x[i],ub), c(0,hx[i],0), col="red")
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
area <- pnorm(ub, mean, sd) - pnorm(lb, mean, sd)
result <- paste("P(",lb,"< IQ <",ub,") =",
signif(area, digits=3))
mtext(result,3)
axis(1, at=seq(40, 160, 20), pos=0)
----------------------------------------------------------------------------------------------------------------
3.
# Q-Q plots
par(mfrow=c(1,2))
# create sample data
x <- rt(100, df=3)
# normal fit
qqnorm(x); qqline(x)
# t(3Df) fit
qqplot(rt(1000,df=3), x, main="t(3) Q-Q Plot",
ylab="Sample Quantiles")
abline(0,1)
----------------------------------------------------------------------------------------------------------------
4.
# Estimate parameters assuming log-Normal distribution
# create some sample data
x <- rlnorm(100)
# estimate paramters
library(MASS)
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
fitdistr(x, "lognormal")
----------------------------------------------------------------------------------------------------------------
# Mosaic Plot Example
1.
library(vcd)
mosaic(HairEyeColor, shade=TRUE, legend=TRUE)
----------------------------------------------------------------------------------------------------------------
2.
# Association Plot Example
library(vcd)
assoc(HairEyeColor, shade=TRUE)
----------------------------------------------------------------------------------------------------------------
Correlogram
1.
# First Correlogram Example
library(corrgram)
corrgram(mtcars, order=TRUE, lower.panel=panel.shade,upper.panel=panel.pie,
text.panel=panel.txt,main="Car Milage Data in PC2/PC1 Order")
----------------------------------------------------------------------------------------------------------------
2. # Second Correlogram Example
corrgram(mtcars, order=TRUE, lower.panel=panel.ellipse,
upper.panel=panel.pts, text.panel=panel.txt,
diag.panel=panel.minmax,
main="Car Mileage Data in PC2/PC1 Order")
----------------------------------------------------------------------------------------------------------------
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli
3. # Third Correlogram Example
corrgram(mtcars, order=NULL, lower.panel=panel.shade,
upper.panel=NULL, text.panel=panel.txt,
main="Car Milage Data (unsorted)")
----------------------------------------------------------------------------------------------------------------
4.# Changing Colors in a Correlogram
library(corrgram)
col.corrgram <- function(ncol){
colorRampPalette(c("darkgoldenrod4", "burlywood1",
"darkkhaki", "darkgreen"))(ncol)}
corrgram(mtcars, order=TRUE, lower.panel=panel.shade,
upper.panel=panel.pie, text.panel=panel.txt,
main="Correlogram of Car Mileage Data (PC2/PC1 Order)")
Dr.A. Pappu Rajan - Associate Professor – St. Joseph’s Institute of Management , Tiruchirappalli