the green lab - [12-a] data visualization in r
Post on 14-Feb-2017
481 Views
Preview:
TRANSCRIPT
1 Het begint met een idee
Data Visualization in R
Giuseppe Procaccianti
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
Vrije Universiteit Amsterdam
3 Giuseppe Procaccianti / S2 group / The Green Lab
A ggplot2 graph
Vrije Universiteit Amsterdam
4 Giuseppe Procaccianti / S2 group / The Green Lab
A ggplot2 graph
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.
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")
Vrije Universiteit Amsterdam
7 Giuseppe Procaccianti / S2 group / The Green Lab
Example: a pie chart
pie + coord_polar(theta = "y")
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)
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
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()
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()
+
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()
+
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!
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)
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)
Vrije Universiteit Amsterdam
16 Giuseppe Procaccianti / S2 group / The Green Lab
Thank you!
g.procaccianti@vu.nl
i.malavolta@vu.nl
top related