SlideShare a Scribd company logo
1 of 36
Download to read offline
Plotting systems in R
Ilya Zhbannikov
Graphics Devices in R
• Screen device
• File device (pdf, png, jpeg, etc.)
Graphics device: a place to make plot appear
How does a plot get created?
• Call a plotting function, i.e. plot(), xyplot(),
qplot()
• The plot appears on a screen device
• Annotate plot, if necessary
• Save it to file, for example.
Screen and file devices
• How the plot will be used?
• Use screen device for quick visualisation or
exploratory studies.
• Plotting functions (plot(), qplot(), xyplot()) send
output to the screen device by default.
• If the plot will be printed, use file device instead.
Example
attach(mtcars)
head(mtcars[, c("hp", "mpg")])
plot(hp, mpg)
Example
attach(mtcars)
head(mtcars[, c("hp", "mpg")])
plot(hp, mpg)
title("MPG vs. hp”) # Add a title
Available plotting systems
• Base
• lattice
• ggplot2
Base plotting system
• Installed by default
• Quickest way to visualise your data
• Plot can be further updated using additional
options
Simple example
attach(mtcars)
plot(hp, mpg)
Extended example
plot(disp,mpg,
main = "MPG vs. HP", # Add a title
type = "p",
col = "grey", # Change the color of the points
pch = 16, # Change the plotting symbol see help(points)
cex = 1, # Change size of plotting symbol
xlab = “Horse power", # Add a label on the x-axis
ylab = "Miles per Gallon", # Add a label on the y-axis
bty = "n", # Remove the box around the plot
#asp = 1, # Change the y/x aspect ratio see help(plot)
font.axis = 1, # Change axis font to bold italic
col.axis = "black", # Set the color of the axis
xlim = c(20,500), # Set limits on x axis
ylim = c(5,50), # Set limits on y axis
las=1) # Make axis labels parallel to x-axis
Extended example
More examples
More examples
par(mfrow=c(2,2))
• Multivariate data
• Uses formulas
• Single function call
The Lattice Plotting System
The Lattice Plotting System
• lattice: code for producing Trellis graphics,
includes functions like xyplot(), bwplot(), levelplot()
• Grid: implements a different graphics system and
the lattice package are on top of it
Main plot functions
• xyplot(): scatterplots
• bwplot(): boxplots
• histogram(): histograms
• stripplot(): a boxplot points
• dotplot(): plot dots on "violin strings"
• splom(): scatterplot matrix
• levelplot(), contourplot(): for "image" data
Example
library(lattice)
attach(mtcars)
xyplot(mpg ~ hp, data = mtcars)
Extended example
More examples
• See lattice_example_1.R
ggplot2 Plotting System
• “Grammar of Graphics” by Leland Wilkinson,
written by Hadley Wickham
• Data must be a data frame
• Uses grammar
• Web site: http://ggplot2.org
What is "Grammar of Graphics"?
"In brief, the grammar tells us that a statistical
graphic is a mapping from data to aesthetic
attributes (colour, shape, size) of geometric
objects (points, lines, bars). The plot may also
contain statistical transformations of the data and
is drawn on a specific coordinate system”-
ggplot2 book
qplot() and ggplot()
• qplot() - works like plot() function in base
plotting system data
• should represent a data frame
• aesthetic (size, colour, shape) and geoms
(points, lines)
• core of ggplot()
qplot
• qplot() - works like plot() function in base
plotting system data
• Should represent a data frame
• aesthetic (size, colour) and geoms (points,
lines)
• Core of ggplot()
Example
library(ggplot2)
attach(mtcars)
head(mtcars)
# Simple plot:
qplot(data=mtcars,x=hp, y=mpg,main="MPG vs. hp")
Example
# Color gradient
qplot(data=mtcars,x=log(hp),y=log(mpg),color=mpg)
Example
# Boxplots:
qplot(data=mtcars,x=factor(cyl), y=mpg,geom="boxplot", main="MPG vs. # of Cylinders")
Components of ggplot()
• A data frame
• aesthetic mapping (how data are mapped
to colour and size)
• geoms (points, shapes, lines)
• facets
• stats (binning, smoothing, quantiles)
• scales (for aesthetic mapping, i.e.
male=‘red’, female=‘blue’)
• coordinate system
Peng at al, Explanatory Data Analysis, Coursera online class
Steps to plot with ggplot()
• Plots are build up in layers
• Plot the data
• Overlay a summary
• Add metadata and annotation
Peng at al, Explanatory Data Analysis, Coursera online class
Peng at al, Explanatory Data Analysis, Coursera online class
Peng at al, Explanatory Data Analysis, Coursera online class
Example
# Basic:
ggplot(mtcars, aes(hp, mpg)) + geom_point()
Peng at al, Explanatory Data Analysis, Coursera online class
Example
# With color:
ggplot(mtcars, aes(hp, mpg)) + geom_point(aes(color = cyl))
Peng at al, Explanatory Data Analysis, Coursera online class
Example
# Boxplots:
ggplot(data=mtcars, aes(hp, mpg)) + geom_boxplot(aes(as.factor(cyl)))
Peng at al, Explanatory Data Analysis, Coursera online class
Example
# Faceting
mtcars$cyl <- factor(mtcars$cyl)
ggplot(data=mtcars, aes(hp, mpg)) + geom_point() + facet_grid(~ cyl)
Peng at al, Explanatory Data Analysis, Coursera online class
Example
# Faceting
mtcars$cyl <- factor(mtcars$cyl)
ggplot(data=mtcars, aes(hp, mpg)) + geom_point() + facet_grid(~ cyl)
Peng at al, Explanatory Data Analysis, Coursera online class
Example
ggplot(data=mtcars, aes(hp, mpg)) + geom_point() + facet_grid(~ cyl) +
geom_smooth(colour = "blue", size = 1, method=lm)
Resources
• http://www.r-bloggers.com
• http://revolutionanalytics.com
• http://www.statmethods.net (Quick-R)
• Coursera (Data Science Specialisation)

More Related Content

What's hot

peRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysispeRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysis
Vyacheslav Arbuzov
 
Создание картограмм на принципах грамматики графики. С помощью R-расширения g...
Создание картограмм на принципах грамматики графики. С помощью R-расширения g...Создание картограмм на принципах грамматики графики. С помощью R-расширения g...
Создание картограмм на принципах грамматики графики. С помощью R-расширения g...
Matrunich Consulting
 

What's hot (19)

Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
 
Geo Spatial Plot using R
Geo Spatial Plot using R Geo Spatial Plot using R
Geo Spatial Plot using R
 
R-ggplot2 package Examples
R-ggplot2 package ExamplesR-ggplot2 package Examples
R-ggplot2 package Examples
 
NumPy Refresher
NumPy RefresherNumPy Refresher
NumPy Refresher
 
peRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysispeRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysis
 
Data visualization using the grammar of graphics
Data visualization using the grammar of graphicsData visualization using the grammar of graphics
Data visualization using the grammar of graphics
 
DSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser Bootsma
DSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser BootsmaDSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser Bootsma
DSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser Bootsma
 
ePOM - Intro to Ocean Data Science - Data Visualization
ePOM - Intro to Ocean Data Science - Data VisualizationePOM - Intro to Ocean Data Science - Data Visualization
ePOM - Intro to Ocean Data Science - Data Visualization
 
Sergey Shelpuk & Olha Romaniuk - “Deep learning, Tensorflow, and Fashion: how...
Sergey Shelpuk & Olha Romaniuk - “Deep learning, Tensorflow, and Fashion: how...Sergey Shelpuk & Olha Romaniuk - “Deep learning, Tensorflow, and Fashion: how...
Sergey Shelpuk & Olha Romaniuk - “Deep learning, Tensorflow, and Fashion: how...
 
Your first TensorFlow programming with Jupyter
Your first TensorFlow programming with JupyterYour first TensorFlow programming with Jupyter
Your first TensorFlow programming with Jupyter
 
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
 
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
 
R and Visualization: A match made in Heaven
R and Visualization: A match made in HeavenR and Visualization: A match made in Heaven
R and Visualization: A match made in Heaven
 
R
RR
R
 
Day 3 plotting.pptx
Day 3   plotting.pptxDay 3   plotting.pptx
Day 3 plotting.pptx
 
Создание картограмм на принципах грамматики графики. С помощью R-расширения g...
Создание картограмм на принципах грамматики графики. С помощью R-расширения g...Создание картограмм на принципах грамматики графики. С помощью R-расширения g...
Создание картограмм на принципах грамматики графики. С помощью R-расширения g...
 
ePOM - Intro to Ocean Data Science - Scientific Computing
ePOM - Intro to Ocean Data Science - Scientific ComputingePOM - Intro to Ocean Data Science - Scientific Computing
ePOM - Intro to Ocean Data Science - Scientific Computing
 
Machine Learning Basics for Web Application Developers
Machine Learning Basics for Web Application DevelopersMachine Learning Basics for Web Application Developers
Machine Learning Basics for Web Application Developers
 
Maps
MapsMaps
Maps
 

Similar to Presentation: Plotting Systems in R

Similar to Presentation: Plotting Systems in R (20)

R training5
R training5R training5
R training5
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
Exploratory data analysis using r
Exploratory data analysis using rExploratory data analysis using r
Exploratory data analysis using r
 
MatplotLib.pptx
MatplotLib.pptxMatplotLib.pptx
MatplotLib.pptx
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine Learning
 
UNit-III. part 2.pdf
UNit-III. part 2.pdfUNit-III. part 2.pdf
UNit-III. part 2.pdf
 
Lec2
Lec2Lec2
Lec2
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlib
 
Lecture 02 visualization and programming
Lecture 02   visualization and programmingLecture 02   visualization and programming
Lecture 02 visualization and programming
 
Elegant Graphics for Data Analysis with ggplot2
Elegant Graphics for Data Analysis with ggplot2Elegant Graphics for Data Analysis with ggplot2
Elegant Graphics for Data Analysis with ggplot2
 
Data Exploration in R.pptx
Data Exploration in R.pptxData Exploration in R.pptx
Data Exploration in R.pptx
 
K10947 Vikas ct
K10947 Vikas ctK10947 Vikas ct
K10947 Vikas ct
 
Tools for research plotting
Tools for research plottingTools for research plotting
Tools for research plotting
 
Tools for research plotting
Tools for research plottingTools for research plotting
Tools for research plotting
 
Intro matlab
Intro matlabIntro matlab
Intro matlab
 
Visualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptxVisualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptx
 
12-IP.pdf
12-IP.pdf12-IP.pdf
12-IP.pdf
 
Python grass
Python grassPython grass
Python grass
 
Time Series.pptx
Time Series.pptxTime Series.pptx
Time Series.pptx
 
UNIT_4_data visualization.pptx
UNIT_4_data visualization.pptxUNIT_4_data visualization.pptx
UNIT_4_data visualization.pptx
 

Recently uploaded

Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
nirzagarg
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
ahmedjiabur940
 
Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
HyderabadDolls
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
gajnagarg
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
gajnagarg
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
vexqp
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
HyderabadDolls
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
 

Recently uploaded (20)

Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
 
Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
 
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
 
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
High Profile Call Girls Service in Jalore { 9332606886 } VVIP NISHA Call Girl...
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
 
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service AvailableVastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Available
 
Introduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptxIntroduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptx
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubai
 
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
 

Presentation: Plotting Systems in R

  • 1. Plotting systems in R Ilya Zhbannikov
  • 2. Graphics Devices in R • Screen device • File device (pdf, png, jpeg, etc.) Graphics device: a place to make plot appear
  • 3. How does a plot get created? • Call a plotting function, i.e. plot(), xyplot(), qplot() • The plot appears on a screen device • Annotate plot, if necessary • Save it to file, for example.
  • 4. Screen and file devices • How the plot will be used? • Use screen device for quick visualisation or exploratory studies. • Plotting functions (plot(), qplot(), xyplot()) send output to the screen device by default. • If the plot will be printed, use file device instead.
  • 6. Example attach(mtcars) head(mtcars[, c("hp", "mpg")]) plot(hp, mpg) title("MPG vs. hp”) # Add a title
  • 7. Available plotting systems • Base • lattice • ggplot2
  • 8. Base plotting system • Installed by default • Quickest way to visualise your data • Plot can be further updated using additional options
  • 10. Extended example plot(disp,mpg, main = "MPG vs. HP", # Add a title type = "p", col = "grey", # Change the color of the points pch = 16, # Change the plotting symbol see help(points) cex = 1, # Change size of plotting symbol xlab = “Horse power", # Add a label on the x-axis ylab = "Miles per Gallon", # Add a label on the y-axis bty = "n", # Remove the box around the plot #asp = 1, # Change the y/x aspect ratio see help(plot) font.axis = 1, # Change axis font to bold italic col.axis = "black", # Set the color of the axis xlim = c(20,500), # Set limits on x axis ylim = c(5,50), # Set limits on y axis las=1) # Make axis labels parallel to x-axis
  • 14. • Multivariate data • Uses formulas • Single function call The Lattice Plotting System
  • 15. The Lattice Plotting System • lattice: code for producing Trellis graphics, includes functions like xyplot(), bwplot(), levelplot() • Grid: implements a different graphics system and the lattice package are on top of it
  • 16. Main plot functions • xyplot(): scatterplots • bwplot(): boxplots • histogram(): histograms • stripplot(): a boxplot points • dotplot(): plot dots on "violin strings" • splom(): scatterplot matrix • levelplot(), contourplot(): for "image" data
  • 19. More examples • See lattice_example_1.R
  • 20. ggplot2 Plotting System • “Grammar of Graphics” by Leland Wilkinson, written by Hadley Wickham • Data must be a data frame • Uses grammar • Web site: http://ggplot2.org
  • 21. What is "Grammar of Graphics"? "In brief, the grammar tells us that a statistical graphic is a mapping from data to aesthetic attributes (colour, shape, size) of geometric objects (points, lines, bars). The plot may also contain statistical transformations of the data and is drawn on a specific coordinate system”- ggplot2 book
  • 22. qplot() and ggplot() • qplot() - works like plot() function in base plotting system data • should represent a data frame • aesthetic (size, colour, shape) and geoms (points, lines) • core of ggplot()
  • 23. qplot • qplot() - works like plot() function in base plotting system data • Should represent a data frame • aesthetic (size, colour) and geoms (points, lines) • Core of ggplot()
  • 27. Components of ggplot() • A data frame • aesthetic mapping (how data are mapped to colour and size) • geoms (points, shapes, lines) • facets • stats (binning, smoothing, quantiles) • scales (for aesthetic mapping, i.e. male=‘red’, female=‘blue’) • coordinate system Peng at al, Explanatory Data Analysis, Coursera online class
  • 28. Steps to plot with ggplot() • Plots are build up in layers • Plot the data • Overlay a summary • Add metadata and annotation Peng at al, Explanatory Data Analysis, Coursera online class
  • 29. Peng at al, Explanatory Data Analysis, Coursera online class
  • 30. Peng at al, Explanatory Data Analysis, Coursera online class Example # Basic: ggplot(mtcars, aes(hp, mpg)) + geom_point()
  • 31. Peng at al, Explanatory Data Analysis, Coursera online class Example # With color: ggplot(mtcars, aes(hp, mpg)) + geom_point(aes(color = cyl))
  • 32. Peng at al, Explanatory Data Analysis, Coursera online class Example # Boxplots: ggplot(data=mtcars, aes(hp, mpg)) + geom_boxplot(aes(as.factor(cyl)))
  • 33. Peng at al, Explanatory Data Analysis, Coursera online class Example # Faceting mtcars$cyl <- factor(mtcars$cyl) ggplot(data=mtcars, aes(hp, mpg)) + geom_point() + facet_grid(~ cyl)
  • 34. Peng at al, Explanatory Data Analysis, Coursera online class Example # Faceting mtcars$cyl <- factor(mtcars$cyl) ggplot(data=mtcars, aes(hp, mpg)) + geom_point() + facet_grid(~ cyl)
  • 35. Peng at al, Explanatory Data Analysis, Coursera online class Example ggplot(data=mtcars, aes(hp, mpg)) + geom_point() + facet_grid(~ cyl) + geom_smooth(colour = "blue", size = 1, method=lm)
  • 36. Resources • http://www.r-bloggers.com • http://revolutionanalytics.com • http://www.statmethods.net (Quick-R) • Coursera (Data Science Specialisation)