-
Notifications
You must be signed in to change notification settings - Fork 13
/
class4.Rmd
409 lines (331 loc) · 16.4 KB
/
class4.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
---
title: 'Introduction to R, Class 4: Data Visualization'
output: github_document
---
<!--class4.md is generated from class4.Rmd. Please edit that file -->
```{r setup, include=FALSE, purl=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Objectives
So far in this course, we have:
- learned basic R syntax, including working with objects and functions
- imported data into R for manipulation with base R methods
- loaded `tidyverse` and used its data science tools to manipulate and filter data
This last material continues our explorations of tidyverse with a specific focus on data visualization.
After completing this material,
you should be able to use `ggplot2` in R to:
- create and modify scatterplots and boxplots
- represent time series data as line plots
- split figures into multiple panels
- customize your plots
## Getting set up
Since we are continuing to work with data in `tidyverse`,
we need to make sure all of our data and packages are available for use.
Open your project in RStudio. Create a new script called `class4.R`,
add a title,
and enter the following code with comments:
```{r prep}
# load library
library(tidyverse)
# read in first filtered data from last class
birth_reduced <- read_csv("data/birth_reduced.csv")
# read in second filtered data from last class
smoke_complete <- read_csv("data/smoke_complete.csv")
```
If you have trouble accessing your data and see an error indicating the file is not found,
it is likely one of the following problems:
1. Check to make sure your project is open in RStudio.
You should see the path to your project directory (e.g., `~/Desktop/introR`) appear at the top of the console
(above the window showing output).
If this doesn't appear, you should save your script in your project directory, then go to `File -> Open Project`.
Navigate to the location of your project directory and open the folder, then try to reexecute your code.
2. Make sure you have the two datasets (`birth_reduced.csv` and `smoke_complete.csv`) in your `data` directory.
Please reference the materials from class 3 to filter the original clinical dataset and export these data.
Once your data are imported appropriately,
we can create a quick plot:
```{r base_plot}
# simple plot from base R from the smoke_complete dataset
plot(x=smoke_complete$age_at_diagnosis, y=smoke_complete$cigarettes_per_day)
```
This plot is from base R.
It gives you a general idea about the data,
but isn't very aesthetically pleasing.
Our work today will focus on developing more refined plots using `ggplot2`,
which is part of the `tidyverse`.
## Intro to ggplot2 and scatterplots
There are three steps to creating a ggplot.
We'll start with a scatterplot, which is used to compare quantitative (continuous) variables.
1. bind data: create a new plot with a designated dataset
```{r data_bind}
# basic ggplot
ggplot(data = smoke_complete) # bind data to plot
```
The last line of code creates an empty plot,
since we didn't include any instructions for how to present the data.
2. specify the aesthetic: maps the data to axes on a plot
```{r aesthetic}
# basic ggplot
ggplot(data = smoke_complete, aes(x = age_at_diagnosis,
y = cigarettes_per_day)) # specify aesthetics (axes)
```
This adds labels to the axis, but no data appear because we haven't specified how they should be represented
3. add layers: visual representation of plot, including ways through which data are represented (geometries or shapes) and themes (anything not the data, like fonts)
```{r geom}
ggplot(data = smoke_complete,
mapping = aes(x = age_at_diagnosis, y = cigarettes_per_day)) +
geom_point() # add a layer of geometry
```
The plus sign (`+`) is used here to connect parts of `ggplot` code together.
The line breaks and indentation used here represents the convention for `ggplot`,
which makes the code more readible and easy to modify.
In the code above, note that we don't need to include the labels for `data =` and `mapping =`.
It's also common to include the mapping (`aes`) in the `geom`, which allows for more flexibility in customizing
(we'll get to this later!).
```{r abb}
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day))
```
This plot is identical to the previous plot, despite the differences in code.
## Customizing plots
Now that we have the data generally displayed the way we'd like, we can start to customize a plot.
```{r alpha}
# add transparency with alpha
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day), alpha = 0.1)
```
Transparency is useful to help see the distribution of data,
especially when points are overlapping.
```{r color}
# change color of points
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day),
alpha = 0.1, color = "green")
```
For more information on colors available, look [here](http://sape.inf.usi.ch/quick-reference/ggplot2/colour).
We can also color points based on another (usually categorical) variable:
```{r var_color}
# plot disease by color
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day,
color = disease),
alpha = 0.1)
```
Note the location of `color=` with the other aesthetics,
as well as the lack of quotation marks around `disease`.
Coloring by a variable automatically adds a legend as well.
We can also change the general appearance of the plot (background colors and fonts):
```{r theme}
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day, color = disease), alpha = 0.1) +
theme_bw() # change background theme
```
This adds another layer to our plot representing a black and white theme.
A complete list of pre-set themes is available [here](https://ggplot2.tidyverse.org/reference/ggtheme.html),
and we'll cover ways to customize our own themes later in this lesson.
While the axes are currently sufficient,
they aren't particularly attractive.
We can add a title and replace the axis labels using `labs`:
```{r title}
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day, color = disease), alpha = 0.1) +
labs(title = "Age at diagnosis vs cigarettes per day", # title
x="age (days)", # x axis label
y="cigarettes per day") +# y axis label
theme_bw()
```
Another common feature to customize involves the orientation and appearance of fonts.
While this can be controlled by default themes like `theme_bw)`,
you can also control different parts independently.
For example, we can make a dramatic modification to all text in the plot:
```{r text}
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day, color = disease)) +
theme(text = element_text(size = 16)) # increase all font size
```
Alternatively, you can alter only one specific type of text:
```{r x_text}
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day, color = disease)) +
theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5)) # rotate and adjust x axis text
```
This rotates and adjusts the horizontal and vertical arrangement of the labels on only the x axis.
Of course, you can also modify other text (y axis, axis labels, legend).
After you're satisfied with a plot,
it's likely you'd want to share it with other people or include in a manuscript or report.
```{r dir, eval=FALSE}
# create directory for output
dir.create("figures")
```
```{r save}
# save plot to file
ggsave("figures/awesomePlot.jpg", width = 10, height = 10, dpi = 300)
```
This automatically saves the last plot for which code was executed.
You can view your `figures/` directory to see the exported jpeg file.
This command interprets the file format for export using the file suffix you specify. The other arguments dictate the size (`width` and `height`) and resolution (`dpi`).
> #### Challenge-scatterplot
> Create a scatterplot showing age at diagnosis vs years smoked
> with points colored by gender and appropriate axis labels.
## Box and whisker plots
Box and whisker plots compare the distribution of a quantitative variable among categories.
```{r box}
# creating a box and whisker plot
ggplot(smoke_complete) +
geom_boxplot(aes(x = vital_status, y = cigarettes_per_day))
```
The main differences from the scatterplots we created earlier are the `geom` type and the variables plotted.
We can change the color similarly to scatterplots:
```{r box_color}
# adding color
ggplot(smoke_complete) +
geom_boxplot(aes(x = vital_status, y = cigarettes_per_day), color = "tomato")
```
It seems weird to change the color of the entire box, though.
A better option would be to add colored points to a black box and whisker plot:
```{r box_jitter}
# adding colored points to black box and whisker plot
ggplot(smoke_complete) +
geom_boxplot(aes(x = vital_status, y = cigarettes_per_day)) +
geom_jitter(aes(x = vital_status, y = cigarettes_per_day), alpha = 0.3, color = "blue")
```
Jitter references a method of randomly offsetting points slightly to allow them to be seen and interpreted more easily.
This method, however, effectively duplicates some data points, since all points are shown with jitter and the boxplot shows outliers. You can use an option in `geom_boxplot` to suppress plotting of outliers:
```{r box_outlier}
# boxplot with both boxes and points
ggplot(smoke_complete) +
geom_boxplot(aes(x = vital_status, y = cigarettes_per_day), outlier.shape = NA) +
geom_jitter(aes(x = vital_status, y = cigarettes_per_day), alpha = 0.3, color = "blue")
```
> #### Challenge-comments
> Write code comments for each of the following lines of code.
> What is the advantage of writing code like this?
```{r comments}
my_plot <- ggplot(smoke_complete, aes(x = vital_status, y = cigarettes_per_day))
my_plot +
geom_boxplot(outlier.shape = NA) +
geom_jitter(alpha = 0.2, color = "purple")
```
> #### Challenge-order
> Does the order of layers in the last plot matter?
> What happens if `jitter` is coded before `boxplot`?
## Time series data as line plots
So far we've been able to work with the data as it appears in our filtered dataset.
Now that we're moving on to time series plots (changes in variables over time),
we need to manipulate the data.
We'll also be working with the `birth_reduced` dataset,
which we created last class (primarily by removing all missing data for year of birth).
We'd like to plot the number of individuals in the dataset born by year,
so we need to first count our observations based on both disease and year of birth:
```{r time_prep}
# count number of observations for each disease by year of birth
yearly_counts <- birth_reduced %>%
count(year_of_birth, disease)
```
We can plot these data as a single line:
```{r time_plot}
# plot all counts by year
ggplot(yearly_counts) +
geom_line(aes(x = year_of_birth, y = n))
```
Here, `n` represents the number of patients born in each year,
from the count table created above.
The result isn't very satisfying, because we also grouped by disease.
We can improve this by plotting each disease on a separate line,
which is more appropriate when there are multiple data points per year:
```{r time_disease}
# plot one line per cancer type
ggplot(yearly_counts) +
geom_line(aes(x = year_of_birth, y = n,
group = disease))
```
Moreover, we can color each line individually:
```{r time_color}
# color each line per cancer type
ggplot(yearly_counts) +
geom_line(aes(x = year_of_birth, y = n, color = disease))
```
Note that you don't have to include a separate argument for `group = disease` because grouping is assumed by `color = disease`.
> #### Challenge-line
> Create a line plot for year of birth and number of patients with lines representing each gender.
> Hint: you'll need to manipulate the `birth_reduced` dataset first.
> #### Challenge-dash
> How do you show differences in lines using dashes/dots instead of color?
## Faceting
So far we've been working on building single plots,
which can show us two main variables (for the x and y axes)
and additional variables using color (and potentially size/shape/etc).
Scientific visualizations often need to compare among categories
(e.g., control vs various treatments),
which is generally clearer if those categories are presented in separate panels.
ggplot provides this capacity through faceting.
Let's revisit the scatterplot we initially created,
plotting age at diagnosis by cigarettes per day,
with points colored by disease.
We add an additional layer to create facets, or separate panels,
for a given variable
(in this case, the same variable being used to color points):
```{r facet}
# use previous scatterplot, but separate panels by disease
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day, color = disease)) +
facet_wrap(vars(disease)) # wraps panels to make a square/rectangular plot
```
`vars` is used for faceting in the same way that `aes()` is used for mapping:
it is used to specify the variable to form facet groups.
`facet_wrap` determines how many rows and columns of panels are needed to create the most square-shaped final plot possible.
This becomes useful when there are many more categories:
```{r facet_wrap}
# add a variable by leaving color but changing panels to other categorical data
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day, color = disease)) +
facet_wrap(vars(tumor_stage))
```
In this case, we're now visualizing an additional variable (tumor stage),
in addition to the original three (age at diagnosis, cigarettes per day, and disease).
If you want to control the specific layout of panels, you can use `facet_grid` instead of `facet_wrap`:
```{r facet_grid}
# scatterplots with panels for vital status in one row
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day, color = disease)) +
facet_grid(rows = vars(vital_status))
```
This method can also plot panels in columns.
We may want to show interactions between two categorical variables,
by arranging panels into rows according to one variable and columns according to another:
```{r facet_both}
# add another variable using faceting
ggplot(smoke_complete) +
geom_point(aes(x = age_at_diagnosis, y = cigarettes_per_day, color = disease)) +
facet_grid(rows = vars(vital_status), cols = vars(disease)) # arrange plots via variables in rows, columns
```
Don't forget to look at the help documentation (e.g., `?facet_grid`) to learn more about additional ways to customize your plots!
> #### Challenge-panels
> Alter your last challenge plot of (birth year by number of patients) to show each gender in separate panels.
> #### Challenge-axis
> How do you change axis formatting, like tick marks and lines?
> Hint: You may want to use Google!
## Wrapping up
This material introduced you to ggplot as a tool for data visualization,
allowing you to now create publication-quality images using R code.
Combined with our previous explorations of the basic principles of R syntax,
importing and extracting data with base R,
and manipulating data using tidyverse,
you should be equipped to continue learning about R on your own and developing code to meet your research needs.
If you are interested in learning more about ggplot:
- Documentation for all `ggplot` features is available [here](https://ggplot2.tidyverse.org).
- RStudio also publishes a [ggplot cheat sheet](https://github.com/rstudio/cheatsheets/raw/master/data-visualization-2.1.pdf) that is really handy!
**This document is written in [R markdown](http://rmarkdown.rstudio.com),**
which is a method of formatting text, code, and output to create documents that are sharable with other people.
While this document is intended to serve as a reference for you to read while typing code into your own script,
you may also be interested in modifying and running code in the original R markdown file ([`class4.Rmd`](class4.Rmd) in the GitHub repository).
The course materials webpage is available [here](README.md).
Materials for all lessons in this course include:
- [Class 1](class1.md): R syntax, assigning objects, using functions
- [Class 2](class2.md): Data types and structures; slicing and subsetting data
- [Class 3](class3.md): Data manipulation with `dplyr`
- [Class 4](class4.md): Data visualization in `ggplot2`
## Extra exercises
Answers to all challenge exercises are available [here](solutions/).
#### Challenge-improve
Improve one of the plots previously created today, by changing thickness of lines,
name of legend, or color palette (http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/)