Scientific Data Visualization: Using Ggplot2
Scientific Data Visualization: Using Ggplot2
Using ggplot2
Sacha Epskamp
University of Amsterdam
Department of Psychological Methods
11-04-2014
Hadley Wickham
Hadley Wickham
Evolution of data visualization
Scientific data visualization
names(Data)
str(Data)
View(Data)
ggplot2
2
Stress
0 10 20 30
Measurement
ggplot(Data, aes(x = Measurement, y = Stress)) +
geom_boxplot()
2
Stress
10 20
Measurement
ggplot(Data, aes(x = Measurement, y = Stress, group = Measurement)) +
geom_boxplot()
2
Stress
0 10 20 30
Measurement
ggplot(Data,
aes(x = Measurement, y = Stress, group = userID)
) + geom_line()
2
Stress
0 10 20 30
Measurement
ggplot(Data,
aes(x = Measurement, y = Stress, group = userID,
colour = Age)) + geom_line() +
facet_grid(Gender ~ .)
female
1
Age
50
0
Stress
40
3
30
20
male
1
0
0 10 20 30
Measurement
Store elements in an object:
g <- ggplot(Data,
aes(x = Measurement, y = Stress, group = userID,
colour = Age))
g <- g + geom_line()
g <- g +facet_grid(Gender ~ .)
Print the object to plot:
print(g)
female
1
Age
50
0
Stress
40
3
30
20
male
1
0
0 10 20 30
Measurement
I Many more graphical options can be added to ggplot calls
xlab Label of x-axis
ylab Label of y -axis
ggtitle Title of plot
theme Many, many graphical settings
theme_bw() A default black and white theme
I Use Google!
g + xlab("Time") + ylab("Amount of Stress") +
ggtitle("A very fancy plot") + theme_bw()
female
1
Age
Amount of Stress
50
0
40
3
30
20
2
male
1
0
0 10 20 30
Time
str(sumData)
20
10
0
rating
-10
-20
20
10
count
800
0
600
rating
400
200
-10
-20
20
10
factor(grade)
1
2
3
0
rating
4
5
6
7
-10
8
-20
20
10
factor(grade)
1
2
3
0
rating
4
5
6
7
-10
8
-20
20
10
factor(grade)
1
2
3
0
rating
4
5
6
7
-10
8
-20
12000
9000
count
6000
3000
2 4 6 8
grade
ggplot(sumData,
aes(x = grade, y = rating, colour = factor(grade))
) + geom_violin()
20
10
factor(grade)
1
2
3
0
rating
4
5
6
7
-10
8
-20
2 4 6 8
grade
Betweenness Closeness Strength Zhang Onnela
Xss
Xso
Xsb
Xli
Oun
Oin
Ocr
Oaa
Hsi
Hmo
Hga
Hfa
Ese
Efe
Ede
Ean
Cpr
Cpe
Cor
Cdi
Apa
Age
Afo
Afl
0 10 20 30 0.0020 0.0025 0.0030 0.0035 0.6 0.9 1.2 1.5 0.05 0.10 0.15 0.20 0.00 0.05 0.10 0.15
Spel: mollenspel
20
Sequence
length
1
2
10
3
Rating
4
5
6
7
0 8
9
20
Sequence
length
1
2
10 3
Rating
4
5
6
7
0
8
9
−10
4
userRating
−4
400
300
0
200
100
−4
Score − expected
0.5
Rating
0.0
0
−0.5
−1.0
−4
Response time
40000
Rating
30000
0 20000
10000
−4