-
Notifications
You must be signed in to change notification settings - Fork 13
/
01-Visualize.Rmd
executable file
·162 lines (97 loc) · 3.97 KB
/
01-Visualize.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
---
title: "Data Visualization"
output: html_notebook
editor_options:
chunk_output_type: inline
---
<!-- This file by Jake Thompson is licensed under a Creative Commons Attribution 4.0 International License, adapted from the orignal work at https://github.com/rstudio/master-the-tidyverse by RStudio. -->
## Setup
The first chunk in an R Notebook is usually titled "setup," and by convention includes the R packages you want to load. Remember, in order to use an R package you have to run some `library()` code every session. Execute these lines of code to load the packages.
```{r setup, include = FALSE}
library(tidyverse)
library(fivethirtyeight)
```
## Bechdel test data
We're going to start by playing with data collected by the website FiveThirtyEight on movies and [the Bechdel test](https://en.wikipedia.org/wiki/Bechdel_test).
To begin, let's just preview our data. There are a couple ways to do that. One is just to type the name of the data and execute it like a piece of code.
```{r}
bechdel
```
Notice that you can page through to see more of the dataset.
Sometimes, people prefer to see their data in a more spreadsheet-like format, and RStudio provides a way to do that. Go to the Console and type `View(bechdel)` to see the data preview.
(An aside-- `View` is a special function. Since it makes something happen in the RStudio interface, it doesn't work properly in R Notebooks. Most R functions have names that start with lowercase letters, so the uppercase "V" is there to remind you of its special status.)
## Consider
What relationship do you expect to see between movie budget (budget) and domestic gross(domgross)?
## Your Turn 1
Run the code on the slide to make a graph. Pay strict attention to spelling, capitalization, and parentheses!
```{r}
```
## Your Turn 2
Add `color`, `size`, `alpha`, and `shape` aesthetics to your graph. Experiment.
```{r}
ggplot(data = bechdel) +
geom_point(mapping = aes(x = budget, y = domgross))
```
## Set vs map
```{r}
ggplot(bechdel) +
geom_point(mapping = aes(x = budget, y = domgross), color = "blue")
```
## Your Turn 3
Replace this scatterplot with one that draws boxplots. Use the cheatsheet. Try your best guess.
```{r}
ggplot(data = bechdel) +
geom_point(mapping = aes(x = clean_test, y = budget))
```
## Your Turn 4
Make a histogram of the `budget` variable from `bechdel`.
```{r}
```
## Your Turn 5
Make a density plot of `budget` colored by `clean_test`.
```{r}
```
## Your Turn 6
Make a barchart of `clean_test` colored by `clean_test`.
```{r}
```
## Your Turn 7
Predict what this code will do. Then run it.
```{r}
ggplot(data = bechdel) +
geom_point(mapping = aes(x = budget, y = domgross)) +
geom_smooth(mapping = aes(x = budget, y = domgross))
```
## global vs local
```{r}
ggplot(data = bechdel, mapping = aes(x = budget, y = domgross)) +
geom_point(mapping = aes(color = clean_test)) +
geom_smooth()
```
```{r}
ggplot(data = bechdel, mapping = aes(x = budget, y = domgross)) +
geom_point(mapping = aes(color = clean_test)) +
geom_smooth(data = filter(bechdel, clean_test == "ok"))
```
## Your Turn 8
Add a position adjustment to this plot to compare the frequency of test results across decades.
```{r}
ggplot(data = bechdel, mapping = aes(x = decade_code)) +
geom_bar(mapping = aes(fill = clean_test))
```
## Saving plots
Save the last plot. If you run your `ggsave()` code inside this notebook, the image will be saved in the same directory as your .Rmd file but if you run `ggsave()` in the Console it will be in your working directory. You can manually set the directory with the `path` argument.
```{r}
ggsave("my-plot.png", width = 8, height = 6, units = "in", dpi = "retina")
```
***
# Take aways
You can use this code template to make thousands of graphs with **ggplot2**.
```{r eval = FALSE}
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>),
stat = <STAT>, position = <POSITION>) +
<FACET_FUNCTION> +
<SCALE_FUNCTION> +
<THEME_FUNCTION>
```