-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-starwars_opinions_final.Rmd
executable file
·212 lines (136 loc) · 4.73 KB
/
3-starwars_opinions_final.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
---
title: "Star Wars Opinion Data"
author: "Angela Zoss"
date: "9/7/2018"
output: html_document
---
## Setup your environment
```{r}
# Load required libraries
library(tidyverse)
```
## Load your data
```{r}
# data from https://fivethirtyeight.com/features/americas-favorite-star-wars-movies-and-least-favorite-characters/
# note: CSV has two rows of headers, so I have manually created a list of headers and am adding
# that after loading just the data rows
new_names <- read_csv('data/StarWarsNames.csv') %>% select(NewNames)
starwars_opins <- read_csv('data/StarWars.csv', skip=2, col_names=FALSE) %>% setNames(unlist(new_names))
```
## Plot the opinions for Hans Solo
```{r}
# hint: there is a geom that will take a categorical variable and count the data points in
# each category
# also: feel free to remove NAs
ggplot(starwars_opins %>% filter(!is.na(OpinionSolo))) +
geom_bar(aes(OpinionSolo))
```
## Plot the opinions for Jar Jar Binks
```{r}
ggplot(starwars_opins %>% filter(!is.na(OpinionJarJar))) +
geom_bar(aes(OpinionJarJar))
```
## Edit the axes of each of these charts to make them comparable
```{r}
# remember, editing an axis usually requires a scale
ggplot(starwars_opins %>% filter(!is.na(OpinionSolo))) +
geom_bar(aes(OpinionSolo)) +
scale_y_continuous(limits=c(0,610))
ggplot(starwars_opins %>% filter(!is.na(OpinionJarJar))) +
geom_bar(aes(OpinionJarJar)) +
scale_y_continuous(limits=c(0,610))
```
## Advanced: can you think of another way to standardize the axes?
```{r}
ggplot(starwars_opins %>% select(OpinionSolo,OpinionJarJar) %>% gather(character,opinion) %>% na.omit()) +
geom_bar(aes(opinion)) +
facet_grid(.~character)
```
## Reorder the opinion levels so they are in a logical order
```{r}
opinion.levels <- c("Unfamiliar (N/A)","Very unfavorably","Somewhat unfavorably",
"Neither favorably nor unfavorably (neutral)",
"Somewhat favorably","Very favorably")
ggplot(starwars_opins %>%
select(OpinionSolo,OpinionJarJar) %>%
gather(character,opinion) %>%
na.omit() %>%
mutate(opinion=factor(opinion, opinion.levels))) +
geom_bar(aes(opinion)) +
facet_grid(.~character)
```
## Flip the axes so the opinion levels are on the y-axis
```{r}
ggplot(starwars_opins %>%
select(OpinionSolo,OpinionJarJar) %>%
gather(character,opinion) %>%
na.omit() %>%
mutate(opinion=factor(opinion, opinion.levels))) +
geom_bar(aes(opinion)) +
facet_grid(.~character) +
coord_flip()
```
## Use a different color for each opinion level
```{r}
ggplot(starwars_opins %>%
select(OpinionSolo,OpinionJarJar) %>%
gather(character,opinion) %>%
na.omit() %>%
mutate(opinion=factor(opinion, opinion.levels))) +
geom_bar(aes(opinion,fill=opinion),show.legend = FALSE) +
facet_grid(.~character) +
coord_flip()
```
## Customize the color palette
```{r}
# hint: editing colors requires a change of scale
# can google "R colors" for a list of all named colors
ggplot(starwars_opins %>%
select(OpinionSolo,OpinionJarJar) %>%
gather(character,opinion) %>%
na.omit() %>%
mutate(opinion=factor(opinion, opinion.levels))) +
geom_bar(aes(opinion,fill=opinion),show.legend = FALSE) +
facet_grid(.~character) +
coord_flip() +
scale_fill_manual(values = c("grey50","firebrick4","firebrick1","grey85","dodgerblue1","dodgerblue4")) +
theme_minimal()
```
## What percentage of survey participants responded "No" to "SeenAnyYN"?
```{r}
# start with a bar chart
ggplot(starwars_opins) +
geom_bar(aes(SeenAnyYN))
```
```{r}
# change it to a stacked bar chart, where the answers are different colors
# hint: there will only be one bar, so x will no longer be tied to a variable
ggplot(starwars_opins) +
geom_bar(aes(1,fill=SeenAnyYN))
```
```{r}
# change the coordinate system to polar with theta="y"
ggplot(starwars_opins) +
geom_bar(aes(1,fill=SeenAnyYN)) +
coord_polar(theta="y")
```
## Advanced 1: Compare this pattern across genders
```{r}
ggplot(starwars_opins) +
geom_bar(aes(1,fill=SeenAnyYN)) +
coord_polar(theta="y") +
facet_grid(.~Gender)
ggplot(starwars_opins) +
geom_bar(aes(1,fill=SeenAnyYN), position="fill") +
coord_polar(theta="y") +
facet_grid(.~Gender)
```
## Advanced 2: Calculate percentage and add as a label, simplify theme
```{r}
ggplot(starwars_opins) +
geom_bar(aes(1,fill=SeenAnyYN),show.legend = FALSE) +
coord_polar(theta="y") +
geom_text(data=starwars_opins %>% count(SeenAnyYN) %>% mutate(total=sum(n),label=paste0(SeenAnyYN,"\n",round(n/total*100,1),"%")),aes(c(1,1),c(1050,450),label=label)) +
theme_void() +
labs(title="Have you seen any of the 6 films in the Star Wars franchise?")
```