SlideShare a Scribd company logo
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

PDF
Data Visualization using matplotlib
PDF
Py lecture5 python plots
PPT
Matlab graphics
PPTX
Graph Plots in Matlab
PPTX
Matlab Visualizing Data
PDF
Javascript Array map method
PPTX
Python programing
PDF
Data visualization with multiple groups using ggplot2
Data Visualization using matplotlib
Py lecture5 python plots
Matlab graphics
Graph Plots in Matlab
Matlab Visualizing Data
Javascript Array map method
Python programing
Data visualization with multiple groups using ggplot2

What's hot (19)

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

Similar to Presentation: Plotting Systems in R (20)

PPTX
Tech talk ggplot2
PPTX
Data visualization using R
PDF
R programming for data science
PDF
Introduction to R Graphics with ggplot2
PPT
R graphics
PDF
M4_DAR_part1. module part 4 analystics with r
PPT
A Survey Of R Graphics
PDF
Data Visualization in R (Graph, Trend, etc)
PPTX
Exploratory data analysis using r
PDF
Lectures r-graphics
DOCX
Week-3 – System RSupplemental material1Recap •.docx
PDF
Introduction to R Short course Fall 2016
PPTX
An implementation of the grammar of graphics: ggplot
PDF
Data Visualization With R
PPTX
R Graphics
PPTX
PPT
Tools for research plotting
PPTX
UNIT_4_data visualization.pptx
PPT
Tools for research plotting
PPTX
Introduction to GGVIS Visualization
Tech talk ggplot2
Data visualization using R
R programming for data science
Introduction to R Graphics with ggplot2
R graphics
M4_DAR_part1. module part 4 analystics with r
A Survey Of R Graphics
Data Visualization in R (Graph, Trend, etc)
Exploratory data analysis using r
Lectures r-graphics
Week-3 – System RSupplemental material1Recap •.docx
Introduction to R Short course Fall 2016
An implementation of the grammar of graphics: ggplot
Data Visualization With R
R Graphics
Tools for research plotting
UNIT_4_data visualization.pptx
Tools for research plotting
Introduction to GGVIS Visualization
Ad

Recently uploaded (20)

PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
Supervised vs unsupervised machine learning algorithms
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
1_Introduction to advance data techniques.pptx
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
Introduction to Knowledge Engineering Part 1
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PDF
annual-report-2024-2025 original latest.
PDF
Fluorescence-microscope_Botany_detailed content
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PDF
Foundation of Data Science unit number two notes
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Supervised vs unsupervised machine learning algorithms
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
1_Introduction to advance data techniques.pptx
IB Computer Science - Internal Assessment.pptx
Introduction to Knowledge Engineering Part 1
Reliability_Chapter_ presentation 1221.5784
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
annual-report-2024-2025 original latest.
Fluorescence-microscope_Botany_detailed content
Business Ppt On Nestle.pptx huunnnhhgfvu
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Foundation of Data Science unit number two notes
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx

Presentation: Plotting Systems in R