R Graphics with ggplot2

Vinu CT
14 Sep 2012

A glimpse on Sachin's ODI scores

R Graphics with ggplot2

Vinu CT
14 Sep 2012
A glimpse on Sachin's ODI scores


  • Introduction
  • ggplot2 Components
    1. Data
    2. Aesthetic Mappings
    3. Geoms, Stats, Scales, coordinate system, and positions
    4. Faceting
    5. Themes
  • Publication Level Graphics
  1. Experience with R
  2. Idea about different Graphical representations
  3. Basic Statistics Knowledege
  4. Some knowledge about Cricket

(Bring your laptop with R installed.)

GGPLOT2: Introduction
  1. Produces very elegant graphics
  2. Very flexible and Plot specification at a high level of abstraction
  3. Very friendly email list (google group).
  4. Worth investing time in.
  5. Syntax takes a while to get used to.
  6. Relatively slow.
R Graphics Options
  1. Base Graphics
  2. Grid Graphics
  3. Lattice Graphics
  4. ggplot2



s10 <- read.csv(file="http://www.iimb.ernet.in/~vinuct10/ggplot/sachin.csv")
## Subsetting the dataset based on few dismissal
s10a <- subset(s10,Dismissal %in% c("bowled", "caught", "lbw","not out"))
s10a$Inns <- factor(s10a$Inns)
X StartDate Runs BF SR Pos Dismissal Inns Opposition Ground result Score
450 450 2012-03-13 6 19 31.57 2 caught 1 v Sri Lanka Dhaka won 304/3
451 451 2012-03-16 114 147 77.55 2 caught 1 v Bangladesh Dhaka lost 289/5
452 452 2012-03-18 52 48 108.33 2 caught 2 v Pakistan Dhaka won 330/4


Mapping aesthetics (position, size, color etc.)

Ball faced Vs Strike Rate by countries
s10c <- subset(s10,Opposition %in% c("v Australia","v South Africa","v New Zealand")  )


geom_smooth(aes(x=BF,y=SR,colour=Opposition )) + xlab("Ball Faced") + ylab("Runs Scored")

Geoms and Stats

## Examples of geoms: line,point,box,bar,... 
s10b <- subset(s10,result %in% c("won", "lost") & Opposition %in% c("v Australia","v South Africa","v New Zealand"))
s10b$result <- factor(s10b$result)
s10b$opp <- factor(s10b$Opposition)
ggplot(s10b,aes(opp,Runs))+geom_violin(aes(fill=opp)) require(chron) qplot(x=factor(years(StartDate)), y=Runs, geom="boxplot", fill=I("blue"), data=s10b)+ xlab("Year")

## Examples of stats: smooth, boxplot,... qplot(Runs, data=s10b,fill=result, geom=c("histogram")) qplot(Runs, data=s10b,colour=result,geom=c("density")) ggplot(data=s10b)+geom_point(aes(x=BF,y=Runs ))+ geom_line(data=s10,aes(BF,BF))+geom_smooth(aes(x=BF,y=Runs )) + xlab("Ball Faced") + ylab("Runs Scored")


## Scales 
ggplot(data=s10b)+geom_point(aes(x=BF,y=Runs,colour=result))+ scale_colour_manual(values=c("lost"="red","won"="green"))
ggplot(data=s10b)+geom_point(aes(x=BF,y=Runs,size=result,shape=result))+ scale_size_manual(values=c("lost"=5,"won"=3))+ scale_shape_manual(values=c("lost"=15,"won"=25))
ggplot(data=s10b)+geom_point(aes(x=BF,y=Runs,colour=result))+ scale_colour_discrete(name="Parinaam",labels=c("lost"="HAAR","won"="JEET"))


## Facets 
s10c= subset(s10,(Opposition %in% c("v Australia","v England","v South Africa","v Sri Lanka")) & (result %in% c("lost", "won")) )
s10c$yr= cut(as.numeric(format(as.Date(s10c$StartDate), format="%Y")),4)
qplot(data=s10c, factor(Inns),fill=result,geom="bar",position="fill",facets= Opposition~yr)


## Themes 
s10a <- subset(s10,Dismissal %in% c("bowled", "caught", "lbw","not out"))
s10a$Dismissal <- factor(s10a$Dismissal)
s10a$Inns <- factor(s10a$Inns)
qplot( data=s10a, Dismissal, geom="bar",fill=Inns)
qplot( data=s10a, Dismissal, geom="bar",fill=Inns)+theme_bw()
qplot( data=s10a, Dismissal, geom="bar",fill=Inns)+theme(panel.background = element_rect(fill="lightblue") )
qplot( data=s10a, Dismissal, geom="bar",fill=Inns)+ theme(plot.background= element_rect(fill="yellow"),panel.background= element_rect(fill='purple'))

Devices in R

  • Raster: png(), jpeg(), bitmap()
  • Vector: pdf(), postscript(),svg(),cairo_ps()
  • On-screen devices: X11, X11cairo, windows, JavaGD, quartz (OS X)
  • Packages: rgl (OpenGL), GDD (bitmap formats), RSvgDevice, canvas (HTML canvas), RSVGTipsDevice, tikzDevice (Latex).
Important factor for preparing publication level graphics

Width, height, font, and resolution

Useful Links

Slides themes are taken from Vadim Makeev's Shower templeate

Thank You