forked from mccallpitcher/ggplot2-for-survey-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_workshop_script.qmd
More file actions
267 lines (202 loc) · 6.63 KB
/
01_workshop_script.qmd
File metadata and controls
267 lines (202 loc) · 6.63 KB
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
---
title: "ggplot2 and survey data"
format: html
editor: visual
---
## 1. Load packages and import data
```{r}
# load packages
library(tidyverse)
library(scales)
# import data
results <- read_csv("employee_survey_results.csv")
```
## 2. Aggregate
```{r}
# counts and percentages
counts_perc <- results |>
count(statement, response, response_num) |>
group_by(statement) |>
mutate(total = sum(n),
percent = n / total)
# medians
medians <- results |>
group_by(statement) |>
summarize(med = median(response_num)) # calculate median by statement
```
## 3. 100% stacked bar chart
#### 3a. Try a bare bones plot
```{r, fig.width= 8, fig.height=4}
```
#### 3b. Set sorting order
```{r}
# determine order of the bars (combined value of agree/strongly agree)
stacked_order <- results |>
filter(response %in% c("Agree", "Strongly Agree")) |>
count(statement) |>
arrange(n) |>
pull(statement) # converts column values to a vector
# set order of the bars and fill categories
counts_perc <- counts_perc |>
mutate(statement = factor(statement,
levels = stacked_order),
response = factor(response,
levels = c("Strongly Agree",
"Agree",
"Neutral",
"Disagree",
"Strongly Disagree")))
```
#### 3c. Assign colors to response values
```{r}
fill_colors <- c(
"Strongly Agree" = "#3e6488",
"Agree" = "#829db2",
"Neutral" = "#c8cdd1",
"Disagree" = "#edad89",
"Strongly Disagree" = "#e36c32"
)
```
#### 3d. Better plot
```{r, fig.width= 8, fig.height=4}
counts_perc |>
ggplot(aes(x = percent, y = statement, fill = response)) +
geom_col(width = .7) +
labs(x = NULL, y = NULL, fill = NULL) +
scale_x_continuous(expand = c(0,0),
labels = percent) +
scale_fill_manual(values = fill_colors,
guide = guide_legend(reverse = TRUE)) +
theme_minimal() +
theme(legend.position = "top",
panel.grid = element_blank(),
panel.grid.major.x = element_line(color = "lightgrey"),
plot.margin = margin(r = 50))
```
#### 3e. Save plot
```{r}
ggsave("stacked_bar.png",
width = 8, height = 4,
dpi = 500, bg = "white")
```
## 4. Jittered strip plot
#### 4a. Try a bare bones plot
```{r, fig.height=4,fig.width=8}
```
#### 4b. Set sorting order
```{r}
# order medians data frame
medians <- medians |>
mutate(statement = fct_reorder(statement, med))
# order results data frame to match
results <- results |>
mutate(statement = factor(statement,
levels = levels(medians$statement)))
```
#### 4c. Create a full grid to enclose jittered points
```{r}
# Get the unique values of x and y variables
x_values <- 1:5
y_values <- unique(results$statement)
# Create a complete grid with all possible combinations
full_grid <- expand.grid(
response_num = x_values,
statement = y_values
)
```
#### 4d. Better plot
```{r, fig.height=4,fig.width=8}
ggplot() +
geom_tile(full_grid,
mapping = aes(x = response_num,
y = statement),
fill = NA,
color = "#dddddd",
height = .8,
linewidth = .5) +
geom_jitter(results,
mapping = aes(x = response_num,
y = statement,
fill = "One respondent"),
size = 2,
height = .25,
width = .25,
alpha = .5,
color = "#84B6E4") +
geom_point(medians,
mapping = aes(x = med,
y = statement,
color = "Median response"),
shape = 108,
size = 11.5) +
guides(color = guide_legend(override.aes = list(size = 6))) +
labs(x = NULL, y = NULL, color = NULL, fill = NULL) +
scale_color_manual(values = "#3e6488") +
scale_x_continuous(expand = c(0,0),
breaks = 1:5,
labels = c("1\nStrongly Disagree",
"2",
"3\nNeutral",
"4",
"5\nStrongly Agree")) +
theme_minimal() +
theme(panel.grid = element_blank(),
axis.text.x = element_text(size = 10, lineheight = 1.1),
legend.text = element_text(size = 10))
```
#### 4e. Save plot
```{r}
ggsave("jittered_strip.png",
width = 8, height = 4,
dpi = 500, bg = "white")
```
## 5. Distribution heatmap
#### 5a. Try a bare bones plot
```{r, fig.height=4,fig.width=8}
```
#### 5b. Set sorting order
```{r}
# sort counts data frame based on median factor
counts_perc <- counts_perc |>
mutate(statement = factor(statement,
levels = levels(medians$statement)))
```
#### 5c. Better plot
```{r, fig.height=4,fig.width=8}
ggplot() +
geom_tile(counts_perc,
mapping = aes(x = response_num,
y = statement,
fill = n),
height = .8) +
geom_tile(full_grid,
mapping = aes(x = response_num,
y = statement),
fill = NA,
color = "#dddddd",
height = .8,
linewidth = .5) +
geom_point(medians,
mapping = aes(x = med, y = statement,
color = "Median response"),
size = 4.3, shape=21, fill = NA) +
labs(x = NULL, y = NULL, fill = "Count", color = NULL) +
scale_x_continuous(expand = c(0,0), breaks = 1:5,
labels = c("1\nStrongly Disagree",
"2",
"3\nNeutral",
"4",
"5\nStrongly Agree")) +
scale_color_manual(values = "black") +
scale_fill_gradient(low = "#EBF5FB",
high = "#3f739b") +
theme_minimal() +
theme(panel.grid = element_blank(),
axis.text.x = element_text(size = 10, lineheight = 1.1))
```
#### 5d. Save plot
```{r}
ggsave("dist_heatmap.png",
width = 8, height = 4,
dpi = 500, bg = "white")
```