SlideShare a Scribd company logo
Prepared by Volkan OBAN
ggplot2 Examples:
1-
students = read.csv("student.csv")
library(ggplot2)
ggplot(students, aes(x = Height)) + geom_dotplot()
2-
ggplot(students, aes(x = Sex, y = Height)) + geom_boxplot() + coord_flip()
3-
ggplot(students,aes(x =Sleep)) +geom_dotplot(dotsize=0.4)
4-
ggplot(students,aes(x =Sleep)) +geom_dotplot(dotsize=0.4) + facet_grid(Level ~.)
5-
ggplot(students, aes(x = Major, y = MilesHome)) + geom_boxplot() +
coord_fip()
qplot
6-
qplot(color, price / carat, data = diamonds, geom = "jitter")
7-
qplot(color, price / carat, data = diamonds, geom = "boxplot")
8-
qplot(carat, data = diamonds, geom = "histogram", fill = color)
9-
>library(nlme)
> data(Oxboys)
> p <- ggplot(Oxboys, aes(age, height, group = Subject)) +
+ geom_line()
> p
> p + geom_smooth(aes(group = Subject), method="lm", se = F)
10-
depth_dist<- ggplot(diamonds,aes(depth)) +xlim(58,68) depth_dist+ geom_histogram(aes(y=
..density..),binwidth= 0.1) + facet_grid(cut~ .) depth_dist+ geom_histogram(aes(fill =cut),
binwidth= 0.1, position= "fill") depth_dist+geom_freqpoly(aes(y=..density..,colour= cut),
binwidth= 0.1)
Reference: http://www.stat.wisc.edu/~larget/stat302/chap2.pdf
Reference book: ggplot2 Elegant Graphics for Data Analysis; Wickham, Hadley
11-
> library("ggplot2")
> library("ggthemes")
> ggplot(diamonds, aes(x = clarity, fill = cut)) +
geom_bar() + scale_fill_ptol() +theme_minimal()
12- > dtemp <- data.frame(months = factor(rep(substr(month.name,1,3), 4), l
evels = substr(month.name,1,3)),
city = rep(c("Tokyo", "New York", "Berlin", "London"), each = 12),
temp = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6
, -0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5,
-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0,
3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8))
> ggplot(dtemp, aes(x = months, y = temp, group = city, color = city)) +
geom_line() +
geom_point(size = 1.1) +
ggtitle("Monthly Average Temperature") +
theme_hc() +
scale_colour_hc()
> ggplot(dtemp, aes(x = months, y = temp, group = city, color = city)) +
geom_line() +
geom_point(size = 1.1) +
ggtitle("Monthly Average Temperature") +
theme_hc(bgcolor = "darkunica") +
scale_fill_hc("darkunica")
Ref: https://cran.r-project.org/web/packages/ggthemes/vignettes/ggthemes.html
13-
> ppc <- ggplot(data = diamonds, aes(x = color, y = price/carat))
> ppc + geom_boxplot(aes(fill = color))
Ref:https://www3.nd.edu/~steve/computing_with_data/11_geom_examples/ggplot_examples.html
14-
morph <- read.csv("Morph_for_Sato.csv")
names(morph) <- tolower(names(morph)) # make columns names lowercase
morph <- subset(morph, islandid == "Flor_Chrl") # take only one island morph <-
morph[,c("taxonorig", "sex", "wingl", "beakh", "ubeakl")] # only keep these
names(morph)[1] <- "taxon" morph <- data.frame(na.omit(morph)) # remove all rows
morph$taxon <- factor(morph$taxon) # remove extra remaining factor levels
morph$sex <- factor(morph$sex) # remove extra remaining factor levels
row.names(morph)
ggplot(morph, aes(taxon, wingl)) + geom_boxplot() + coord_flip()
Ref: http://seananderson.ca/ggplot2-FISH554/
15-
ggplot(morph, aes(sex, wingl)) + geom_violin() + facet_wrap(~taxon)
16.
library("dplyr")
> morph_quant <-
morph %>%
+ group_by(taxon) %>%
+ summarise(
+ l = quantile(wingl, 0.25)[[1]],
+ m = median(wingl),
+ u = quantile(wingl, 0.75)[[1]]) %>%
+ # re-order factor levels by median for plotting:
+ mutate(taxon = reorder(taxon, m, function(x) x))
>ggplot(morph_quant, aes(x = taxon, y = m, ymin = l, ymax = u)) +
geom_pointrange() + coord_flip() + ylab("Wing length") + xlab("")
Ref: http://seananderson.ca/ggplot2-FISH554/
17-
>library(ggplot2)
> plot2 <- qplot(color, price/carat, data = diamonds, geom = "boxplot", col
our = color)
> plot2
18-
> library(GGally)
> library(ggplot2)
>data(tips, package = "reshape")
> pm <- ggpairs(tips, mapping = aes(color = sex), columns = c("total_bill", "time", "tip"))
> pm
Ref: http://ggobi.github.io/ggally/docs.html#columns_and_mapping

More Related Content

DOCX
Data Visualization with R.ggplot2 and its extensions examples.
DOCX
Advanced Data Visualization Examples with R-Part II
DOCX
imager package in R and examples..
DOCX
Advanced Data Visualization in R- Somes Examples.
DOCX
Basic Calculus in R.
DOCX
ggtimeseries-->ggplot2 extensions
PDF
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
DOCX
ggplot2 extensions-ggtree.
Data Visualization with R.ggplot2 and its extensions examples.
Advanced Data Visualization Examples with R-Part II
imager package in R and examples..
Advanced Data Visualization in R- Somes Examples.
Basic Calculus in R.
ggtimeseries-->ggplot2 extensions
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
ggplot2 extensions-ggtree.

What's hot (20)

PDF
Data visualization with multiple groups using ggplot2
PDF
Data visualization using the grammar of graphics
PDF
Geo Spatial Plot using R
PPTX
DOCX
CLUSTERGRAM
PPTX
Seminar psu 20.10.2013
PDF
peRm R group. Review of packages for r for market data downloading and analysis
PPTX
Seminar PSU 10.10.2014 mme
PPTX
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
PDF
CLIM Undergraduate Workshop: (Attachment) Performing Extreme Value Analysis (...
PDF
Perm winter school 2014.01.31
PDF
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
KEY
R meets Hadoop
TXT
Script jantung copy
PDF
NumPy Refresher
TXT
Mpibhseguranca3
PDF
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
PDF
Numpy python cheat_sheet
PDF
Chapter 1 Basic Concepts
PDF
Py lecture5 python plots
Data visualization with multiple groups using ggplot2
Data visualization using the grammar of graphics
Geo Spatial Plot using R
CLUSTERGRAM
Seminar psu 20.10.2013
peRm R group. Review of packages for r for market data downloading and analysis
Seminar PSU 10.10.2014 mme
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
CLIM Undergraduate Workshop: (Attachment) Performing Extreme Value Analysis (...
Perm winter school 2014.01.31
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
R meets Hadoop
Script jantung copy
NumPy Refresher
Mpibhseguranca3
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
Numpy python cheat_sheet
Chapter 1 Basic Concepts
Py lecture5 python plots
Ad

Viewers also liked (7)

PDF
Introduction to R Graphics with ggplot2
PDF
Data Visualization With R
PPTX
Data Visualization in R
PPTX
Mini Innovation Lab: Community Foundations and Shared Data
PPTX
R and Visualization: A match made in Heaven
PDF
ggplot2用例集 入門編
PPT
Free Download Powerpoint Slides
Introduction to R Graphics with ggplot2
Data Visualization With R
Data Visualization in R
Mini Innovation Lab: Community Foundations and Shared Data
R and Visualization: A match made in Heaven
ggplot2用例集 入門編
Free Download Powerpoint Slides
Ad

Similar to R-ggplot2 package Examples (20)

PDF
Data Visualization with ggplot2.pdf
PDF
Data visualization-2.1
PDF
VISIALIZACION DE DATA.pdf
PPTX
Data and donuts: Data Visualization using R
PDF
data-visualization.pdf
PDF
Ggplot2 cheatsheet-2.1
PDF
PDF
Ggplot2 ch2
PDF
R workshop iii -- 3 hours to learn ggplot2 series
PDF
Download full ebook of Datacamp Ggplot2 Cheatsheet Itebooks instant download pdf
PDF
Intro to ggplot2 - Sheffield R Users Group, Feb 2015
PDF
ggplot2: An Extensible Platform for Publication-quality Graphics
PPTX
Exploratory Analysis Part1 Coursera DataScience Specialisation
PPTX
Tech talk ggplot2
PPTX
RBootcamp Day 4
PPTX
Visualization_Data with ggplot2_Day 2.pptx
PPTX
ggplotcourse.pptx
PDF
Q plot tutorial
PDF
Introduction to R Short course Fall 2016
PDF
Broom: Converting Statistical Models to Tidy Data Frames
Data Visualization with ggplot2.pdf
Data visualization-2.1
VISIALIZACION DE DATA.pdf
Data and donuts: Data Visualization using R
data-visualization.pdf
Ggplot2 cheatsheet-2.1
Ggplot2 ch2
R workshop iii -- 3 hours to learn ggplot2 series
Download full ebook of Datacamp Ggplot2 Cheatsheet Itebooks instant download pdf
Intro to ggplot2 - Sheffield R Users Group, Feb 2015
ggplot2: An Extensible Platform for Publication-quality Graphics
Exploratory Analysis Part1 Coursera DataScience Specialisation
Tech talk ggplot2
RBootcamp Day 4
Visualization_Data with ggplot2_Day 2.pptx
ggplotcourse.pptx
Q plot tutorial
Introduction to R Short course Fall 2016
Broom: Converting Statistical Models to Tidy Data Frames

More from Dr. Volkan OBAN (20)

PDF
Conference Paper:IMAGE PROCESSING AND OBJECT DETECTION APPLICATION: INSURANCE...
PDF
Covid19py Python Package - Example
PDF
Object detection with Python
PDF
Python - Rastgele Orman(Random Forest) Parametreleri
DOCX
Linear Programming wi̇th R - Examples
DOCX
"optrees" package in R and examples.(optrees:finds optimal trees in weighted ...
DOCX
k-means Clustering in Python
DOCX
Naive Bayes Example using R
DOCX
R forecasting Example
DOCX
k-means Clustering and Custergram with R
PDF
Data Science and its Relationship to Big Data and Data-Driven Decision Making
PDF
Scikit-learn Cheatsheet-Python
PDF
Python Pandas for Data Science cheatsheet
PDF
Pandas,scipy,numpy cheatsheet
PPTX
ReporteRs package in R. forming powerpoint documents-an example
PPTX
ReporteRs package in R. forming powerpoint documents-an example
DOCX
R Machine Learning packages( generally used)
DOCX
treemap package in R and examples.
DOCX
Mosaic plot in R.
PDF
R-Data table Cheat Sheet
Conference Paper:IMAGE PROCESSING AND OBJECT DETECTION APPLICATION: INSURANCE...
Covid19py Python Package - Example
Object detection with Python
Python - Rastgele Orman(Random Forest) Parametreleri
Linear Programming wi̇th R - Examples
"optrees" package in R and examples.(optrees:finds optimal trees in weighted ...
k-means Clustering in Python
Naive Bayes Example using R
R forecasting Example
k-means Clustering and Custergram with R
Data Science and its Relationship to Big Data and Data-Driven Decision Making
Scikit-learn Cheatsheet-Python
Python Pandas for Data Science cheatsheet
Pandas,scipy,numpy cheatsheet
ReporteRs package in R. forming powerpoint documents-an example
ReporteRs package in R. forming powerpoint documents-an example
R Machine Learning packages( generally used)
treemap package in R and examples.
Mosaic plot in R.
R-Data table Cheat Sheet

Recently uploaded (20)

PPTX
Understanding Prototyping in Design and Development
PDF
Digital Infrastructure – Powering the Connected Age
PPTX
Major-Components-ofNKJNNKNKNKNKronment.pptx
PDF
Data Science Trends & Career Guide---ppt
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PDF
Chad Readey - An Independent Thinker
PPTX
artificial intelligence deeplearning-200712115616.pptx
PDF
Nashik East side PPT 01-08-25. vvvhvjvvvhvh
PDF
Company Profile 2023 PT. ZEKON INDONESIA.pdf
PDF
Research about a FoodFolio app for personalized dietary tracking and health o...
PPTX
Global journeys: estimating international migration
PPT
Performance Implementation Review powerpoint
PPTX
batch data Retailer Data management Project.pptx
PPT
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
PPTX
办理新西兰毕业证(Lincoln毕业证书)林肯大学毕业证毕业 证
PPTX
LESSON-1-NATURE-OF-MATHEMATICS.pptx patterns
PPTX
CL11_CH20_-LOCOMOTION-AND-MOVEMENT-Autosaved.pptx
PDF
Data Analyst Certificate Programs for Beginners | IABAC
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PDF
Taxes Foundatisdcsdcsdon Certificate.pdf
Understanding Prototyping in Design and Development
Digital Infrastructure – Powering the Connected Age
Major-Components-ofNKJNNKNKNKNKronment.pptx
Data Science Trends & Career Guide---ppt
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Chad Readey - An Independent Thinker
artificial intelligence deeplearning-200712115616.pptx
Nashik East side PPT 01-08-25. vvvhvjvvvhvh
Company Profile 2023 PT. ZEKON INDONESIA.pdf
Research about a FoodFolio app for personalized dietary tracking and health o...
Global journeys: estimating international migration
Performance Implementation Review powerpoint
batch data Retailer Data management Project.pptx
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
办理新西兰毕业证(Lincoln毕业证书)林肯大学毕业证毕业 证
LESSON-1-NATURE-OF-MATHEMATICS.pptx patterns
CL11_CH20_-LOCOMOTION-AND-MOVEMENT-Autosaved.pptx
Data Analyst Certificate Programs for Beginners | IABAC
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Taxes Foundatisdcsdcsdon Certificate.pdf

R-ggplot2 package Examples

  • 1. Prepared by Volkan OBAN ggplot2 Examples: 1- students = read.csv("student.csv") library(ggplot2) ggplot(students, aes(x = Height)) + geom_dotplot()
  • 2. 2- ggplot(students, aes(x = Sex, y = Height)) + geom_boxplot() + coord_flip()
  • 5. 5- ggplot(students, aes(x = Major, y = MilesHome)) + geom_boxplot() + coord_fip()
  • 6. qplot 6- qplot(color, price / carat, data = diamonds, geom = "jitter")
  • 7. 7- qplot(color, price / carat, data = diamonds, geom = "boxplot")
  • 8. 8- qplot(carat, data = diamonds, geom = "histogram", fill = color)
  • 9. 9- >library(nlme) > data(Oxboys) > p <- ggplot(Oxboys, aes(age, height, group = Subject)) + + geom_line() > p > p + geom_smooth(aes(group = Subject), method="lm", se = F)
  • 10. 10- depth_dist<- ggplot(diamonds,aes(depth)) +xlim(58,68) depth_dist+ geom_histogram(aes(y= ..density..),binwidth= 0.1) + facet_grid(cut~ .) depth_dist+ geom_histogram(aes(fill =cut), binwidth= 0.1, position= "fill") depth_dist+geom_freqpoly(aes(y=..density..,colour= cut), binwidth= 0.1) Reference: http://www.stat.wisc.edu/~larget/stat302/chap2.pdf Reference book: ggplot2 Elegant Graphics for Data Analysis; Wickham, Hadley
  • 11. 11- > library("ggplot2") > library("ggthemes") > ggplot(diamonds, aes(x = clarity, fill = cut)) + geom_bar() + scale_fill_ptol() +theme_minimal()
  • 12. 12- > dtemp <- data.frame(months = factor(rep(substr(month.name,1,3), 4), l evels = substr(month.name,1,3)), city = rep(c("Tokyo", "New York", "Berlin", "London"), each = 12), temp = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6 , -0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5, -0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0, 3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8)) > ggplot(dtemp, aes(x = months, y = temp, group = city, color = city)) + geom_line() + geom_point(size = 1.1) + ggtitle("Monthly Average Temperature") + theme_hc() + scale_colour_hc() > ggplot(dtemp, aes(x = months, y = temp, group = city, color = city)) + geom_line() + geom_point(size = 1.1) + ggtitle("Monthly Average Temperature") + theme_hc(bgcolor = "darkunica") + scale_fill_hc("darkunica") Ref: https://cran.r-project.org/web/packages/ggthemes/vignettes/ggthemes.html
  • 13. 13- > ppc <- ggplot(data = diamonds, aes(x = color, y = price/carat)) > ppc + geom_boxplot(aes(fill = color)) Ref:https://www3.nd.edu/~steve/computing_with_data/11_geom_examples/ggplot_examples.html
  • 14. 14- morph <- read.csv("Morph_for_Sato.csv") names(morph) <- tolower(names(morph)) # make columns names lowercase morph <- subset(morph, islandid == "Flor_Chrl") # take only one island morph <- morph[,c("taxonorig", "sex", "wingl", "beakh", "ubeakl")] # only keep these names(morph)[1] <- "taxon" morph <- data.frame(na.omit(morph)) # remove all rows morph$taxon <- factor(morph$taxon) # remove extra remaining factor levels morph$sex <- factor(morph$sex) # remove extra remaining factor levels row.names(morph) ggplot(morph, aes(taxon, wingl)) + geom_boxplot() + coord_flip() Ref: http://seananderson.ca/ggplot2-FISH554/
  • 15. 15- ggplot(morph, aes(sex, wingl)) + geom_violin() + facet_wrap(~taxon)
  • 16. 16. library("dplyr") > morph_quant <- morph %>% + group_by(taxon) %>% + summarise( + l = quantile(wingl, 0.25)[[1]], + m = median(wingl), + u = quantile(wingl, 0.75)[[1]]) %>% + # re-order factor levels by median for plotting: + mutate(taxon = reorder(taxon, m, function(x) x)) >ggplot(morph_quant, aes(x = taxon, y = m, ymin = l, ymax = u)) + geom_pointrange() + coord_flip() + ylab("Wing length") + xlab("") Ref: http://seananderson.ca/ggplot2-FISH554/
  • 17. 17- >library(ggplot2) > plot2 <- qplot(color, price/carat, data = diamonds, geom = "boxplot", col our = color) > plot2
  • 18. 18- > library(GGally) > library(ggplot2) >data(tips, package = "reshape") > pm <- ggpairs(tips, mapping = aes(color = sex), columns = c("total_bill", "time", "tip")) > pm Ref: http://ggobi.github.io/ggally/docs.html#columns_and_mapping