|
| 1 | +--- |
| 2 | +layout: slides |
| 3 | +title: R for reproducible scientific analysis |
| 4 | +subtitle: Why Use R? |
| 5 | +--- |
| 6 | + |
| 7 | +```{r, include=FALSE} |
| 8 | +library(ggplot2) |
| 9 | +theme_set(theme_bw()) |
| 10 | +source("tools/chunk-options.R") |
| 11 | +library(dplyr) |
| 12 | +gapminder <- tbl_df(read.csv("data/gapminder-FiveYearData.csv")) |
| 13 | +``` |
| 14 | + |
| 15 | +## Why R? |
| 16 | + |
| 17 | +* Powerful statistical analysis |
| 18 | +* and powerful visualisation |
| 19 | +* integrated elegantly |
| 20 | + |
| 21 | +## What We'll Accomplish |
| 22 | + |
| 23 | +* Get to know R and RStudio |
| 24 | +* Analyze a meaningful data set |
| 25 | +* Extract insights and deliver them visually |
| 26 | +* Leave ready to learn more R independently |
| 27 | + |
| 28 | +## R loves ingesting data |
| 29 | + |
| 30 | +``` |
| 31 | +gapminder <- read.csv( |
| 32 | + "data/gapminder-FiveYearData.csv", |
| 33 | + header=TRUE, |
| 34 | + sep=',') |
| 35 | +``` |
| 36 | + |
| 37 | +## Data w/ column names |
| 38 | + |
| 39 | +head(gapminder, 1) # Show me the first row |
| 40 | + |
| 41 | +country year pop continent lifeExp gdpPercap |
| 42 | +1 Afghanistan 1952 8425333 Asia 28.801 779.4453 |
| 43 | + |
| 44 | +## Quickly graph ... |
| 45 | +``` |
| 46 | +ggplot( |
| 47 | + data=gapminder, |
| 48 | + aes(x=lifeExp, y=gdpPercap) |
| 49 | +) + geom_point() |
| 50 | +``` |
| 51 | + |
| 52 | +## ... to see what we have |
| 53 | + |
| 54 | + |
| 55 | +## Let's graph more factors |
| 56 | +``` |
| 57 | +ggplot( |
| 58 | + data=gapminder, |
| 59 | + aes(x=year, y=lifeExp, by=country, colour=continent) |
| 60 | +) + geom_line() |
| 61 | + + geom_point() |
| 62 | +``` |
| 63 | + |
| 64 | +## Pretty! |
| 65 | + |
| 66 | + |
| 67 | +## dyplr gives us ... |
| 68 | +```{r} |
| 69 | +library(dplyr) |
| 70 | +cors <- gapminder %>% |
| 71 | + group_by(year) %>% |
| 72 | + summarise( |
| 73 | + gdpPercap.lifeExp = cor(gdpPercap, lifeExp), |
| 74 | + gdpPercap.pop = cor(gdpPercap, pop), |
| 75 | + pop.lifeExp = cor(pop, lifeExp)) |
| 76 | +``` |
| 77 | + |
| 78 | +## ... pairwise correlations |
| 79 | +``` |
| 80 | +head(cors, 1) |
| 81 | +Source: local data frame [1 x 4] |
| 82 | +year gdpPercap.lifeExp gdpPercap.pop pop.lifeExp |
| 83 | +1 1952 0.2780236 -0.02526041 -0.002724782 |
| 84 | +``` |
| 85 | + |
| 86 | +## Restructuring the table ... |
| 87 | +```{r} |
| 88 | +library(tidyr) |
| 89 | +tidy.cors <- cors %>% gather( |
| 90 | + variables, correlation, |
| 91 | + gdpPercap.lifeExp, gdpPercap.pop, |
| 92 | + pop.lifeExp) |
| 93 | +``` |
| 94 | +## ... a subtle art ... |
| 95 | +``` |
| 96 | +head(tidy.cors, 1) |
| 97 | +
|
| 98 | +Source: local data frame [1 x 3] |
| 99 | + year variables correlation |
| 100 | +1 1952 gdpPercap.lifeExp 0.2780236 |
| 101 | +``` |
| 102 | + |
| 103 | +## ... produces great results |
| 104 | + |
0 commit comments