forked from jananiravi/R_intro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
538 lines (443 loc) · 19.8 KB
/
index.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
---
title: "R@GARD | Intro to R programming"
subtitle: "⚔"
author: "Jill Bilodeaux, Kewalin Samart <br/>JRaviLab"
institute: "University of Colorado Anschutz Medical Campus<br/>"
date: "jill.bilodeaux@cuanschutz.edu | kewalin.samart@cuanschutz.edu"
output:
xaringan::moon_reader:
css: xaringan-themer.css
nature:
slideNumberFormat: "%current%"
highlightStyle: github
highlightLines: true
highlightSpans: true
ratio: 16:9
countIncrementalSlides: false
includes:
after_body: insert-logo.html
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(
fig.width=9, fig.height=3.5, fig.retina=3,
out.width = "60%",
cache = FALSE,
echo = TRUE,
message = FALSE,
warning = FALSE,
hiline = TRUE
)
```
```{r xaringan-themer, include=FALSE, warning=FALSE}
library(xaringanthemer)
style_duo_accent(
primary_color = "#000000",
secondary_color = "#43418A",
inverse_header_color = "#FFFFFF"
)
# style_duo(primary_color = "#1F4257",
# secondary_color = "#F97B64")
#style_mono_accent_inverse(base_color = "#3C989E")
#style_mono_accent(base_color = "#43418A")
```
---
# Why R?
.pull-left[
### R is free
### R is popular
### R is powerful
### R is flexible
### R is well-supported
]
.pull-right[
### R has a strong community!
```{r, out.width='60%', echo=FALSE, fig.align='bottom', fig.alt="RStats community by Allison Horst: A person in a cape that reads 'code hero' who looks like they are flying through the air while typing on a computer while saying 'I’m doing a think all on my own!' The coder’s arms and legs have ropes attached to two hot air balloons lifting them up, with labels on the balloons including 'teachers', 'bloggers', 'friends', 'developers'. Below the code hero, several people carry a trampoline with labels 'support' and 'community' that will catch them if they fall"}
knitr::include_graphics('https://github.com/allisonhorst/stats-illustrations/raw/main/rstats-artwork/code_hero.jpg')
```
]
---
# Why R?
**R is fun!**
.pull-left[
`beeper::beep(3)`
```{r, out.width='70%', echo=FALSE, fig.alt="beepr: A cartoon alligator running excitedly toward a computer with musical notes coming out of it. Text reads 'beepr::beep()s when your analysis is done running!'"}
knitr::include_graphics('https://github.com/allisonhorst/stats-illustrations/raw/main/rstats-artwork/beepr.jpg')
```
]
.pull-right[
`Rcade::games$Pacman` & `Rcade::games$`2048``
```{r, out.width='30%', echo=FALSE, fig.alt="Pacman from the Rcade package with the four characters, Blinky, Pinky, Inky, and Clyde"}
knitr::include_graphics('img/rcade_pacman.png')
```
```{r, out.width='20%', echo=FALSE, fig.alt="Rcade package snapshot of the 2048 game"}
knitr::include_graphics('img/rcade_2048.png')
```
]
---
# R community
A few global R communities in addition to RFoundation, RForwards, RStudio, and the Rconsortium.
.pull-left[
```{r echo=F, out.width='25%', fig.alt="8 R community logos: rOpenSci, useR, R-Ladies, AfricaR, ArabR, AsiaR, miR, LatinR"}
knitr::include_graphics("https://cdn.shortpixel.ai/spai/w_600+q_lossy+ret_img+to_webp/https://www.numfocus.org/wp-content/uploads/2016/07/ropensci-logo-300.png")
knitr::include_graphics("https://user2021.r-project.org//img/artwork/user-logo-color.png")
#knitr::include_graphics("https://www.r-consortium.org/wp-content/uploads/sites/13/2016/09/RConsortium_Horizontal_Pantone.png")
#knitr::include_graphics("https://community.rstudio.com/uploads/default/original/3X/5/d/5dc960154a129282ba4283771da2fab6fde146fb.png")
knitr::include_graphics("https://community-cdn.rstudio.com/uploads/default/original/3X/e/6/e61d5c8d76feee06efb84f95880e3fc4e12cf269.png")
knitr::include_graphics("https://africa-r.org/author/avatar_hu73f0541d09985efc907138a8670305ca_97180_270x270_fill_lanczos_center_2.png")
knitr::include_graphics("https://arabr.github.io/img/arab-r-logo2.png")
knitr::include_graphics("https://pbs.twimg.com/profile_images/1491075015730954244/pzpN5s2V_400x400.jpg")
knitr::include_graphics("https://mircommunity.com/img/mir_logo_small.png")
knitr::include_graphics("https://github.com/LatinR/latinr/raw/master/man/figures/logo.png")
#"https://www.r-consortium.org/wp-content/uploads/sites/13/2016/09/RConsortium_Horizontal_Pantone.png")
```
<br> <br>
Find your local R chapter on Twitter, [Meetup.com](//meetup.com), [RLadies.org](//rladies.org), or the [R community explorer](https://benubah.github.io/r-community-explorer)!
]
.pull-right[
```{r out.width='60%', echo=F, fig.alt="RStats community logo again by Allison Horst"}
knitr::include_graphics("https://github.com/allisonhorst/stats-illustrations/raw/main/rstats-artwork/code_hero.jpg")
```
```{r out.width='80%', echo=F, fig.alt="RUG and RLadies chapter stats"}
knitr::include_graphics("img/202303-rladies-dashboard.png")
knitr::include_graphics("img/202303-rugs-dashboard.png")
```
]
---
# We all go through the 'stages of R'
.center[
```{r, out.width='70%', echo=FALSE, fig.align='bottom', fig.alt="R Knowledge Rollercoaster: An illustrated cartoon graph with 'How much I think I know about R' on the y-axis, with axis labels at 'I know nothing' and 'I know lots', versus 'time' on the x-axis. The line varies widely between the two. Above the line are emoji-like faces, showing uncertainty and hope early on. At a second peak is the label 'join R community on twitter', with a 'mind-blown' emoji face. The line quickly descends, but with a happy looking emoji character sliding down it."}
knitr::include_graphics("https://github.com/allisonhorst/stats-illustrations/raw/main/rstats-artwork/r_rollercoaster.png")
```
]
---
# What can you do with R?
#### Data wrangling | knowing about your data
#### Data analysis & summarization | including RStats
#### Data visualization & graphical summaries
#### Reproducible research
#### Manuscripts, books, thesis
#### Interactive web apps
#### Webpages, blogs
#### and more...
_We've shared a few examples for each towards the end._
---
class: inverse, center, middle
# Getting Started
---
# First steps...
### Install R/RStudio
You are recommended to use the [RStudio IDE](https://www.rstudio.com/products/rstudio/), but you do not have to.
--
### Install tidyverse and gapminder (data) and other required packages
```{r installation, eval=F, echo=T}
install.packages("tidyverse") # for data wrangling
install.packages("gapminder") # sample dataset
install.packages("rmarkdown", # Convert R Markdown documents into various formats
"knitr", # for dynamic report generation
"gganimate", # for animation
"gifski") # gganimate's dependency
```
```{r out.width='20%', fig.alt="R programming logo in blue", echo=FALSE}
#knitr::include_graphics("https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/R_logo.svg/1448px-R_logo.svg.png?20160212050515")
knitr::include_graphics("https://d33wubrfki0l68.cloudfront.net/57299a1dcd979c623325f11bf5e5ce60f3d4eb00/e4602/wp-content/uploads/2018/10/black.png")
```
```{r echo=FALSE, out.width='10%', fig.alt="tidyverse package hex from RStudio"}
knitr::include_graphics("https://www.rstudio.com/wp-content/uploads/2014/04/tidyverse.png")
```
```{r echo=FALSE, out.width='30%', fig.alt="gapminder logo from website (yellow bordered text) for gapminder package"}
knitr::include_graphics("https://www.gapminder.org/wp-content/themes/gapminder2/images/gapminder-logo.svg")
```
---
# Importing your data
```{r data-import, eval=FALSE, echo=TRUE}
library(tidyverse)
read_csv(file="my_data.csv",
col_names=T) # comma-separated values, as exported from excel/spreadsheets
read_delim(file="my_data.txt", col_names=T,
delim="//") # any delimitter
# Other useful packages
# readxl by Jenny Bryan
read_excel(path="path/to/excel.xls",
sheet=1,
range="A1:D50",
col_names=T)
```
---
# The Gapminder data
.center[
```{r echo=F, out.width='60%', fig.alt="Snapshot of the gapminder data health vs wealth in 2015"}
knitr::include_graphics("https://s3-eu-west-1.amazonaws.com/static.gapminder.org/GapminderMedia/wp-uploads/20161007154225/countries_health_wealth_2016_v71.png")
```
]
---
# Viewing your data
```{r data-str-run, echo=TRUE, eval=T}
library(tidyverse)
gapminder::gapminder # Data is in a cleaned up 'tibble' format by default
```
--
```{r eval=F}
str(gapminder) # Structure of the dataframe
head(gapminder) # Shows the top few observations (rows) of your data frame
glimpse(gapminder) # Info-dense summary of the data
```
---
# Viewing your data
**A quick tabulated snapshot**
```{r}
library(knitr)
kable(head(gapminder::gapminder), format = 'html')
```
---
# Viewing your data
**Your full data!**
```{r paged_table, layout="l-body-outset"}
library(rmarkdown)
paged_table(gapminder::gapminder)
```
---
# Viewing your data
**w/ dynamic filtering**
```{r eval=require('DT'), tidy=FALSE}
library(gapminder)
DT::datatable(head(gapminder, 1000),
fillContainer = FALSE, options = list(pageLength = 5))
```
---
# Tidying your data
.center[
```{r echo=F, out.width='70%', fig.alt="There are two sets of anthropomorphized data tables. The top group of three tables are all rectangular and smiling, with a shared speech bubble reading 'our columns are variables and our rows are observations!'. Text to the left of that group reads 'The standard structure of tidy data means that 'tidy datasets are all alike...' The lower group of four tables are all different shapes, look ragged and concerned, and have different speech bubbles reading (from left to right) 'my column are values and my rows are variables', 'I have variables in columns AND in rows', 'I have multiple variables in a single column', and 'I don’t even KNOW what my deal is.' Next to the frazzled data tables is text '...but every messy dataset is messy in its own way. -Hadley Wickham.'"}
knitr::include_graphics("https://github.com/allisonhorst/stats-illustrations/raw/main/rstats-artwork/tidydata_2.jpg")
```
]
---
# Tidying your data
```{r tidyr, echo=TRUE, eval=FALSE}
pivot_longer() # wide -> long
pivot_wider() # long -> wide
separate() # Separate 1 COLUMN -> many COLUMNS
unite() # Unite several COLUMNS -> 1 COLUMN
```
---
# Tidying your data
.center[
```{r echo=F, out.width='40%', fig.alt="GAdenBuie Tidyexplain gif for long to wide data transformation"}
knitr::include_graphics("https://github.com/gadenbuie/tidyexplain/raw/main/images/tidyr-pivoting.gif")
```
]
---
# Wrangling your data
```{r dplyr, echo=TRUE, eval=FALSE}
filter() # PICK observations by their values | ROWS
select() # PICK variables by their names | COLUMNS
mutate() # CREATE new variables w/ functions of existing variables | COLUMNS
transmute() # COMPUTE 1 or more COLUMNS but drop original columns
arrange() # REORDER the ROWS
summarize() # COLLAPSE many values to a single SUMMARY
group_by() # GROUP data into rows with the same value of variable (COLUMN)
```
---
# Wrangling your data
**w/ filter**
.center[
```{r echo=F, out.width='70%', fig.alt="Allison Horst dply::filter. Cartoon showing three fuzzy monsters either selecting or crossing out rows of a data table. If the type of animal in the table is 'otter' and the site is 'bay', a monster is drawing a purple rectangle around the row. If those conditions are not met, another monster is putting a line through the column indicating it will be excluded. Stylized text reads 'dplyr::filter() - keep rows that satisfy your conditions.'"}
knitr::include_graphics("https://github.com/allisonhorst/stats-illustrations/raw/main/rstats-artwork/dplyr_filter.jpg")
```
]
---
# Wrangling your data
```{r filter1, echo=TRUE, eval=F}
head(gapminder) # Snapshot of the dataframe
# Now, filter by year and look at only the data from the year 1962
filter(gapminder, year==1962)
# Can be rewritten using "Piping" %>%
gapminder %>% # Pipe ('then') operator to serially connect operations
filter(year==1962)
# Filter for China in 2002
gapminder %>%
filter(year==2002, country=="China")
```
---
# Wrangling your data
```{r filter2, echo=F, eval=T}
gapminder %>% # Pipe ('then') operator to serially connect operations
filter(year==1962)
# Filter for China in 2002
gapminder %>%
filter(year==2002, country=="China")
```
---
# Math Expressions
You can write LaTeX math expressions inside a pair of dollar signs, <br>
_e.g.,_ `$\alpha+\beta$` renders $\alpha+\beta$.
You can use the display style with double dollar signs (`$$`):
```{}
$$EWCos_{j} = Cos(v_{dis}(S),EWlogFC_{j})$$
```
$$EWCos_{j} = Cos(v_{dis}(S),EWlogFC_{j})$$
--
Check out a math-riddled manuscript written with RMarkdown:
[jravilab.github.io/connectivity_scores](//jravilab.github.io/connectivity_scores)
---
# Visualizing your data
```{r echo=T, eval=F, fig.width=8, fig.height=3}
ggplot(gapminder) +
aes(x=gdpPercap, y=lifeExp, size=pop, color=country) +
geom_point(show.legend=FALSE)
```
--
```{r echo=F, eval=T, out.width='60%'}
ggplot(gapminder) +
aes(x = gdpPercap, y = lifeExp, size = pop, color = country) +
geom_point(show.legend = FALSE)
```
---
# Visualizing your data
```{r echo=T, eval=F, fig.width=8}
ggplot(gapminder) +
aes(x=gdpPercap, y=lifeExp, size=pop, color=country) +
geom_point(show.legend=FALSE) +
{{facet_wrap(~year)}}
```
```{r echo=F, eval=T, out.width='60%'}
ggplot(gapminder) +
aes(x = gdpPercap, y = lifeExp, size = pop, color = country) +
geom_point(show.legend = FALSE) +
facet_wrap(~year)
```
---
# Visualizing your data
**The code**
```{r gapminder-static1, layout="l-body-outset", echo=TRUE, eval=F}
library(tidyverse)
library(gapminder)
static_plot <- ggplot(gapminder,
aes(gdpPercap, lifeExp,
size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() + theme_minimal() +
facet_wrap(~continent)
static_plot
```
---
# Visualizing your data
**The plot**
```{r gapminder-static2, layout="l-body-outset", fig.width=6, echo=F, eval=TRUE, message=FALSE, warning=FALSE}
library(tidyverse)
library(gapminder)
static_plot <- ggplot(gapminder,
aes(gdpPercap, lifeExp,
size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() + theme_minimal() +
facet_wrap(~continent)
static_plot
```
---
# Visualizing your data
**The code**
```{r gapminder-gganimate1, layout="l-body-outset", echo=TRUE, eval=F}
library(gganimate)
ggplot(gapminder,
aes(gdpPercap, lifeExp,
size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() + theme_minimal() +
facet_wrap(~continent) +
# Here comes the gganimate specific bits #<<
labs(title = 'Year: {frame_time}', # labels #<<
x = 'GDP per capita', y = 'life expectancy') + #<<
transition_time(year) + # the dynamic variable #<<
ease_aes('linear') #<<
```
---
# Visualizing your data
[**The plot**](https://jananiravi.github.io/workshop-tidyverse/gapminder/workshop-tidyverse-gapminder#gganimate)
```{r gapminder-gganimate2, layout="l-body-outset", out.width='80%', echo=F, eval=T, message=FALSE, warning=FALSE}
library(gganimate); library(tidyverse); library(gifski)
ggplot(gapminder,
aes(gdpPercap, lifeExp,
size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() + theme_minimal() +
facet_wrap(~continent) +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}', # labels
x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) + # the dynamic variable
ease_aes('linear')
```
---
```{r eval=F, out.width='100%', fig.height=6, eval=require('leaflet')}
library(leaflet)
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 17)
```
---
# Saving your data
```{r ggsave, eval=F, echo=TRUE}
library(tidyverse)
# Save your file name
plot1 <- "gapminder_static_plot.png"
# Save your absolute/relative path
my_full_path <- here("gapminder")
# To save as a tab-delimited text file ...
{{ggsave(filename=plot1,
plot=static_plot,
device="png",
path=my_full_path,
dpi=300) }}
```
---
# Have fun w/ programming
.center[
```{r echo=F, out.width='70%', fig.alt="Allison Horst's image on the phases of debugging!"}
knitr::include_graphics("https://github.com/allisonhorst/stats-illustrations/raw/main/other-stats-artwork/debugging.jpg")
```
]
---
# Do more with R
.pull-left[
- Draft a **Manuscript** | [jravilab.github.io/connectivity_scores](//jravilab.github.io/connectivity_scores)
- Create a **Website** | [jravilab.github.io](//jravilab.github.io)
- **Present** your data | e.g., [jravilab.github.io/R_intro](//jravilab.github.io/R_intro)
- Create a **Web application** | [jravilab.org/molevolvr](http://jravilab.org/molevolvr)
- Write your **Thesis** or an **e-Book** | [jravilab.github.io/lab_docs](https://jravilab.github.io/lab_docs)
- Material: [github.com/rladies-eastlansing](www.github.com/rladies-eastlansing)
```{r, out.width='40%', echo=FALSE, fig.alt="RStats community by Allison Horst: A person in a cape that reads 'code hero' who looks like they are flying through the air while typing on a computer while saying 'I’m doing a think all on my own!' The coder’s arms and legs have ropes attached to two hot air balloons lifting them up, with labels on the balloons including 'teachers', 'bloggers', 'friends', 'developers'. Below the code hero, several people carry a trampoline with labels 'support' and 'community' that will catch them if they fall"}
knitr::include_graphics('https://github.com/allisonhorst/stats-illustrations/raw/main/rstats-artwork/code_hero.jpg')
```
]
.pull-right[
```{r echo=F, eval=T, out.width='80%', fig.alt="A digital cartoon by Allison Horst with two illustrations: the top shows the R-logo with a scary face, and a small scared little fuzzy monster holding up a white flag in surrender while under a dark stormcloud. The text above says: at first I was like... The lower cartoon is a friendly, smiling R-logo jumping up to give a happy fuzzy monster a high-five under a smiling sun and next to colorful flowers. The text above the bottom illustration reads: but now it's like..."}
knitr::include_graphics("https://github.com/allisonhorst/stats-illustrations/raw/main/rstats-artwork/r_first_then.png")
```
]
---
# Acknowledgments
- GARD 2023 | Chelsie Boodoo
- Content by Dr. Janani Ravi | Janani.Ravi@cuanschutz.edu | [@janani137](//twitter.com/janani137) | [jravilab.github.io](//jravilab.github.io)
- Slides created via the R package [xaringan](https://github.com/yihui/xaringan) built with [remark.js](https://remarkjs.com), [knitr](https://yihui.org/knitr/), and [R Markdown](https://rmarkdown.rstudio.com).
- Cartoons from Allison Horst; Tidyverse GIFs by Garrick Aden-Buie.
- [JRaviLab](https://jravilab.github.io) | [Krishnan Lab](https://thekrishnanlab.org)
- [R-Ladies East Lansing](https://github.com/rladies-eastlansing) | [Women+ Data Science](https://github.com/women-plus-datascience) | [AsiaR](//github.com/AsiaR-community)
.pull-left[
### How to reach us
Jill.Bilodeaux@cuanschutz.edu <br>
Kewalin.Samart@cuanschutz.edu | [@KewalinSamart](//twitter.com/KewalinSamart) <br>
JRaviLab on [Twitter](//twitter.com/JRaviLab) | [GitHub](//github.com/JRaviLab) | [Mastodon](https://genomic.social/@jravilab)
]
.pull-right[
```{r echo=F, out.width='40%', fig.align='right', fig.alt="Baracktocat, GitHub Octocat with 'Yes we code' election themed poster"}
knitr::include_graphics("https://octodex.github.com/images/baracktocat.jpg")
```
]
You can access all relevant material pertaining to this workshop [here](https://github.com/jravilab/R_intro).