flexible, beautiful, customizable graphszief0002.github.io/computing-club/notes/2015_s/2015... · r...
TRANSCRIPT
![Page 1: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/1.jpg)
Flexible, Beautiful,Customizable GraphsUnderstanding ggplot2’s Grammar
Kyle Nickodem
Monday, February 2nd, 2015
Flexible, Beautiful, Customizable Graphs
1 of 22
![Page 2: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/2.jpg)
Take Home Points
Become aware of the various components thatcomprise a graph
1.
Understand how ggplot2 uses these components toconstruct a plot
2.
2/22
Flexible, Beautiful, Customizable Graphs
2 of 22
![Page 3: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/3.jpg)
What is this graphic trying to tellus?
3/22
Flexible, Beautiful, Customizable Graphs
3 of 22
![Page 4: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/4.jpg)
The Grammar of Graphics
Why is it necessary to understand the grammar?
ggplot2 operates using this grammar1.
It provides us with a process to think about thestructure that underlies statistical grapics
2.
4/22
Flexible, Beautiful, Customizable Graphs
4 of 22
![Page 5: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/5.jpg)
Data and asthetic mapping
Geometric objects
Scales and coordinate system
Plot annotations and themes
·
·
·
·
5/22
Flexible, Beautiful, Customizable Graphs
5 of 22
![Page 6: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/6.jpg)
6/22
Flexible, Beautiful, Customizable Graphs
6 of 22
![Page 7: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/7.jpg)
How does this work in ggplot2?
library(ggplot2)head(diamonds)
## carat cut color clarity depth table price x y z## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31## 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
7/22
Flexible, Beautiful, Customizable Graphs
7 of 22
![Page 8: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/8.jpg)
ggplot(data=diamonds, aes(x = x, y = carat)) + geom_point()
8/22
Flexible, Beautiful, Customizable Graphs
8 of 22
![Page 9: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/9.jpg)
ggplot(data=diamonds, aes(x = x, y = carat)) + geom_point()
ggplot() + layer(data = diamonds, mapping = aes(x = x, y = carat), geom = "point", stat = "identity", pos = "identity") scale_x_continuous() + scale_y_continuous() + coord_cartesian() + theme()
ggplot(diamonds, aes(x,carat)) + geom_point()
9/22
Flexible, Beautiful, Customizable Graphs
9 of 22
![Page 10: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/10.jpg)
Quiz Time
What will this plot look like?
ggplot(data = economics, aes(x = date, y = pop)) + geom_line()
10/22
Flexible, Beautiful, Customizable Graphs
10 of 22
![Page 11: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/11.jpg)
ggplot(data = economics, aes(x = date, y = pop)) + geom_line()
11/22
Flexible, Beautiful, Customizable Graphs
11 of 22
![Page 12: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/12.jpg)
Question #2
ggplot(data = diamonds, aes(x = price)) + geom_histogram()
12/22
Flexible, Beautiful, Customizable Graphs
12 of 22
![Page 13: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/13.jpg)
ggplot(data = diamonds, aes(x = price)) + geom_histogram()
13/22
Flexible, Beautiful, Customizable Graphs
13 of 22
![Page 14: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/14.jpg)
Add multiple geometric objects
ggplot(data = diamonds, aes(x = price)) + geom_histogram(aes(y = ..density..)) + geom_density(color = "red")
14/22
Flexible, Beautiful, Customizable Graphs
14 of 22
![Page 15: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/15.jpg)
Frequency of diamond clarity bycut?
ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar()
15/22
Flexible, Beautiful, Customizable Graphs
15 of 22
![Page 16: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/16.jpg)
Change color scheme
library(RColorBrewer)ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar() + scale_fill_brewer()
16/22
Flexible, Beautiful, Customizable Graphs
16 of 22
![Page 17: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/17.jpg)
Flip the coordinate grid
ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar() + scale_fill_brewer() + coord_flip()
17/22
Flexible, Beautiful, Customizable Graphs
17 of 22
![Page 18: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/18.jpg)
Dodged bar chart
ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar(position = "dodge") + scale_fill_brewer()
18/22
Flexible, Beautiful, Customizable Graphs
18 of 22
![Page 19: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/19.jpg)
Facet
ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar() + scale_fill_brewer() + facet_wrap(~cut)
19/22
Flexible, Beautiful, Customizable Graphs
19 of 22
![Page 20: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/20.jpg)
Alter the theme
ggplot(data=diamonds, aes(x = clarity, fill = cut)) + geom_bar() + scale_fill_brewer() + facet_wrap(~cut) + theme_bw()
20/22
Flexible, Beautiful, Customizable Graphs
20 of 22
![Page 21: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/21.jpg)
Resources
Hadley's ggplot2 documentation - docs.ggplot2.org
ZevRoss ggplot2 cheatsheet
R Graphics Cookbook
R Color Brewer
Wilkinson, L. (2006). The grammar of graphics. Springer -Available for free from through the UM Library portal.
·
·
·
·
·
21/22
Flexible, Beautiful, Customizable Graphs
21 of 22
![Page 22: Flexible, Beautiful, Customizable Graphszief0002.github.io/Computing-Club/notes/2015_S/2015... · R Graphics Cookbook R Color Brewer Wilkinson, L. (2006). The grammar of graphics](https://reader035.vdocuments.us/reader035/viewer/2022071019/5fd2b9848f126c48e96aaf1b/html5/thumbnails/22.jpg)
Hadley's favorite pie chart
ggplot(df, aes(x = "", y = value, fill = variable)) + geom_bar(width stat = "identity") + scale_fill_manual(values = c("red", "yellow")) + coord_polar("y", start = pi / 3) + labs(title = "Pac man")
22/22
Flexible, Beautiful, Customizable Graphs
22 of 22