-
Notifications
You must be signed in to change notification settings - Fork 8
/
viz.Rmd
283 lines (222 loc) · 7.05 KB
/
viz.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
---
title: Data visualization
author: Aaron A. King
output:
html_document:
toc: yes
toc_depth: 4
toc_float:
collapsed: TRUE
smooth_scroll: TRUE
highlight: haddock
number_sections: FALSE
df_print: paged
includes:
after_body:
- _includes/main_bottom.html
- _includes/license.html
bibliography: tutorial.bib
csl: jss.csl
---
-----------------------
```{css echo=FALSE,purl=FALSE}
.folder {
color: #3333ff;
font-weight: bold;
}
```
```{r knitr-opts,include=FALSE,purl=FALSE}
prefix <- "viz"
source("_includes/setup.R",local=knitr::knit_global())
```
```{r prelims,include=FALSE,purl=FALSE}
set.seed(594709947L)
library(ggplot2)
theme_set(theme_bw())
```
## How to use this document.
This is an extremely condensed introduction to **R**'s base graphics and---more importantly---the powerful data-visualization package **ggplot2**, developed by Hadley Wickham.
Run the codes shown and study the outputs to learn about these tools.
When questions are posed, do your best to answer them.
For your convenience, the **R** codes for this document are `r xfun::embed_file("viz.R",text="provided in a script")` which you can download, edit, and run.
## Getting started: **R**'s base graphics
### Transgenic mosquito experiment
Let's load the data on transgenic mosquito survival time.
```{r}
dat <- read.csv("https://kingaa.github.io/R_Tutorial/data/mosquitoes.csv")
```
Let's compare the average lifespan of transgenic vs wildtype mosquitoes from this experiment.
The following split the data into two subsets, one for each genetic type.
```{r}
wt <- subset(dat,type=="wildtype",select=lifespan)
tg <- subset(dat,type=="transgenic",select=-type)
```
Let's try and visualize the data.
```{r}
dat$type <- factor(dat$type)
plot(dat)
op <- par(mfrow=c(1,2))
hist(tg$lifespan,breaks=seq(0,55,by=5),ylim=c(0,40))
hist(wt$lifespan,breaks=seq(0,55,by=5),ylim=c(0,40))
par(op)
```
**Question:** What does the second `par` command accomplish?
Another way to visualize a distribution is via the *empirical cumulative distribution plot*.
```{r}
plot(sort(dat$lifespan),seq(1,nrow(dat))/nrow(dat),type='n')
lines(sort(wt$lifespan),seq(1,nrow(wt))/nrow(wt),type='s',col='blue')
lines(sort(tg$lifespan),seq(1,nrow(tg))/nrow(tg),type='s',col='red')
```
**Question:** What does `type="n"` do in the first line above?
### Mammal body and brain sizes
The data on mammal body and brain sizes is included in the **MASS** package:
```{r}
library(MASS)
plot(mammals)
plot(mammals,log='x')
plot(mammals,log='xy')
plot(mammals$body,mammals$brain,log='xy')
plot(brain~body,data=mammals,log='xy')
```
### Oil production
```{r}
read.csv(
"https://kingaa.github.io/R_Tutorial/data/oil_production.csv",
comment.char="#"
) -> oil
head(oil)
summary(oil)
plot(oil)
plot(Gbbl~year,data=oil,subset=region=="North.America",type='l')
lines(Gbbl~year,data=oil,subset=region=="Eurasia",type="l",col='red')
library(tidyr)
library(dplyr)
oil |>
group_by(year) |>
summarize(Gbbl=sum(Gbbl)) -> total
plot(Gbbl~year,data=total,type='l')
```
## A systematic approach to visualization: the Grammar of Graphics
Parts of a graphic:
1. ***Data***
1. ***Geometrical object***: point, line, box, bar, density plot, contours, ribbons
1. ***Statistical transformations***: bins, mean, median, quantile, ECDF, identity
1. ***Aesthetic attributes***: x and y position, color, fill, size, shape, line type, transparency
1. ***Scales***: map the data onto the aesthetic attributes
1. A ***coordinate system***: maps x and y position onto the page
1. A ***faceting system***: multiple plots
You construct a graphical visualization by choosing the constituent parts.
This is implemented in the **ggplot2** package.
### References
- [ggplot2.tidyverse.org](https://ggplot2.tidyverse.org/)
- [ggplot2 documentation](https://ggplot2.tidyverse.org/reference/)
## Examples
### Energy production
```{r}
library(readr)
read_csv(
"https://kingaa.github.io/R_Tutorial/data/energy_production.csv",
comment="#"
) -> energy
library(ggplot2)
ggplot(data=energy,mapping=aes(x=year,y=TJ,color=region,linetype=source))+
geom_line()
ggplot(data=energy,mapping=aes(x=year,y=TJ,color=region))+
geom_line()+
facet_wrap(~source)
ggplot(data=energy,mapping=aes(x=year,y=TJ,color=source))+
geom_line()+
facet_wrap(~region,ncol=2)
```
What can you conclude from the above?
Try plotting these data on the log scale (`scale_y_log10()`).
How does your interpretation change?
```{r}
ggplot(data=energy,mapping=aes(x=year,y=TJ))+
geom_line()
ggplot(data=energy,mapping=aes(x=year,y=TJ,group=source))+
geom_line()
```
**Question:** How do you account for the appearance of the two plots immediately above?
```{r}
ggplot(data=energy,mapping=aes(x=year,y=TJ,group=interaction(source,region)))+
geom_line()
```
**Question:** What does the `group` aesthetic do?
Let's aggregate across regions by year and source of energy.
```{r}
energy |>
group_by(year,source) |>
summarize(TJ=sum(TJ)) |>
ungroup() -> tot
tot |>
ggplot(aes(x=year,y=TJ,color=source))+
geom_line()
tot |>
ggplot(aes(x=year,y=TJ,fill=source))+
geom_area()
```
Now let's aggregate across years by region and source.
See the [data munging tutorial](./munging.html) for more information on manipulating and reshaping data frames.
```{r}
energy |>
group_by(region,source) |>
summarize(TJ=mean(TJ)) |>
ungroup() -> reg
reg |>
ggplot(aes(x=region,y=TJ,fill=source))+
geom_bar(stat="identity")+
coord_flip()
reg |>
group_by(region) |>
mutate(frac = TJ/sum(TJ)) |>
ungroup() -> reg
reg |>
ggplot(aes(x=region,y=frac,fill=source))+
geom_bar(stat="identity")+
coord_flip()+
labs(y="fraction of production",x="region")
```
In the above, we first average across years for every region and source.
Then, for each region, we compute the fraction of the total production due to each source.
Finally, we plot the fractions using a barplot.
The `coord_flip` coordinate specification gives us horizontal bars instead of the default vertical bars.
Fancy!
Let's compare fossil fuel production to renewable.
We divide the sources into three types: Carbon-based, Nuclear, and Renewable.
We accomplish this using a "crosswalk" table:
```{r}
data.frame(
source=c("Coal","Gas","Oil","Nuclear","Hydro","Other Renewables"),
source1=c("Carbon","Carbon","Carbon","Nuclear","Renewable","Renewable")
) |>
right_join(energy,by="source") -> energy
energy |>
group_by(source1,region,year) |>
summarize(TJ = sum(TJ)) |>
ungroup() -> x
x |>
ggplot(aes(x=year,y=TJ,fill=source1))+
geom_area()+
facet_wrap(~region,ncol=2)+
labs(fill="source")
x |>
ggplot(aes(x=year,y=TJ,fill=source1))+
geom_area()+
facet_wrap(~region,scales="free_y",ncol=2)+
labs(fill="source")
x |>
group_by(source1,year) |>
summarize(TJ = sum(TJ)) |>
ungroup() -> y
y |>
ggplot(aes(x=year,y=TJ,fill=source1))+
geom_area()+
labs(fill="source")
```
--------------------------
### Exercise
Ask a question regarding one of the datasets shown here and devise a visualization to answer it.
--------------------------
Produced with **R** version `r getRversion()`.
--------------------------