-
Notifications
You must be signed in to change notification settings - Fork 2
/
ggplot_tips.R
361 lines (289 loc) · 9.41 KB
/
ggplot_tips.R
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
library(tidyverse)
library(ggrepel)
library(ggridges)
##############################################
# Pre-course survey data
##############################################
# Load the data, calculate word counts
surveys<-readRDS("surveyDat.RDS") %>%
mutate(why_join_wdct=str_count(why_join,"[[:alpha:]]+"),
future_plans_wdct=str_count(future_plans,"[[:alpha:]]+"),
group_project_wdct=str_count(group_project,"[[:alpha:]]+")
)
# ggplot has easy histograms
surveys %>%
ggplot(aes(x=stay_in_class)) +
geom_histogram()
# most people wanted to stay! that's nice.
# let's change the number ratings to words
# create a new variable called "stay"
# and create the mapping with factor labels
surveys <- surveys %>%
mutate(stay=factor(stay_in_class,ordered=TRUE,
levels=1:5,
labels=c("not sure",
"slightly sure",
"somewhat sure",
"very sure",
"extremely sure")
))
# since they are categories now, we can use a bar plot
surveys %>%
ggplot(aes(x=stay)) +
geom_bar()
# we can see there are empty cells now!
# Let's filter them out of the data
surveys <- surveys %>%
filter(!is.na(stay_in_class))
# Let's make them different colors, flip the axis and add axis labels
surveys %>%
ggplot(aes(x=stay,fill=stay)) +
geom_bar() +
coord_flip() +
labs(x="Will you stay in the course?",
y="Number of Students")
# let's put the question as the title so it is horizontal,
# make the words bigger, and get rid of the legend
surveys %>%
ggplot(aes(x=stay,fill=stay)) +
geom_bar() +
coord_flip() +
ggtitle("Will you stay in the course?") +
labs(x="",
y="Number of Students") +
theme(legend.position = "none",
title =element_text(size=20),
axis.text=element_text(size=15),
axis.title=element_text(size=20))
# instead of a screenshot, we save it to a file
ggsave("stay in course.png")
####################################
# is this variable correlated with whether they know RStudio?
surveys %>%
ggplot(aes(x=stay_in_class,y=exp_RStudio)) +
geom_point()
# the points overlap, so we use jitter
surveys %>%
ggplot(aes(x=jitter(stay_in_class),y=jitter(exp_RStudio))) +
geom_point()
# is there a relationship? let's fit a trend line
surveys %>%
ggplot(aes(x=jitter(stay_in_class),y=jitter(exp_RStudio))) +
geom_point() +
geom_smooth()
# we don't have enough points for a curve, let's do a straight line
surveys %>%
ggplot(aes(x=jitter(stay_in_class),y=jitter(exp_RStudio))) +
geom_point() +
geom_smooth(method="lm")
# Let's change the color, axis labels, and the background
surveys %>%
ggplot(aes(x=jitter(stay_in_class),y=jitter(exp_RStudio))) +
geom_point() +
geom_smooth(method="lm",
color="purple",
fill="orange") +
theme_bw() +
labs(x="Will you stay in class?",
y="Do you have RStudio experience?")
# bigger axis titles, and get rid of the grid
surveys %>%
ggplot(aes(x=jitter(stay_in_class),y=jitter(exp_RStudio))) +
geom_point() +
geom_smooth(method="lm",
color="purple",
fill="orange") +
theme_bw() +
labs(x="Will you stay in class?",
y="Do you have RStudio experience?") +
theme(panel.grid=element_blank(),
axis.title=element_text(size=15))
ggsave("Rstudio.png")
########################################
# let's look at what topics people know about
surveys %>%
select(starts_with("know_"))
# we need to convert from wide to long format
surveylong<-surveys %>%
select(ResponseId,starts_with("know_")) %>%
pivot_longer(-ResponseId,
values_to="knowledge",
names_to="topic")
surveylong
# remove the "know_" from the topic names
surveylong<-surveylong%>%
mutate(topic=gsub("know_","",topic,fixed=T))
surveylong
# some of these names are bad
unique(surveylong$topic)
#let's fix them with case_when()
surveylong<- surveylong %>%
mutate(topic_named=case_when(
# if the part left of the ~ is TRUE, use the right part as the value
topic == "crossval"~ "cross-validation",
topic == "supervised"~ "supervised learning",
topic == "dialogact"~ "dialogue acts",
topic == "topics"~ "topic models",
TRUE ~ topic # Finally, if none of the above are true, use this value
))
# not that helpful... no colors or labels and too much overlap
surveylong %>%
ggplot(aes(x=knowledge,group=topic_named)) +
geom_density(alpha=.2)
# better
surveylong %>%
ggplot(aes(x=knowledge,y=topic_named)) +
geom_density_ridges()
#needs colors, axis titles, etc.
surveylong %>%
ggplot(aes(x=knowledge,y=topic_named,
fill=topic_named)) +
geom_density_ridges() +
theme_bw() +
labs(x="Topic Knowledge",
y="") +
theme(legend.position="none",
panel.grid=element_blank(),
axis.text = element_text(size=20),
axis.title = element_text(size=24))
##### Let's put them in order
# first, calculate the order from low to high
topic_order=surveylong %>%
group_by(topic_named) %>%
summarize(tOrder=mean(knowledge))
# ugh, there are NAs again!!
head(topic_order)
topic_order=surveylong %>%
filter(!is.na(knowledge)) %>%
group_by(topic) %>%
summarize(tOrder=mean(knowledge))
surveylong %>%
# merge order data
left_join(topic_order) %>%
# use the order column to re-order topicID as a factor
mutate(topic_named=fct_reorder(topic_named,tOrder)) %>%
ggplot(aes(x=knowledge,fill=topic_named,
y=topic_named)) +
geom_density_ridges() +
theme_bw() +
theme(legend.position="none")
ggsave("topic_knowledge.png")
#################################################################
reviews<-readRDS("rev_med.RDS") %>%
mutate(wordcount=str_count(text,"[[:alpha:]]+"))
# let's plot the effect of word count on star rating
reviews %>%
ggplot(aes(x=wordcount,y=stars)) +
geom_point() +
geom_smooth()
# looks weird, let's flip the axes
reviews %>%
ggplot(aes(x=stars,y=wordcount)) +
geom_point() +
geom_smooth(method="lm")
# still weird... let's do a separate point for each star rating
reviews %>%
group_by(stars) %>%
summarize(avg=mean(wordcount),
se=sd(wordcount)/sqrt(n())) %>%
ggplot(aes(x=stars,y=avg,
ymin=avg-se,ymax=avg+se)) +
geom_point() +
geom_errorbar()
# maybe the effect is different for different price levels?
reviews %>%
group_by(stars,price) %>%
summarize(avg=mean(wordcount),
se=sd(wordcount)/sqrt(n())) %>%
ggplot(aes(x=stars,y=avg,
ymin=avg-se,ymax=avg+se)) +
geom_point() +
geom_errorbar()
# what a mess! we can connect points with colors and lines
reviews %>%
group_by(stars,price) %>%
summarize(avg=mean(wordcount),
se=sd(wordcount)/sqrt(n())) %>%
ggplot(aes(x=stars,y=avg,
ymin=avg-se,ymax=avg+se,
color=price,group=price)) +
geom_point() +
geom_errorbar() +
geom_line()
# still messy... let's make price a factor and narrower error bars
reviews %>%
group_by(stars,price) %>%
summarize(avg=mean(wordcount),
se=sd(wordcount)/sqrt(n())) %>%
mutate(price=factor(price)) %>%
ggplot(aes(x=stars,y=avg,
ymin=avg-se,ymax=avg+se,
color=price,group=price)) +
geom_point() +
geom_errorbar(width=.2) +
geom_line()
# let's add a fourth variable -is the user experienced?
# it's continuous so let's break up into four groups
reviews <- reviews %>%
mutate(user_exp=factor(ntile(user_review_count,3),
ordered=T,
levels=1:3,
labels=c("New Users","Some Experience","Most Experienced")))
# ntile creates equal groups from the distribution
reviews %>%
group_by(user_exp) %>%
summarize(mean(user_review_count))
# We can use different shapes but there are still too many lines
reviews %>%
group_by(stars,price,user_exp) %>%
summarize(avg=mean(wordcount),
se=sd(wordcount)/sqrt(n())) %>%
mutate(price=factor(price)) %>%
ggplot(aes(x=stars,y=avg,
ymin=avg-se,ymax=avg+se,
color=price,group=price,
shape=user_exp
)) +
geom_point() +
geom_errorbar(width=.2) +
geom_line()
#Instead, let's use facets!! Multiple plots at once :)
reviews %>%
group_by(stars,price,user_exp) %>%
summarize(avg=mean(wordcount),
se=sd(wordcount)/sqrt(n())) %>%
mutate(price=factor(price)) %>%
ggplot(aes(x=stars,y=avg,
ymin=avg-se,ymax=avg+se,
color=price,group=price,
)) +
geom_point() +
geom_errorbar(width=.2) +
geom_line() +
facet_wrap(~user_exp)
# let's clean up the labels fix the background, and move the legend
# not enough data for the top price so let's get rid of it
reviews %>%
filter(price!=4) %>%
group_by(stars,price,user_exp) %>%
summarize(avg=mean(wordcount),
se=sd(wordcount)/sqrt(n())) %>%
mutate(price=factor(price)) %>%
ggplot(aes(x=stars,y=avg,
ymin=avg-se,ymax=avg+se,
color=price,group=price,
)) +
geom_point() +
geom_errorbar(width=.2) +
geom_line() +
theme_bw() +
facet_wrap(~user_exp) +
labs(x="Star Rating",y="Word Count",
color="Price") +
theme(axis.title=element_text(size=20),
axis.text=element_text(size=14),
strip.text=element_text(size=16),
legend.title=element_text(size=20),
legend.text=element_text(size=14),
legend.position="top")
ggsave("review_experience.png")