forked from jkosie/uoregon_r_bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.Rhistory
512 lines (512 loc) · 16.9 KB
/
.Rhistory
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
ps_data <- import("data/ps_data.sav")
ps_data <- import("https://raw.githubusercontent.com/Coryc3133/uoregon_r_bootcamp/master/pragmatic_scales_data.csv")
export(ps_data,
"data/ps_data.xlsx")
head(ps_data)
head(ps_data, n = 20)
tail(ps_data)
str(ps_data)
summary(ps_data)
ps_data[1, 5]
ps_data[1, "condition"]
ps_data[, "condition"]
ps_data$condition
which(ps_data$condition == "Label")
ps_data[which(ps_data$condition == "Label"),]
ps_data[which(ps_data$age >= 2.5),]
ps_data[,grep("^c", colnames(ps_data))]
ps_data[which(ps_data$condition == "Label" & ps_data$age < 3),]
ps_data[which(ps_data$item == "faces" | ps_data$item == "houses"),]
select(ps_data, age)
select(ps_data, age, condition)
select(ps_data, 1)
select(ps_data, -age)
select(ps_data, -5)
# select first three:
select(ps_data, 1:3)
# de-select last three:
select(ps_data, -(1:3)) # - requires parenthetical sequence
# select first three
select(ps_data, subid:correct)
# deselect first three
select(ps_data, -(subid:correct))
select(ps_data, starts_with("c"))
ps_data[, grep("^c", colnames(ps_data))]
select(ps_data, ends_with("e"))
select(ps_data, contains("i"))
filter(ps_data, condition == "No Label")
filter(ps_data, condition == "No Label" & age <= 3)
filter(ps_data, condition == "Label" | age <= 3)
filter(ps_data, between(age, 2.1, 2.5))
ps_data$age %>% # LHS is age vector from ps_data
sum() # pass that to the sum function
ps_data %>% # take the data, then...
select(age) %>% # select age, then...
sum() # take the sum
ps_data %>% select(age) %>% sum()
ps_data %>%
select(age) %>%
sum()
ps_data %>% # take the data, then...
select(age) %>% # select age, then...
sum()
sum(select(ps_data, age), na.rm = TRUE)
ps_data %>% # take the data, then...
filter(age > 2.5 | age > 3.2) %>% # filter for kids between 2.5 and 3.2, then...
select(subid, age) %>% # select subject id and centered age, then...
unique() # get unique rows
unique_filtered_data <- ps_data %>% # take the data, then...
filter(age > 2.5 | age > 3.2) %>% # filter for kids between 2.5 and 3.2, then...
select(subid, age) %>% # select subject id and centered age, then...
unique() # get unique rows
starwars %>%
select(name, homeworld)
starwars %>%
select(name:homeworld)
starwars %>%
select(contains("color"))
starwars %>%
select(homeworld, name, everything())
starwars %>%
filter(height < 100)
starwars %>%
filter(height > mean(height, na.rm = TRUE) + sd(height, na.rm = TRUE)) # don't forget na.rm!
starwars %>%
filter(is.na(mass)) # note you wrap the variable in is.na()
starwars %>%
filter(!is.na(mass))
!TRUE
!FALSE
! 5 == 5
! 5 == 4
starwars %>%
filter(height > mean(height, na.rm = TRUE) + sd(height, na.rm = TRUE) &
mass < mean(mass, na.rm = TRUE))
starwars %>%
filter(height > mean(height, na.rm = TRUE) + sd(height, na.rm = TRUE) |
height < mean(height, na.rm = TRUE) + sd(height, na.rm = TRUE))
starwars %>%
filter(hair_color == "grey")
starwars %>%
filter(hair_color == "grey" |
hair_color == "brown")
starwars %>%
filter(hair_color != "brown")
starwars %>%
filter(str_detect(hair_color, "grey")) # first argument is string var,
# followed by pattern it looks for
starwars %>%
filter(!str_detect(hair_color, "grey"))
starwars$height_in_m = starwars$height/100
starwars
# cleaning it back up
starwars <- starwars %>%
select(-height_in_m)
search()
starwars %>%
mutate(height_in_mm = height*10, # height in millimeters
height_in_m = height/100) # height in meters
starwars %>%
mutate(height_in_mm = height*10, # height in millimeters
height_in_m = height/100) # height in meters
starwars %>%
mutate(height_z = scale(height),
mass_z = scale(mass),
height_mass_z = rowMeans(data.frame(height_z, mass_z), na.rm = TRUE))
starwars %>%
mutate(height_z = scale(height),
mass_z = scale(mass),
height_mass_z = mean(data.frame(height_z, mass_z), na.rm = TRUE))
starwars %>%
rowwise() %>%
mutate(height_z = scale(height),
mass_z = scale(mass),
height_mass_z = mean(c(height_z, mass_z), na.rm = TRUE))
starwars %>%
mutate(height_z = scale(height),
mass_z = scale(mass)) %>%
rowwise() %>%
mutate(height_mass_z = mean(c(height_z, mass_z), na.rm = TRUE))
starwars
starwars %>%
mutate(gender = factor(gender))
starwars %>%
transmute(gender = factor(gender))
starwars %>%
mutate(gender = factor(gender))
starwars %>%
mutate(gender = mass*50)
starwars %>%
transmute(height_z = scale(height),
mass_z = scale(mass))
temp<- starwars %>%
transmute(height_z = scale(height),
mass_z = scale(mass))
View(temp)
View(temp)
starwars %>%
mutate(height_in_m = height / 100,
bmi = mass / height_in_m^2)
starwars %>%
mutate(height_in_m = height / 100,
bmi = mass / height_in_m^2)
starwars %>%
transmute(name = name,
mass = mass,
height_in_m = height / 100,
bmi = mass / height_in_m^2)
starwars %>%
group_by(homeworld)
starwars %>%
group_by(homeworld)
x <- starwars %>%
group_by(homeworld)
starwars %>%
group_by(homeworld)
starwars %>%
group_by(homeworld)
starwars %>%
group_by(homeworld)
starwars %>%
group_by(homeworld)
starwars %>%
group_by(homeworld)
5 + 5
starwars %>%
group_by(homeworld)
starwars %>%
mutate(mean_mass = mean(mass, na.rm = TRUE)) %>%
select(homeworld, mean_mass, everything()) # re-arrange for easy viewing
starwars %>%
group_by(homeworld) %>%
mutate(mean_mass = mean(mass, na.rm = TRUE)) %>%
select(homeworld, mean_mass, everything())
starwars %>%
group_by(homeworld, species) %>%
mutate(mean_mass = mean(mass, na.rm = TRUE)) %>%
select(homeworld, mean_mass, everything())
starwars %>%
summarize(mean_mass = mean(mass, na.rm = TRUE))
?n
starwars %>%
summarize(mean_mass = mean(mass, na.rm = TRUE),
sd_mass = sd(mass, na.rm = TRUE),
n = n())
starwars %>%
summarize(mean_mass = mean(mass, na.rm = TRUE),
sd_mass = sd(mass, na.rm = TRUE),
n = n(na.rm = TRUE))
starwars %>%
summarize(mean_mass = mean(mass, na.rm = TRUE),
sd_mass = sd(mass, na.rm = TRUE),
n = n())
starwars %>%
filter(!is.na(mass)) %>%
summarize(mean_mass = mean(mass, na.rm = TRUE),
sd_mass = sd(mass, na.rm = TRUE),
n = n())
starwars %>%
summarize(mean_mass = mean(mass, na.rm = TRUE),
sd_mass = sd(mass, na.rm = TRUE),
n = n())
starwars %>%
group_by(species) %>% # just add the group_by() call
summarize(mean_mass = mean(mass, na.rm = TRUE),
sd_mass = sd(mass, na.rm = TRUE),
n = n())
starwars %>%
group_by(species) %>% # just add the group_by() call
summarize(mean_mass = mean(mass, na.rm = TRUE),
sd_mass = sd(mass, na.rm = TRUE),
n = n()) %>%
filter(n > 1)
starwars %>% filter(species == "Zabrak")
starwars %>%
group_by(species, gender) %>% # just change the group_by() call
summarize(mean_mass = mean(mass, na.rm = TRUE),
sd_mass = sd(mass, na.rm = TRUE),
n = n()) %>%
filter(n > 1)
starwars %>% filter(is.na(species))
ps_data %>%
group_by(condition, item) %>%
summarize(num_correct = sum(correct),
num_trials = n(),
prop_correct = mean(correct))
starwars %>%
arrange(mass)
starwars %>%
arrange(desc(mass))
starwars %>%
arrange(height, mass)
starwars %>%
arrange(name)
starwars %>%
rename(char_name = name)
starwars %>%
rename(char_name = name,
height_cm = height,
mass_kg = mass)
lightsabers <- data.frame(name = c("Luke Skywalker",
"Darth Vader",
"Obi-Wan Kenobi",
"Dooku"),
lightsaber_color = c("green",
"red",
"blue",
"red"))
lightsabers
left_join(starwars,
lightsabers)
left_join(lightsabers,
starwars)
lightsabers
starwars %>%
distinct(homeworld)
starwars %>%
distinct(homeworld, species)
starwars %>%
distinct(homeworld, species, .keep_all = TRUE)
starwars %>%
distinct()
starwars %>%
count(species)
head(iris)
ggplot(data = iris)
ggplot(data = iris) + # attach data
geom_histogram(mapping = aes(x = Sepal.Length)) # add geom w/ aesthetic map
ggplot(data = iris) + # attach data
geom_histogram(mapping = aes(x = Sepal.Length), # add geom to aesthetic mapping
bins = 9) # change bins to 9
ggplot(data = iris) + # attach data
geom_histogram(mapping = aes(x = Sepal.Length),
fill = "red") # fill, bc hist bars are 2-d
ggplot(data = iris) + # attach data
geom_histogram(mapping = aes(x = Sepal.Length), # add geom to aesthetic mapping
bins = 100) # change bins to 9
ggplot(data = iris) + # attach data
geom_histogram(mapping = aes(x = Sepal.Length),
fill = "red") # fill, bc hist bars are 2-d
ggplot(data = iris) + # attach data
geom_histogram(mapping = aes(x = Sepal.Length),
color = "red") # fill, bc hist bars are 2-d
ggplot(data = iris) + # attach data
geom_histogram(mapping = aes(x = Sepal.Length),
fill = "red") # fill, bc hist bars are 2-d
ggplot(data = iris) + # attach data
geom_point(mapping = aes(x = Sepal.Length)) # add geom w/ aesthetic map
ggplot(data = iris) + # attach data
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) # add geom w/ aesthetic map
ggplot(data = iris) + # attach data
geom_point(mapping = aes(x = Sepal.Length, # geom w/ aesthetic mapping
y = Petal.Length,
color = Species)) # color including in aes()
ggplot(data = iris) + # attach data
geom_point(mapping = aes(x = Sepal.Length, # geom w/ aesthetic mapping
y = Petal.Length,
color = "red")) # color including in aes()
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species)
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
geom_smooth(mapping = aes(x = Sepal.Length, y = Petal.Length), color = "black")
ggplot(data = iris) +
geom_point(aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
geom_smooth(aes(x = Sepal.Length, y = Petal.Length),
color = "black",
method = "lm")
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point(aes(color = Species)) +
geom_smooth(color = "black", method = "lm")
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
geom_point() +
geom_smooth()
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
geom_point() +
geom_smooth(color = "black")
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
geom_point() + # inhereit global
geom_smooth() + # inherit global
geom_smooth(color = "black") # change color to constant
ggplot(data = iris, aes(x = Species, y = Petal.Length)) +
geom_bar() # inhereit global
?geom_bar
ggplot(data = iris, aes(x = Species, y = Petal.Length)) +
geom_bar(stat = "identity") # inhereit global
iris$Petal.Length
ggplot(data = iris, aes(x = Species, y = Petal.Length, fill = Species)) +
geom_bar(stat = "identity") # inhereit global
# get summary data
starwars_means <- starwars %>%
filter(species == "Human" | species == "Droid") %>%
group_by(species) %>%
summarize(mean_mass = mean(mass, na.rm = TRUE))
ggplot(starwars_means, aes(x = species, y = mean_mass, fill = species)) +
geom_bar(stat = "identity")
# uisng the summary data as the global data
ggplot(starwars_means, aes(x = species, y = mean_mass, fill = species)) +
geom_bar(stat = "identity") +
geom_point(data = filter(starwars, species == "Human" |
species == "Droid"), # using individual
# data as geom specific data
aes(x = species, y = mass))
# uisng the summary data as the global data
ggplot(starwars_means, aes(x = species, y = mean_mass, fill = species)) +
geom_bar(stat = "identity") +
geom_point(data = filter(starwars, species == "Human" |
species == "Droid"), # using individual
# data as geom specific data
aes(x = species, y = mass),
position = position_jitter())
# uisng the summary data as the global data
ggplot(starwars_means, aes(x = species, y = mean_mass, fill = species)) +
geom_bar(stat = "identity") +
geom_point(data = filter(starwars, species == "Human" |
species == "Droid"), # using individual
# data as geom specific data
aes(x = species, y = mass),
position = position_jitter(height = 0, width = .1))
# uisng the summary data as the global data
ggplot(starwars_means, aes(x = species, y = mean_mass, fill = species)) +
geom_bar(stat = "identity") +
geom_point(data = filter(starwars, species == "Human" |
species == "Droid"), # using individual
# data as geom specific data
aes(x = species, y = mass),
alpha = .3)
# Import ps_data (uneccesary if you completed exercise 1.2a)
ps_data <- import("pragmatic_scales_data.csv")
# Mean Data:
ps_mean_age <- ps_data %>%
group_by(condition) %>%
summarize(mean_age = mean(age, na.rm = TRUE))
# distinct kids data
ps_distinct_age <- ps_data %>%
distinct(subid, condition, age)
ggplot(ps_mean_age, aes(x = condition, y = mean_age, fill = condition)) +
geom_bar(stat = "identity") +
geom_point(data = ps_distinct_age, aes(x = condition, y = age),
position = position_jitter(height = 0, width = .2))
ggplot(ps_mean_age, aes(x = condition, y = mean_age, fill = condition)) +
geom_bar(stat = "identity") +
geom_point(data = ps_distinct_age, aes(x = condition, y = age),
position = position_jitter(height = 0, width = .2)) +
# change labels with labs:
labs(title = "Age by Condition:", # add a title with title
subtitle = "Group- and Kid-level age", # you can even add a subtitle
x = "Condition",
y = "Mean Age")
ggplot(ps_mean_age, aes(x = condition, y = mean_age, fill = condition)) +
geom_bar(stat = "identity") +
geom_point(data = ps_distinct_age, aes(x = condition, y = age),
position = position_jitter(height = 0, width = .2)) +
# change labels with labs:
labs(title = "Age by Condition:", # add a title with title
subtitle = "Group- and Kid-level age", # you can even add a subtitle
x = "Condition",
fill = "Condition",
y = "Mean Age")
ggplot(ps_mean_age, aes(x = condition, y = mean_age, fill = condition)) +
geom_bar(stat = "identity") +
geom_point(data = ps_distinct_age, aes(x = condition, y = age),
position = position_jitter(height = 0, width = .2)) +
# change labels with labs:
labs(title = "Age by Condition:", # add a title with title
subtitle = "Group- and Kid-level age", # you can even add a subtitle
x = "Condition",
y = "Mean Age") +
theme(axis.text = # axis.title argument of theme
element_text(size = 30)) # size element of element_text
ggplot(ps_mean_age, aes(x = condition, y = mean_age, fill = condition)) +
geom_bar(stat = "identity") +
geom_point(data = ps_distinct_age, aes(x = condition, y = age),
position = position_jitter(height = 0, width = .2)) +
# change labels with labs:
labs(title = "Age by Condition:", # add a title with title
subtitle = "Group- and Kid-level age", # you can even add a subtitle
x = "Condition",
y = "Mean Age") +
theme(axis.text = # axis.title argument of theme
element_blank()) # set to element_blank
label_madness_theme <- theme(axis.text = # axis.title argument of theme
element_text(size = 30)) # size element of element_text
ggplot(ps_data, aes(x = item, y = correct, fill = item)) +
geom_bar(stat = "identity") +
facet_wrap(~condition) +
label_madness_theme # add in the custom theme
# Custom theme for the reputation manuscript
theme_rep_manuscript <- theme(panel.background = element_blank(),
panel.grid.major.x = element_line(size=.4, color="gray90"))+
theme(axis.line.x = element_line(color="black", size = 1),
axis.line.y = element_line(color="black", size = 1))+
theme(axis.title = element_text(size = rel(1), color="black"),
axis.text = element_text(size = rel(1), color="black"))+
theme(plot.title= element_text(size=rel(1), hjust = .5))
# Custom theme for the reputation manuscript
theme_rep_ppt <- theme(panel.background = element_blank(),
panel.grid.major.x = element_line(size=1, color="gray90"))+
theme(axis.line.x = element_line(color="black", size = 2),
axis.line.y = element_line(color="black", size = 2))+
theme(axis.title = element_text(size = rel(2), color="black"),
axis.text = element_text(size = rel(2), color="black"))+
theme(plot.title= element_text(size=rel(2), hjust = .5))
ggplot(ps_data, aes(x = item, y = correct, fill = item)) +
geom_bar(stat = "identity") +
facet_wrap(~condition) +
theme_rep_manuscript
ggplot(ps_data, aes(x = item, y = correct, fill = item)) +
geom_bar(stat = "identity") +
facet_wrap(~condition) +
theme_rep_ppt +
coord_flip() # flip the coordinates to accomodate size of text
ggplot(ps_data, aes(x = item, y = correct, fill = item)) +
geom_bar(stat = "identity") +
facet_wrap(~condition) +
theme_minimal()
ggplot(ps_data, aes(x = item, y = correct, fill = item)) +
geom_bar(stat = "identity") +
facet_wrap(~condition) +
theme_linedraw()
diamonds <- diamonds %>%
sample_n(1000)
diamonds
ggplot(diamonds, aes(x = carat)) +
geom_histogram()
ggplot(diamonds, aes(x = price)) +
geom_histogram()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point() +
theme_minimal() +
scale_x_log10() +
scale_y_log10()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point() +
theme_minimal() +
scale_x_log10() +
scale_y_log10()
ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
geom_point(alpha = .5) + # changing alpha bc of overlap
theme_minimal() +
scale_x_log10() +
scale_y_log10()
?scale_color_brewer
ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
geom_point(alpha = .5) + # changing alpha bc of overlap
theme_minimal() +
scale_x_log10() +
scale_y_log10() +
scale_color_brewer(palette="Set1")
ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
geom_point(alpha = .5) + # changing alpha bc of overlap
theme_minimal() +
scale_x_log10() +
scale_y_log10() +
scale_color_brewer(palette="Dark2")
ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
geom_point(alpha = .5) + # changing alpha bc of overlap
theme_minimal() +
scale_x_log10() +
scale_y_log10() +
scale_color_brewer(palette="Dark2") +
annotation_logticks()