SlideShare a Scribd company logo
1 of 13
Download to read offline
Data Visualization - II
Using ggplot2, geom_density2d(), stat_density_2d(),
geom_smooth(), stat_ellipse(), scatterplot()
Rupak Roy
ggplot2(): A system for 'declaratively' creating graphics, based on "The
Grammar of Graphics". Provide the data, tell 'ggplot2' how to map variables to
aesthetics, what graphical primitives to use and it will take care of all the
details.
A ggplot comprises of 3 important components:
1. Aesthetic Mapping i.e. aes in short is used to define the data for x and y
axis, size, positions and more.
2. Geometrical Objects i.e. geoms in short are the geometrical elements that
we mark on the plot like boxplot, lines, points, polygons, bars etc.
3. Statistical Transformation: it's often useful to transform the data into
counts, bin, density etc. before plotting. Few important lists of stat():
stat_bin: for Discretizing/binning
stat_smooth: for Function Continuity
stat_density for Probability Density function (PDF)
Create Elegant Data Visualizations Using
the Grammar of Graphics
Rupak Roy
#load the data
>m<-mtcars
#set the aesthetic mapping
>p<-ggplot(m,aes(x=mpg,y=cyl)
#define the geometrical objects(geom_point) and statistical transformation(stat)
where “identity” refers no transformation
>p+geom_point(color="blue", stat = "identity")
#histogram
#aesthetic mapping
>p<-ggplot(m,aes(x=mpg))
#plot the geometrical objects
>p+geom_histogram(color="blue",fill=“green",stat="bin")
>p+geom_histogram(color="blue",fill=“green")
#will result the same because the geom already contains default stat value.
Create Elegant Data Visualizations
Using the Grammar of Graphics
#to know more about aes mappings use
>help(aes)
#for a list of geom objects use
>help(alpha) #and select ggplot2
from objects exported from other packages.
Look at the index under G for the documentation of complete geom list.
Few geom’s from the list:
etc.
Create Elegant Data Visualizations
Using the Grammar of Graphics
geom Default stat
geom_histogram bin
geom_polygon identity
geom_boxplot boxplot
geom stat
geom_point identity
geom_line identity
geom_title identity
geom
geom_abline
geom_jitter
geom_violin
>library(ggplot)
#load the dataset
>mt_cars<-mtcars
>p<-ggplot(mt_cars, aes(x=mpg,y= disp))
#now we can differentiate x=mpg and y=disp based on “cyl” directly by color
>p+geom_point(aes(color=cyl))
#adding the labels
>p+geom_point(aes(color=cyl))+xlab("mileage")
+ylab("disp")
Create Elegant Data Visualizations
Using the Grammar of Graphics
#use single color
>geom_point(colour=‘blue’)
#use color by groups
>geom_point(aes(colour=VariableName))
#for boxplots
>geom_boxplot() + scale_fill_manual(values=c("#999999", "#E69F00",
"#56B4E9"))
Or > geom_boxplot() + scale_fill_manual(values=c("red", "blue", "green"))
#for scatterplot
>geom_point()+scale_color_manual(values=c("red", "blue", "green"))
#using RColorBrewer package >library(RColorBrewer)
>geom_point+scale_fill_brewer(palette=“Set2") #for boxplot
>geom_point+scale_color_brewer(palette=“Set3") #for scatterplot
>display.brewer.all() #to display all the available palette list of RColorBrewer
Change default colour: ggplot()
#-----------change colors to continuous colors i.e. gradient colors------------------------#
#for scatterplot
>p+geom_point(aes(color=cyl),alpha=0.3)
+xlab("mileage") +ylab("disp")+
scale_color_gradient(low="blue", high="red")
#for histogram
>p+geom_histogram(aes(fill= ..count..))+scale_fill_gradient(low="blue",
high="red")
Change default colour:: ggplot()
Rupak Roy
#add a regression line(is the line that best fits the trend of a given data, such that
the overall distance from the line to the points (variable values) plotted on a graph
is the smallest.
#for continuous variable
>p+geom_point(aes(color=cyl))+xlab("mileage")
+ylab("disp")+geom_smooth(method = lm)
+scale_color_gradient(low="blue", high="red")
#for discrete variable
>mt_cars$cyl<-as.factor(mt_cars$cyl)
>plm<- ggplot(mt_cars, aes(x=wt,y= mpg))
>plm+geom_point(aes(color=cyl))+xlab(“weight")
+ylab(“mpg")+geom_smooth(method = lm,
se=FALSE, fullrange=TRUE)
+scale_fill_gradient(low="blue", high=“red”)
regression line:: ggplot()
#alternative way but with separate regression lines for each cyl
> ggplot(mt_cars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm,aes(fill=cyl))
regression line::ggplot()
Rupak Roy
#scatterplots with 2d density
>p<-ggplot(mt_cars, aes(x=mpg,y= disp))
>p+geom_point()+geom_density2d()
#adding a rug plot (It helps to add short lines to
represent points for the existing plot to
illuminate information that sometimes remains
unseen)
>p+geom_point()+geom_density2d()+geom_rug()
Alternative to 2d density is stat_density_2d()
>p + stat_density_2d(aes(fill = ..level..),
geom="polygon")+scale_fill_gradient(low=
"blue", high="red")+geom_rug()
Scatter plots with 2d density
#one ellipse for all variables
>p+geom_point()+geom_rug()+stat_ellipse()
#by group
>p<-ggplot(mt_cars, aes(x=mpg,y= disp,
color= disp >280))
>p+geom_point()+stat_ellipse()
#increase the accuracy of ellipses with "t", "norm", "euclid“
>p+geom_point()+stat_ellipse(type = "norm")
Scatter plots with ellipses
#another easy way to create scatter plot by using scatterplot() from “car”
package
>install.packages("car")
>library(car)
>scatterplot(mpg ~ qsec | cyl, data=mtcars)
car::scatterplot()
Rupak Roy
Next:
We will learn how to plot multiple groups using facet,
heatmaps, geom_density and more.
Data Visualization
Rupak Roy

More Related Content

What's hot

imager package in R and examples..
imager package in R and examples..imager package in R and examples..
imager package in R and examples..Dr. Volkan OBAN
 
Quantum espresso G Vector distributon
Quantum espresso G Vector distributonQuantum espresso G Vector distributon
Quantum espresso G Vector distributonEric Pascolo
 
ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.Dr. Volkan OBAN
 
Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.Dr. Volkan OBAN
 
Q plot tutorial
Q plot tutorialQ plot tutorial
Q plot tutorialAbhik Seal
 
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...Dr. Volkan OBAN
 
7.3 power functions and function operations
7.3 power functions and function operations7.3 power functions and function operations
7.3 power functions and function operationshisema01
 
Move your data (Hans Rosling style) with googleVis + 1 line of R code
Move your data (Hans Rosling style) with googleVis + 1 line of R codeMove your data (Hans Rosling style) with googleVis + 1 line of R code
Move your data (Hans Rosling style) with googleVis + 1 line of R codeJeffrey Breen
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONTomomi Imura
 
Pytorch and Machine Learning for the Math Impaired
Pytorch and Machine Learning for the Math ImpairedPytorch and Machine Learning for the Math Impaired
Pytorch and Machine Learning for the Math ImpairedTyrel Denison
 
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 analysisVyacheslav Arbuzov
 
Visualising Big Data
Visualising Big DataVisualising Big Data
Visualising Big DataAmit Kapoor
 
treemap package in R and examples.
treemap package in R and examples.treemap package in R and examples.
treemap package in R and examples.Dr. Volkan OBAN
 
1 seaborn introduction
1 seaborn introduction 1 seaborn introduction
1 seaborn introduction YuleiLi3
 

What's hot (20)

imager package in R and examples..
imager package in R and examples..imager package in R and examples..
imager package in R and examples..
 
Quantum espresso G Vector distributon
Quantum espresso G Vector distributonQuantum espresso G Vector distributon
Quantum espresso G Vector distributon
 
CLUSTERGRAM
CLUSTERGRAMCLUSTERGRAM
CLUSTERGRAM
 
ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.ggplot2 extensions-ggtree.
ggplot2 extensions-ggtree.
 
R
RR
R
 
Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.
 
Basic Calculus in R.
Basic Calculus in R. Basic Calculus in R.
Basic Calculus in R.
 
Q plot tutorial
Q plot tutorialQ plot tutorial
Q plot tutorial
 
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
 
7.3 power functions and function operations
7.3 power functions and function operations7.3 power functions and function operations
7.3 power functions and function operations
 
Move your data (Hans Rosling style) with googleVis + 1 line of R code
Move your data (Hans Rosling style) with googleVis + 1 line of R codeMove your data (Hans Rosling style) with googleVis + 1 line of R code
Move your data (Hans Rosling style) with googleVis + 1 line of R code
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
 
Scaling up data science applications
Scaling up data science applicationsScaling up data science applications
Scaling up data science applications
 
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
 
Pytorch and Machine Learning for the Math Impaired
Pytorch and Machine Learning for the Math ImpairedPytorch and Machine Learning for the Math Impaired
Pytorch and Machine Learning for the Math Impaired
 
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
 
Visualising Big Data
Visualising Big DataVisualising Big Data
Visualising Big Data
 
Googlevis examples
Googlevis examplesGooglevis examples
Googlevis examples
 
treemap package in R and examples.
treemap package in R and examples.treemap package in R and examples.
treemap package in R and examples.
 
1 seaborn introduction
1 seaborn introduction 1 seaborn introduction
1 seaborn introduction
 

Similar to Create Elegant Data Visualizations Using the Grammar of Graphics

Tech talk ggplot2
Tech talk   ggplot2Tech talk   ggplot2
Tech talk ggplot2jalle6
 
Presentation: Plotting Systems in R
Presentation: Plotting Systems in RPresentation: Plotting Systems in R
Presentation: Plotting Systems in RIlya Zhbannikov
 
ggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphicsggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality GraphicsClaus Wilke
 
Data visualization-2.1
Data visualization-2.1Data visualization-2.1
Data visualization-2.1RenukaRajmohan
 
Dem 7263 fall 2015 Spatial GLM's
Dem 7263 fall 2015 Spatial GLM'sDem 7263 fall 2015 Spatial GLM's
Dem 7263 fall 2015 Spatial GLM'sCorey Sparks
 
Data Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdfData Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdfCarlosTrujillo199971
 
Exploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience SpecialisationExploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience SpecialisationWesley Goi
 
Computer graphics
Computer graphicsComputer graphics
Computer graphicsamitsarda3
 
computer graphics-C/C++-dancingdollcode
computer graphics-C/C++-dancingdollcodecomputer graphics-C/C++-dancingdollcode
computer graphics-C/C++-dancingdollcodeBhavya Chawla
 
The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.5.3 book - Part 58 of 184The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.5.3 book - Part 58 of 184Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184Mahmoud Samir Fayed
 
R scatter plots
R scatter plotsR scatter plots
R scatter plotsAbhik Seal
 
C Graphics Functions
C Graphics FunctionsC Graphics Functions
C Graphics FunctionsSHAKOOR AB
 

Similar to Create Elegant Data Visualizations Using the Grammar of Graphics (20)

Tech talk ggplot2
Tech talk   ggplot2Tech talk   ggplot2
Tech talk ggplot2
 
Presentation: Plotting Systems in R
Presentation: Plotting Systems in RPresentation: Plotting Systems in R
Presentation: Plotting Systems in R
 
Python grass
Python grassPython grass
Python grass
 
Pycon2011
Pycon2011Pycon2011
Pycon2011
 
Survey Demo
Survey DemoSurvey Demo
Survey Demo
 
data-visualization.pdf
data-visualization.pdfdata-visualization.pdf
data-visualization.pdf
 
RBootcamp Day 4
RBootcamp Day 4RBootcamp Day 4
RBootcamp Day 4
 
ggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphicsggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphics
 
Data visualization-2.1
Data visualization-2.1Data visualization-2.1
Data visualization-2.1
 
Dem 7263 fall 2015 Spatial GLM's
Dem 7263 fall 2015 Spatial GLM'sDem 7263 fall 2015 Spatial GLM's
Dem 7263 fall 2015 Spatial GLM's
 
Data Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdfData Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdf
 
Exploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience SpecialisationExploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience Specialisation
 
Data visualization
Data visualizationData visualization
Data visualization
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Primitives
PrimitivesPrimitives
Primitives
 
computer graphics-C/C++-dancingdollcode
computer graphics-C/C++-dancingdollcodecomputer graphics-C/C++-dancingdollcode
computer graphics-C/C++-dancingdollcode
 
The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.5.3 book - Part 58 of 184The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.5.3 book - Part 58 of 184
 
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184
 
R scatter plots
R scatter plotsR scatter plots
R scatter plots
 
C Graphics Functions
C Graphics FunctionsC Graphics Functions
C Graphics Functions
 

More from Rupak Roy

Hierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPHierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPRupak Roy
 
Clustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPClustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPRupak Roy
 
Network Analysis - NLP
Network Analysis  - NLPNetwork Analysis  - NLP
Network Analysis - NLPRupak Roy
 
Topic Modeling - NLP
Topic Modeling - NLPTopic Modeling - NLP
Topic Modeling - NLPRupak Roy
 
Sentiment Analysis Practical Steps
Sentiment Analysis Practical StepsSentiment Analysis Practical Steps
Sentiment Analysis Practical StepsRupak Roy
 
NLP - Sentiment Analysis
NLP - Sentiment AnalysisNLP - Sentiment Analysis
NLP - Sentiment AnalysisRupak Roy
 
Text Mining using Regular Expressions
Text Mining using Regular ExpressionsText Mining using Regular Expressions
Text Mining using Regular ExpressionsRupak Roy
 
Introduction to Text Mining
Introduction to Text Mining Introduction to Text Mining
Introduction to Text Mining Rupak Roy
 
Apache Hbase Architecture
Apache Hbase ArchitectureApache Hbase Architecture
Apache Hbase ArchitectureRupak Roy
 
Introduction to Hbase
Introduction to Hbase Introduction to Hbase
Introduction to Hbase Rupak Roy
 
Apache Hive Table Partition and HQL
Apache Hive Table Partition and HQLApache Hive Table Partition and HQL
Apache Hive Table Partition and HQLRupak Roy
 
Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export Rupak Roy
 
Introductive to Hive
Introductive to Hive Introductive to Hive
Introductive to Hive Rupak Roy
 
Scoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSScoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSRupak Roy
 
Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode Rupak Roy
 
Introduction to scoop and its functions
Introduction to scoop and its functionsIntroduction to scoop and its functions
Introduction to scoop and its functionsRupak Roy
 
Introduction to Flume
Introduction to FlumeIntroduction to Flume
Introduction to FlumeRupak Roy
 
Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Rupak Roy
 
Passing Parameters using File and Command Line
Passing Parameters using File and Command LinePassing Parameters using File and Command Line
Passing Parameters using File and Command LineRupak Roy
 
Apache PIG Relational Operations
Apache PIG Relational Operations Apache PIG Relational Operations
Apache PIG Relational Operations Rupak Roy
 

More from Rupak Roy (20)

Hierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPHierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLP
 
Clustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPClustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLP
 
Network Analysis - NLP
Network Analysis  - NLPNetwork Analysis  - NLP
Network Analysis - NLP
 
Topic Modeling - NLP
Topic Modeling - NLPTopic Modeling - NLP
Topic Modeling - NLP
 
Sentiment Analysis Practical Steps
Sentiment Analysis Practical StepsSentiment Analysis Practical Steps
Sentiment Analysis Practical Steps
 
NLP - Sentiment Analysis
NLP - Sentiment AnalysisNLP - Sentiment Analysis
NLP - Sentiment Analysis
 
Text Mining using Regular Expressions
Text Mining using Regular ExpressionsText Mining using Regular Expressions
Text Mining using Regular Expressions
 
Introduction to Text Mining
Introduction to Text Mining Introduction to Text Mining
Introduction to Text Mining
 
Apache Hbase Architecture
Apache Hbase ArchitectureApache Hbase Architecture
Apache Hbase Architecture
 
Introduction to Hbase
Introduction to Hbase Introduction to Hbase
Introduction to Hbase
 
Apache Hive Table Partition and HQL
Apache Hive Table Partition and HQLApache Hive Table Partition and HQL
Apache Hive Table Partition and HQL
 
Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export
 
Introductive to Hive
Introductive to Hive Introductive to Hive
Introductive to Hive
 
Scoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSScoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMS
 
Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode
 
Introduction to scoop and its functions
Introduction to scoop and its functionsIntroduction to scoop and its functions
Introduction to scoop and its functions
 
Introduction to Flume
Introduction to FlumeIntroduction to Flume
Introduction to Flume
 
Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Apache Pig Relational Operators - II
Apache Pig Relational Operators - II
 
Passing Parameters using File and Command Line
Passing Parameters using File and Command LinePassing Parameters using File and Command Line
Passing Parameters using File and Command Line
 
Apache PIG Relational Operations
Apache PIG Relational Operations Apache PIG Relational Operations
Apache PIG Relational Operations
 

Recently uploaded

Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Recently uploaded (20)

Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 

Create Elegant Data Visualizations Using the Grammar of Graphics

  • 1. Data Visualization - II Using ggplot2, geom_density2d(), stat_density_2d(), geom_smooth(), stat_ellipse(), scatterplot() Rupak Roy
  • 2. ggplot2(): A system for 'declaratively' creating graphics, based on "The Grammar of Graphics". Provide the data, tell 'ggplot2' how to map variables to aesthetics, what graphical primitives to use and it will take care of all the details. A ggplot comprises of 3 important components: 1. Aesthetic Mapping i.e. aes in short is used to define the data for x and y axis, size, positions and more. 2. Geometrical Objects i.e. geoms in short are the geometrical elements that we mark on the plot like boxplot, lines, points, polygons, bars etc. 3. Statistical Transformation: it's often useful to transform the data into counts, bin, density etc. before plotting. Few important lists of stat(): stat_bin: for Discretizing/binning stat_smooth: for Function Continuity stat_density for Probability Density function (PDF) Create Elegant Data Visualizations Using the Grammar of Graphics Rupak Roy
  • 3. #load the data >m<-mtcars #set the aesthetic mapping >p<-ggplot(m,aes(x=mpg,y=cyl) #define the geometrical objects(geom_point) and statistical transformation(stat) where “identity” refers no transformation >p+geom_point(color="blue", stat = "identity") #histogram #aesthetic mapping >p<-ggplot(m,aes(x=mpg)) #plot the geometrical objects >p+geom_histogram(color="blue",fill=“green",stat="bin") >p+geom_histogram(color="blue",fill=“green") #will result the same because the geom already contains default stat value. Create Elegant Data Visualizations Using the Grammar of Graphics
  • 4. #to know more about aes mappings use >help(aes) #for a list of geom objects use >help(alpha) #and select ggplot2 from objects exported from other packages. Look at the index under G for the documentation of complete geom list. Few geom’s from the list: etc. Create Elegant Data Visualizations Using the Grammar of Graphics geom Default stat geom_histogram bin geom_polygon identity geom_boxplot boxplot geom stat geom_point identity geom_line identity geom_title identity geom geom_abline geom_jitter geom_violin
  • 5. >library(ggplot) #load the dataset >mt_cars<-mtcars >p<-ggplot(mt_cars, aes(x=mpg,y= disp)) #now we can differentiate x=mpg and y=disp based on “cyl” directly by color >p+geom_point(aes(color=cyl)) #adding the labels >p+geom_point(aes(color=cyl))+xlab("mileage") +ylab("disp") Create Elegant Data Visualizations Using the Grammar of Graphics
  • 6. #use single color >geom_point(colour=‘blue’) #use color by groups >geom_point(aes(colour=VariableName)) #for boxplots >geom_boxplot() + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9")) Or > geom_boxplot() + scale_fill_manual(values=c("red", "blue", "green")) #for scatterplot >geom_point()+scale_color_manual(values=c("red", "blue", "green")) #using RColorBrewer package >library(RColorBrewer) >geom_point+scale_fill_brewer(palette=“Set2") #for boxplot >geom_point+scale_color_brewer(palette=“Set3") #for scatterplot >display.brewer.all() #to display all the available palette list of RColorBrewer Change default colour: ggplot()
  • 7. #-----------change colors to continuous colors i.e. gradient colors------------------------# #for scatterplot >p+geom_point(aes(color=cyl),alpha=0.3) +xlab("mileage") +ylab("disp")+ scale_color_gradient(low="blue", high="red") #for histogram >p+geom_histogram(aes(fill= ..count..))+scale_fill_gradient(low="blue", high="red") Change default colour:: ggplot() Rupak Roy
  • 8. #add a regression line(is the line that best fits the trend of a given data, such that the overall distance from the line to the points (variable values) plotted on a graph is the smallest. #for continuous variable >p+geom_point(aes(color=cyl))+xlab("mileage") +ylab("disp")+geom_smooth(method = lm) +scale_color_gradient(low="blue", high="red") #for discrete variable >mt_cars$cyl<-as.factor(mt_cars$cyl) >plm<- ggplot(mt_cars, aes(x=wt,y= mpg)) >plm+geom_point(aes(color=cyl))+xlab(“weight") +ylab(“mpg")+geom_smooth(method = lm, se=FALSE, fullrange=TRUE) +scale_fill_gradient(low="blue", high=“red”) regression line:: ggplot()
  • 9. #alternative way but with separate regression lines for each cyl > ggplot(mt_cars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point() + geom_smooth(method=lm,aes(fill=cyl)) regression line::ggplot() Rupak Roy
  • 10. #scatterplots with 2d density >p<-ggplot(mt_cars, aes(x=mpg,y= disp)) >p+geom_point()+geom_density2d() #adding a rug plot (It helps to add short lines to represent points for the existing plot to illuminate information that sometimes remains unseen) >p+geom_point()+geom_density2d()+geom_rug() Alternative to 2d density is stat_density_2d() >p + stat_density_2d(aes(fill = ..level..), geom="polygon")+scale_fill_gradient(low= "blue", high="red")+geom_rug() Scatter plots with 2d density
  • 11. #one ellipse for all variables >p+geom_point()+geom_rug()+stat_ellipse() #by group >p<-ggplot(mt_cars, aes(x=mpg,y= disp, color= disp >280)) >p+geom_point()+stat_ellipse() #increase the accuracy of ellipses with "t", "norm", "euclid“ >p+geom_point()+stat_ellipse(type = "norm") Scatter plots with ellipses
  • 12. #another easy way to create scatter plot by using scatterplot() from “car” package >install.packages("car") >library(car) >scatterplot(mpg ~ qsec | cyl, data=mtcars) car::scatterplot() Rupak Roy
  • 13. Next: We will learn how to plot multiple groups using facet, heatmaps, geom_density and more. Data Visualization Rupak Roy