-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2_Notes.R
482 lines (264 loc) · 11.3 KB
/
Day2_Notes.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
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
## Follow up for day 1 and notes on ggplot2:
# Load Data:----
gapminder <- read.csv("data/gapminder_data.csv", header = TRUE)
head(gapminder)
cats <- read.csv("data/feline_data.csv", header = TRUE)
cats
# Libraries:----
# if needed, install.packages("ggplot2")
library(ggplot2)
# Challenges and Followup:----
# 1. How many rows and columns are in gapminder?
# 2. Calculate the log10 of gdpPercap and store it in a new variable called gdpPercap_log
# 3. Add gdpPercap_log as a new column to gapminder. Check the dimensions of the data and head.
# 4. Find the mean of lifeExp for the entire gapminder dataset.
# 5. Find the min and max lifeExp for the entire gapminder dataset.
# 6. How many years are present in the data?
# 7. Subset the data for only Africa in 2002.
# 8. Find the mean of each continents lifeExp.
# Solutions:----
# 1. How many rows and columns are in gapminder?
dim(gapminder)
# 2. Calculate the log10 of gdpPercap and store it in a new variable called gdpPercap_log
gdpPercap_log <- log10(gapminder$gdpPercap)
# 3. Add gdpPercap_log as a new column to gapminder. Check the dimensions of the data and head.
gapminder <- cbind(gapminder, gdpPercap_log)
head(gapminder)
dim(gapminder)
# Another way, do the calculation and the add column at the same time.
gapminder$gdpPercap_sqrt <- sqrt(gapminder$gdpPercap)
head(gapminder)
# 4. Find the mean of lifeExp for the entire gapminder dataset.
mean(gapminder$lifeExp)
# 5. Find the min and max lifeExp for the entire gapminder dataset.
min(gapminder$lifeExp)
max(gapminder$lifeExp)
# or, if you are super cool,
summary(gapminder$lifeExp)
# 6. How many years are present in the data?
gapminder$year
unique(gapminder$year)
length(unique(gapminder$year))
# 7. Subset the for only Africa in 2002:
x <- gapminder[(gapminder$continent == "Africa"), ]
y <- x[(x$year == 2002), ]
Africa2002 <- gapminder[(gapminder$continent == "Africa" & gapminder$year == 2002), ]
# 8. Find the mean of each continents lifeExp.
# For loops can help.
# First, how do we subset the data.
# yesterday, gapminder[(gapminder$year == 2002), ]
# What continents are in our dataset?
str(gapminder$continent) # not very helpful...
levels(gapminder$continent) # Ah ha!
gapminder[(gapminder$continent == "Africa"), ] # gives us every line in Africa, reaches max print
gapminder[(gapminder$continent == "Africa"), 5] # gives us every line in Africa, and only lifeExp
# Syntax for for loop:
# for(iterator in "set of things"){
# do something
# }
SetOfThings <- levels(gapminder$continent)
SetOfThings
# do something:
mean(gapminder[(gapminder$continent == "Africa"), 5])
# Now make this calculation for each of the continents:
for(continent in SetOfThings){
print(mean(gapminder[(gapminder$continent == continent), 5]))
}
# Almost, but which continent is which?
for(continent in SetOfThings){
print(paste(continent,mean(gapminder[(gapminder$continent == continent), 5])))
}
# Wow that was a lot of work. Surely it can be simpler?
MeanLifeExp <- aggregate(lifeExp ~ continent, gapminder, mean)
MeanLifeExp
MeanLifeExp_byYear <- aggregate(lifeExp ~ continent + year, gapminder, mean)
MeanLifeExp_byYear
# Bonus:
# Question about finding duplicate data:
# make a test case:
cats
# make a duplicate row in cats:
cats_dup <- rbind(cats, cats[1, ])
cats_dup
duplicated(cats_dup)
anyDuplicated(cats_dup)
# pull all the data that are unique, considering the entire row.
cats_clean <- unique(cats_dup)
# Plotting using ggplot2:----
# Three elements to keep track of:
# 1. dataset
# 2. coordinate system
# 3. geoms (what you want to plot)
# Activate Libraries
library(ggplot2)
head(gapminder)
# Whats the relationship between gdp and life expectancy?
# Syntax:
# ggplot(DATA, aes(XVar, YVar)) +
# geoms
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point()
# This is the same information but cleaner:
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point()
# What if no geom:
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp))
# just a coordinate system.
# CHALLENGE: Plot the change in life expentancy over time:----
ggplot(gapminder, aes(year, lifeExp)) +
geom_point()
# Now change the color of the points:
ggplot(gapminder, aes(year, lifeExp)) +
geom_point(aes(color = continent))
ggplot(MeanLifeExp_byYear, aes(year, lifeExp)) +
geom_point(aes(color = continent))
# Line Plots:----
# visualize data over time
ggplot(gapminder,aes(x=year, y=lifeExp, by=country, color=continent)) +
geom_line()
# you can keep adding layers to some extent:
ggplot(data = gapminder, mapping = aes(x=year, y=lifeExp, by=country, color=continent)) +
geom_line() + geom_point()
ggplot(data = MeanLifeExp_byYear, mapping = aes(x=year, y=lifeExp, color=continent)) +
geom_line() + geom_point()
# but the order matters.
# For example:
ggplot(data = gapminder, mapping = aes(x=year, y=lifeExp, by=country)) +
geom_line(aes(color = continent)) + geom_point()
ggplot(data = gapminder, mapping = aes(x=year, y=lifeExp, by=country)) +
geom_point() +
geom_line(aes(color = continent))
# Transformations and Statistics:----
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point()
# This is hard to read because of outliers.
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point(alpha = 0.5) + scale_x_log10()
# Remember earlier we added this new column to the data set. Is it the same?
head(gapminder)
ggplot(data = gapminder, mapping = aes(x = gdpPercap_log, y = lifeExp)) +
geom_point(alpha = 0.5)
# Do some light statistics:
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point() + scale_x_log10() + geom_smooth(method="lm")
# We can change elements of how the best fit line looks as well:
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point() + scale_x_log10() + geom_smooth(method="lm", size=1.5)
# CHALLENGE: Change the points to orange (or your favorite color) and make them larger. Change the color of the best fit line to black:----
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point(color = "orange", size = 2) + scale_x_log10() +
geom_smooth(method="lm", color = "black")
# What if you are interested in diffences among your groups? Hint: try using an aes with geom_smooth.
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point(alpha = 0.3) +
scale_x_log10() +
geom_smooth(method="lm", aes(color = continent))
# Including information about 3 quantitative variables:----
head(gapminder)
ggplot(gapminder, aes(lifeExp, gdpPercap_log)) +
geom_point(aes(size = pop, color = continent), alpha = 0.5)
# Can we make this more clear?
CountryAvgs <- aggregate(cbind(lifeExp, gdpPercap_log, pop) ~ continent + country, gapminder, mean)
CountryAvgs
ggplot(CountryAvgs, aes(lifeExp, gdpPercap_log)) +
geom_point(aes(size = pop, color = continent), alpha = 0.5)
ggplot(CountryAvgs, aes(lifeExp, gdpPercap_log)) +
geom_point(aes(size = pop, color = continent), alpha = 0.5) +
guides(size = FALSE)
# Add the trend line:
ggplot(CountryAvgs, aes(lifeExp, gdpPercap_log)) +
geom_point(aes(size = pop, color = continent), alpha = 0.5) +
geom_smooth(method = "lm", color = "black")
# Boxplots:----
head(gapminder)
ggplot(gapminder, aes(continent, lifeExp)) +
geom_boxplot()
# Add some color:
ggplot(gapminder, aes(continent, lifeExp)) +
geom_boxplot(aes(fill = continent))
# Add some color:
ggplot(gapminder, aes(continent, lifeExp)) +
geom_boxplot(aes(fill = continent, color = continent), alpha = 0.5)
# What if you want to specify the color:
# Add some color:
ggplot(gapminder, aes(continent, lifeExp)) +
geom_boxplot(aes(fill = continent, color = continent), alpha = 0.5) +
scale_fill_manual(values = c("purple","orange","blue","red","green")) +
scale_color_manual(values = c("purple","orange","blue","red","green"))
# CHALLENGE: plot the relationship between year and gpdPercap by country and specify the colors by continent:----
ggplot(data = gapminder, mapping = aes(x=year, y=lifeExp, by=country, color=continent)) +
geom_line() + geom_point() +
scale_color_manual(values = c("purple","orange","blue","red","green"))
# Plot distributions:----
ggplot(gapminder, aes(lifeExp)) +
geom_density(aes(fill = continent), alpha = 0.3)
# Plot with facets:-----
americas <- gapminder[gapminder$continent == "Americas",]
ggplot(data = americas, mapping = aes(x = year, y = lifeExp)) +
geom_line() +
facet_wrap( ~ country) +
theme(axis.text.x = element_text(angle = 45))
# CHALLENGE: Make a facets plot for the distributions:-----
ggplot(gapminder, aes(lifeExp)) +
geom_density(aes(fill = continent), alpha = 0.3) +
facet_wrap( ~ continent)
ggplot(gapminder, aes(lifeExp)) +
geom_density(aes(fill = factor(year)), alpha = 0.3) +
facet_wrap( ~ continent, scales = "free")
# Change the appearance of text:------
ggplot(data = americas, mapping = aes(x = year, y = lifeExp)) +
geom_line() +
facet_wrap( ~ country) +
theme(axis.text.x = element_text(angle = 45))
ggplot(data = americas, mapping = aes(x = year, y = lifeExp, color=continent)) +
geom_line() + facet_wrap( ~ country) +
labs(
x = "Year", # x axis title
y = "Life expectancy", # y axis title
title = "Figure 1", # main title of figure
color = "Continent" # title of legend
) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
# What else about this plot do you want to change?
ggplot(data = americas, mapping = aes(x = year, y = lifeExp, color=continent)) +
geom_line() + facet_wrap( ~ country) +
labs(
x = "Year", # x axis title
y = "Life expectancy", # y axis title
title = "Figure 1", # main title of figure
color = "Continent" # title of legend
) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
theme(plot.title = element_text(hjust = 0.5)) +
theme_classic()
# Save the plot:----
# Make a directory called results/
lifeExp_plot <- ggplot(data = americas, mapping = aes(x = year, y = lifeExp, color=continent)) +
geom_line() + facet_wrap( ~ country) +
labs(
x = "Year", # x axis title
y = "Life expectancy", # y axis title
title = "Figure 1", # main title of figure
color = "Continent" # title of legend
) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggsave(filename = "lifeExp.png",
plot = lifeExp_plot,
width = 20,
height = 14,
dpi = 300,
units = "cm")
# Resources:----
# https://www.data-to-viz.com
# https://rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf
# more general cheatsheets:
# https://rstudio.com/wp-content/uploads/2016/05/base-r.pdf
# https://sites.ualberta.ca/~ahamann/teaching/renr690/R_Cheat_Data.pdf
# https://github.com/rstudio/cheatsheets/blob/master/data-import.pdf
# Helpful Packages for data wrangling and resources:
# Tidyverse: Merging and subsetting data:
# https://rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf
# https://rstudio.com/resources/cheatsheets/
# Rmisc: Calculating summary statistics (mean, 95% confidence intervals, standard error, standard deviation): Rmisc https://cran.r-project.org/web/packages/Rmisc/Rmisc.pdf
# If you want access to my notes:
# https://github.com/ereverman/SWC_Jan2020