generated from r4ds/bookclub-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
06.Rmd
247 lines (177 loc) · 3.62 KB
/
06.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
# Drawing graphs.
**Learning objectives:**
- Learn how to make Exploratory Data Analysis
- use of the `plot()` function and `{ggplot2}` package
## Overview
Exploratory data analysis is one of the most important part of the data analysis.
The book expands on using the base r `plot()` function for **data visualization**. Here we have a look at how to make the same plots with `{ggplot2}` and the `{tidyverse}` syntax.
## John Snow’s cholera project
```{r message=FALSE,warning=FALSE}
library(tidyverse)
```
```{r}
library(HistData)
# ?HistData
SnowMap()
```
```{r}
data("Snow.deaths")
Snow.deaths%>%
ggplot(aes(x,y))+
geom_point()
```
```{r}
data("Snow.deaths")
data("Snow.streets")
plot(Snow.deaths[,c("x","y")],
col="red", pch=19, cex=.7)
slist <- split(Snow.streets[,c("x","y")],
as.factor(Snow.streets[,"street"]))
?invisible
invisible(lapply(slist, lines, col="black"))
```
## Data used for making the plots
- Fibonacci sequence
- aflsmall
- afl2
- parenthood
- afl.finalists
A simple intro to how to make a plot with the Fibonacci sequence data:
```{r}
Fibonacci <- c( 1,1,2,3,5,8,13 )
plot( Fibonacci )
```
## Histograms
```{r}
load( "./data/aflsmall.Rdata" )
# install.packages("lsr")
library(lsr)
who()
```
```{r}
afl.margins[1:10]
```
```{r}
afl.margins_df <- data.frame(afl.margins=afl.margins)
afl.margins_df%>%
ggplot(aes(x=afl.margins))+
geom_histogram(binwidth = 8,color="white",fill="steelblue")
```
## Stem and Leaf plots
?stem()
```{r}
# ?stem()
x <- c( 1,1,2,3,5,8,13 )
stem(x,
scale = 2,
width = 10, atom = 1e-08)
```
```{r}
islands_df <- data_frame(values=islands,islands=names(islands))
```
```{r}
islands_df%>%
ggplot(aes(values))+
geom_histogram()+
coord_flip()+
scale_x_reverse()
```
```{r}
stem(islands)
stem(log10(islands))
```
```{r}
stem( afl.margins )
```
## Boxplots
```{r}
summary( afl.margins )
```
```{r}
afl.margins_df %>%
ggplot(aes(y=afl.margins))+
geom_boxplot()
```
```{r}
knitr::include_graphics("images/ch6_boxplot.png")
```
```{r}
load( "./data/aflsmall2.Rdata" )
who( TRUE )
```
```{r}
head( afl2 )
```
```{r fig.cap="AFL winning margins for the 24 years from 1987 to 2010 inclusive"}
afl2_df <- data.frame(afl2)
afl2_df %>%
ggplot(aes(x=year,
y=margin,group=year))+
geom_boxplot()+
geom_hline(aes(yintercept=(mean(margin))),color="red")
```
## Scatterplots
```{r}
load( "./data/parenthood.Rdata" )
parenthood%>%head
```
```{r}
parenthood%>%
ggplot(aes(x=dan.sleep,y=dan.grump))+
geom_point()
```
```{r}
parenthood%>%
ggplot(aes(x=dan.sleep,y=dan.grump))+
geom_point(shape=21,stroke=0.5,fill="pink")+
geom_smooth(method = "lm",se=F)+
tvthemes::theme_avatar()
# ggthemes::theme_fivethirtyeight()
```
```{r}
cor(x=parenthood)
```
```{r}
pairs(parenthood)
```
```{r}
pairs( formula = ~ dan.sleep + baby.sleep + dan.grump,
data = parenthood)
```
## Bar Graphs
?tabulate()
```{r}
freq <- tabulate( afl.finalists )
freq
```
```{r}
afl.finalists_df <- data.frame(afl.finalists)
afl.finalists_df %>% count(afl.finalists)
```
```{r}
afl.finalists_df %>%
count(afl.finalists) %>%
ggplot(aes(x=afl.finalists,y=n))+
geom_col()+
theme(axis.text.x = element_text(angle = 45))
```
```{r}
afl.finalists_df %>%
count(afl.finalists) %>%
ggplot(aes(y=fct_reorder(afl.finalists,n),x=n))+
geom_col()+
labs(y="afl.finalists")
```
## Conclusions
More features can be added specifying:
- theme() options
- saving as png/jpeg file
## Meeting Videos
### Cohort 1
`r knitr::include_url("https://www.youtube.com/embed/URL")`
<details>
<summary> Meeting chat log </summary>
```
LOG
```
</details>