the green lab - [12-a] data visualization in r
TRANSCRIPT
![Page 1: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/1.jpg)
1 Het begint met een idee
Data Visualization in R
Giuseppe Procaccianti
![Page 2: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/2.jpg)
Vrije Universiteit Amsterdam
2 Giuseppe Procaccianti / S2 group / The Green Lab
Data Visualization in R
● Goals for this lab session:
○ learn the basics of the ggplot2 package
■ understand the basic concepts behind ggplot2
■ perform a simple tutorial
![Page 3: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/3.jpg)
Vrije Universiteit Amsterdam
3 Giuseppe Procaccianti / S2 group / The Green Lab
A ggplot2 graph
![Page 4: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/4.jpg)
Vrije Universiteit Amsterdam
4 Giuseppe Procaccianti / S2 group / The Green Lab
A ggplot2 graph
![Page 5: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/5.jpg)
Vrije Universiteit Amsterdam
5 Giuseppe Procaccianti / S2 group / The Green Lab
A ggplot2 graph
● The ggplot2 package is based upon the graphics grammar (gg)
concept [1]
● Complex statistical graphs can be constructed by composing
elementary elements or changing their properties
[1] Wilkinson, Leland. The grammar of graphics. Springer Science & Business Media, 2006.
![Page 6: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/6.jpg)
Vrije Universiteit Amsterdam
6 Giuseppe Procaccianti / S2 group / The Green Lab
Example: a pie chart
pie <- ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) +
geom_bar(width = 1, position = "fill", color = "black")
![Page 7: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/7.jpg)
Vrije Universiteit Amsterdam
7 Giuseppe Procaccianti / S2 group / The Green Lab
Example: a pie chart
pie + coord_polar(theta = "y")
![Page 8: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/8.jpg)
Vrije Universiteit Amsterdam
8 Giuseppe Procaccianti / S2 group / The Green Lab
The qplot() function
● The qplot() function allows to create quick graphs
● Simple 1-line syntax, but not very powerful - similar to plot()
qplot(CPUusr, Watts, data=run)
![Page 9: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/9.jpg)
Vrije Universiteit Amsterdam
9 Giuseppe Procaccianti / S2 group / The Green Lab
The ggplot() function
● The ggplot() function is the main function of ggplot
● It allows to create graphs by adding subsequent layers
● Three main layers: aesthetics, geometrics, statistics
![Page 10: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/10.jpg)
Vrije Universiteit Amsterdam
10 Giuseppe Procaccianti / S2 group / The Green Lab
The ggplot() function
● The aesthetics layer contains the data you want to encode as
graphical properties
● The geometrics layer contains the actual graphical elements
a_graph <- ggplot(data = run,
aes(x = Watts, y = CPUusr))
a_graph <- a_graph + geom_point()
● The statistics layer contains statistics computed from the data
a_graph <- a_graph + stat_smooth()
![Page 11: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/11.jpg)
Vrije Universiteit Amsterdam
11 Giuseppe Procaccianti / S2 group / The Green Lab
Example: the violin plot
● From a boxplot….
a_graph <- ggplot(data = run, aes(x = Watts, y = CPUusr))
+ geom_boxplot()
+
![Page 12: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/12.jpg)
Vrije Universiteit Amsterdam
12 Giuseppe Procaccianti / S2 group / The Green Lab
Example: the violin plot
● ...to a violin plot
a_graph <- ggplot(data = run, aes(x = Watts, y = CPUusr))
+ geom_violin()
+
![Page 13: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/13.jpg)
Vrije Universiteit Amsterdam
13 Giuseppe Procaccianti / S2 group / The Green Lab
Why violin plots are great
● More informative (full data distribution is shown)
● You can draw boxplots on them!
![Page 14: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/14.jpg)
Vrije Universiteit Amsterdam
14 Giuseppe Procaccianti / S2 group / The Green Lab
Tutorial: qplot() + ggplot()
● Perform a basic exercise using both qplot() and ggplot()...
● ...using real experimental data (similar to yours)
![Page 15: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/15.jpg)
Vrije Universiteit Amsterdam
15 Giuseppe Procaccianti / S2 group / The Green Lab
Further links and references
● Official ggplot2 website
● qplot() tutorial - Edwin Chen
● ggplot2() tutorial - Josef Fruehwald
● Wilkinson, Leland. The grammar of graphics. Springer Science & Business Media, 2006. (Amazon)
![Page 16: The Green Lab - [12-A] Data visualization in R](https://reader034.vdocuments.mx/reader034/viewer/2022042906/58a27fbf1a28ab891a8b5809/html5/thumbnails/16.jpg)
Vrije Universiteit Amsterdam
16 Giuseppe Procaccianti / S2 group / The Green Lab
Thank you!