-
Notifications
You must be signed in to change notification settings - Fork 1
/
02_visualization.Rmd
261 lines (197 loc) · 7.82 KB
/
02_visualization.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
---
title: "Visualizations in R"
author: "Gregor Pirs, Jure Demsar and Erik Strumbelj"
date: "25/7/2019"
output:
prettydoc::html_pretty:
highlight: github
theme: architect
toc: true
toc_depth: 2
---
<div style="text-align:center">
<img src="./bstatcomp.png" alt="drawing" width="128"/>
</div>
# Scatterplot and line plot
A (2-dimensional) *scatterplot* can be used to represent the relationship between two variables by plotting pairs of observations. This can be two different variables, one variable as a function of the other, one variable as a function of time, etc. If we use a connecting line instead of just the points, we get a *line plot*.
In this example, we'll visualize the average recorded male and female height at 18 years of age by year of birth in Slovenia and Sweden First, we'll select these countries from the original data set and prepare data in long format:
```{r, warning = FALSE}
library(ggplot2)
library(tidyr)
# we obtained this data set from https://ourworldindata.org/human-height
dat <- read.csv(file = "./data/average-height-by-year-of-birth.csv")
dat <- dat[dat$Entity == "Slovenia" | dat$Entity == "Sweden",]
dat <- dat[,c(1,3,4,5)]
names(dat) <- c("Country", "Year", "Male", "Female")
dat <- gather(dat, "Gender", "Height", -Country, -Year)
# print a few randomly selected rows
set.seed(0)
print(dat[sample(1:nrow(dat), 10),])
```
Now we can plot the data:
```{r}
tmp <- dat[dat$Gender == "Male" & dat$Year >= 1970,]
# creating a layer
ggplot() + layer(data = tmp, geom = "point", mapping = aes(x = Year, y = Height, colour = Country), stat = "identity", position = "identity") +
ggtitle("Height of males at 18 years old by year of birth.") +
ylab("height (cm)") +
xlab("year of birth")
# using "shorthand" instructions
ggplot(tmp, aes(x = Year, y = Height, colour = Country)) +
geom_point() +
ggtitle("Height of males at 18 years old by year of birth.") +
ylab("height (cm)") +
xlab("year of birth")
```
One of the advantages of ggplot2 (grammar of graphics) is that once a mapping has been determined between the data and aesthetics, we can easily add another graphical layer that uses a different geometric representation, for example, a line:
```{r}
tmp <- dat[dat$Gender == "Male" & dat$Year >= 1970,]
ggplot(tmp, aes(x = Year, y = Height, colour = Country)) +
geom_point() +
ggtitle("Height of males at 18 years old by year of birth.") +
ylab("height (cm)") +
xlab("year of birth") +
geom_line()
```
## Shape aesthetic
```{r}
tmp <- dat[dat$Year >= 1970,]
ggplot(tmp, aes(x = Year, y = Height, colour = Country, shape = Gender)) +
geom_point() +
ggtitle("Height at 18 years old by year of birth.") +
ylab("height (cm)") +
xlab("year of birth") +
geom_line()
```
## Facetting
```{r}
tmp <- dat[dat$Year >= 1970,]
ggplot(tmp, aes(x = Year, y = Height, colour = Country)) +
geom_point() +
ggtitle("Height at 18 years old by year of birth.") +
ylab("height (cm)") +
xlab("year of birth") +
geom_line() +
facet_wrap(.~Gender)
```
By default all facets have the same scales. We can change that to each facet having its own x-scale, y-scale, or both:
```{r}
tmp <- dat[dat$Year >= 1970,]
ggplot(tmp, aes(x = Year, y = Height, colour = Country)) +
geom_point() +
ggtitle("Height at 18 years old by year of birth.") +
ylab("height (cm)") +
xlab("year of birth") +
geom_line() +
facet_wrap(.~Gender, scales = "free_y")
```
We can facet on an arbitrary number of variables:
```{r}
tmp <- dat[dat$Year >= 1970,]
ggplot(tmp, aes(x = Year, y = Height)) +
geom_point() +
ggtitle("Height at 18 years old by year of birth.") +
ylab("height (cm)") +
xlab("year of birth") +
geom_line() +
facet_wrap(.~Gender + Country, scales = "free_y")
```
# Histogram and density plot
A *histogram* is a way of representing the shape of the distribution of a single variable by binning the observations and plotting the counts. A *density plot* can be considered a continuous analogue to the histogram -- instead of binning the observations, we use some sort of kernel density observation.
For this example, we'll use data from all countries but only for year 1996:
```{r}
# we obtained this data set from https://ourworldindata.org/human-height
dat <- read.csv(file = "./data/average-height-by-year-of-birth.csv")
dat <- dat[dat$Year == 1996,]
dat <- dat[,c(1,3,4,5)]
names(dat) <- c("Country", "Year", "Male", "Female")
dat <- gather(dat, "Gender", "Height", -Country, -Year)
```
Our goal is to understand the distribution of male/female heights across the world:
```{r}
ggplot(dat, aes(x = Height)) +
geom_density() +
xlim(140,190)
ggplot(dat, aes(x = Height, fill = Gender)) +
geom_density(alpha = 0.5) +
xlim(140,190)
# NOTE: it's easy to save plots in one of the common formats
# (jpg, png, bmp, eps, pdf; it automatically reads the extension)
g1 <- ggplot(dat, aes(x = Height, fill = Gender)) +
geom_density(alpha = 0.5) + xlim(140,190)
ggsave("my_plot.pdf", g1, width = 5, height = 3)
```
```{r}
ggplot(dat, aes(x = Height)) +
geom_histogram()
ggplot(dat, aes(x = Height)) +
geom_histogram() +
facet_wrap(.~Gender)
```
# Bar plot
A *bar plot* can be used to represent the value of a variable for different groups. This includes representing the distribution of a variable with a relatively small number of unique values (categorical, ordinal) or the relationship between such a variable and a property of a numerical variable (for example, the mean).
In this example we'll plot the average age for different parts of the world:
```{r}
# we obtained this data set from https://ourworldindata.org/human-height
dat <- read.csv(file = "./data/average-height-by-year-of-birth.csv")
dat <- dat[dat$Year == 1996,]
dat <- dat[,c(1,3,4,5)]
names(dat) <- c("Country", "Year", "Male", "Female")
dat <- gather(dat, "Gender", "Height", -Country, -Year)
dat <- dat[dat$Country %in% c("Middle East and North Africa", "South Asia", "Sub-Saharan Africa", "Latin America and Caribbean",
"Europe and Central Asia","East Asia and Pacific"),]
ggplot(dat, aes(x = Country, y = Height, fill = Gender)) +
geom_bar(stat = "identity", position = "dodge")
ggplot(dat, aes(x = Country, y = Height, fill = Gender)) +
geom_bar(stat = "identity", position = "dodge") +
coord_flip()
```
# Boxplot
```{r}
# we obtained this data set from https://ourworldindata.org/human-height
dat <- read.csv(file = "./data/average-height-by-year-of-birth.csv")
dat <- dat[,c(1,3,4)]
names(dat) <- c("Country", "Year", "Height")
dat <- dat[dat$Year > 1960,]
ggplot(dat, aes(x = factor(Year), y = Height)) +
geom_boxplot()
ggplot(dat, aes(x = factor(Year), y = Height)) +
geom_boxplot() +
xlab("Year") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
```
# Practice examples
## Example 1
![](./visualization_examples/example01.png)
```{r}
# create data set
dat <- read.csv(file = "./data/average-height-by-year-of-birth.csv")
dat <- dat[,c(1,3,4)]
names(dat) <- c("Country", "Year", "Height")
dat <- dat[dat$Year == max(dat$Year),]
geo <- read.csv("./data/countries.csv")[,-1]
df <- merge(dat, geo)
# create plot
world_map <- readRDS("./data/world_map.rds") # preloaded from maps package
ggplot() +
geom_polygon(world_map, mapping = aes(x = long, y = lat, group=group), fill="white", colour="gray")
```
## Example 2
![](./visualization_examples/example02.png)
```{r}
# create data set
x <- seq(-2*pi, +2*pi, 0.01)
df <- data.frame(x = x, y = sin(x), fun = "sin(x)")
df <- rbind(df, data.frame(x = x, y = cos(x), fun = "cos(x)"))
# create plot
```
## Example 3
![](./visualization_examples/example03.png)
```{r}
# create data set
set.seed(0)
x <- c("A", "B", "C", "D", "E")
y <- runif(10, 0, 100)
df <- data.frame(subject = rep(x, 2), value = y, type = rep(c("Group I", "Group II"), each = 5))
# create plot
```