-
Notifications
You must be signed in to change notification settings - Fork 3
/
02-plots.Rmd
316 lines (224 loc) · 9.72 KB
/
02-plots.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
# EXAMINING A DISTRIBUTION
**Chapter Links**
* [Chapter 2 Slide Show](http://tysonbarrett.com/EDUC-6600/Slides/u01_Ch2_DataViz.html#1)
**Assingment Links**
* [Unit 1 Assignment - Write Up Skeleton](https://usu.box.com/s/bn0m44cp5zrkev3ppjdilfsvic4bubvu)
* [Unit 1 Assignment - Rmd Skeleton](https://usu.box.com/s/zjjwexn827ziifxlcoe6emr2brfifk0x)
* [Inho's Dataset - Excel format](https://usu.box.com/s/hyky7eb24l6vvzj2xboedhcx1xolrpw1)
```{r global_options, include=FALSE}
# set global chunk options...
# this changes the defaults so you don't have to repeat yourself
knitr::opts_chunk$set(comment = NA,
cache = TRUE,
echo = TRUE,
warning = FALSE,
message = FALSE)
```
## Example: Ihno's Experiment {-}
This is the same dataset used in the previous chapter and in most of the textbook's section C questions.
### Required Packages {-}
```{r load_libraries}
library(tidyverse) # Loads several very helpful 'tidy' packages
library(readxl) # Read in Excel datasets
library(furniture) # Nice tables (by our own Tyson Barrett)
library(psych) # Lots of nice tid-bits
```
### Data Import and Wrangling {-}
The previous chapter build the following code to read in the dataset and prepare it for analysis.
```{r, eval=FALSE}
data_clean <- read_excel("Ihno_dataset.xls") %>%
dplyr::rename_all(tolower) %>%
dplyr::mutate(genderF = factor(gender,
levels = c(1, 2),
labels = c("Female",
"Male"))) %>%
dplyr::mutate(majorF = factor(major,
levels = c(1, 2, 3, 4,5),
labels = c("Psychology",
"Premed",
"Biology",
"Sociology",
"Economics"))) %>%
dplyr::mutate(reasonF = factor(reason,
levels = c(1, 2, 3),
labels = c("Program requirement",
"Personal interest",
"Advisor recommendation"))) %>%
dplyr::mutate(exp_condF = factor(exp_cond,
levels = c(1, 2, 3, 4),
labels = c("Easy",
"Moderate",
"Difficult",
"Impossible"))) %>%
dplyr::mutate(coffeeF = factor(coffee,
levels = c(0, 1),
labels = c("Not a regular coffee drinker",
"Regularly drinks coffee"))) %>%
dplyr::mutate(hr_base_bps = hr_base / 60) %>%
dplyr::mutate(anx_sum = rowsums(anx_base, anx_pre, anx_post)) %>%
dplyr::mutate(hr_mean = rowmeans(hr_base + hr_pre + hr_post)) %>%
dplyr::mutate(statDiff = statquiz - exp_sqz)
```
```{r, include=FALSE}
data_clean <- read_excel("data/Ihno_dataset.xls") %>%
dplyr::rename_all(tolower) %>%
dplyr::mutate(genderF = factor(gender,
levels = c(1, 2),
labels = c("Female",
"Male"))) %>%
dplyr::mutate(majorF = factor(major,
levels = c(1, 2, 3, 4,5),
labels = c("Psychology",
"Premed",
"Biology",
"Sociology",
"Economics"))) %>%
dplyr::mutate(reasonF = factor(reason,
levels = c(1, 2, 3),
labels = c("Program requirement",
"Personal interest",
"Advisor recommendation"))) %>%
dplyr::mutate(exp_condF = factor(exp_cond,
levels = c(1, 2, 3, 4),
labels = c("Easy",
"Moderate",
"Difficult",
"Impossible"))) %>%
dplyr::mutate(coffeeF = factor(coffee,
levels = c(0, 1),
labels = c("Not a regular coffee drinker",
"Regularly drinks coffee"))) %>%
dplyr::mutate(hr_base_bps = hr_base / 60) %>%
dplyr::mutate(anx_sum = rowsums(anx_base, anx_pre, anx_post)) %>%
dplyr::mutate(hr_mean = rowmeans(hr_base + hr_pre + hr_post)) %>%
dplyr::mutate(statDiff = statquiz - exp_sqz)
```
```{r}
tibble::glimpse(data_clean)
```
---------------------------------------
## Frequency Tables
These tables are best for showing the breakdown of a sample across the levels of a **single CATEGORICAL** variable *(factor)*. They help pick out the mode(s) and identify unusual or impossible values [@R-furniture].
```{r}
data_clean %>%
furniture::tableF(majorF)
```
Phobia is one variable that is in between being categorical and continuous.
```{r}
data_clean %>%
furniture::tableF(phobia)
```
If a variable has many possible values (i.e. it is more continuous than categorical), you can add an option to tell how many values `n = #` you want displayed in the table, cutting out all the middle values.
```{r}
data_clean %>%
furniture::tableF(hr_post, n = 10)
```
---------------------------------------
## Bar Charts
These plots are best for showing the breakdown of a sample across the levels of a **single CATEGORICAL** variable. They help pick out the mode(s) and identify unusual or impossible values [@R-ggplot2].
> There must be SPACE between the bars!
```{r}
data_clean %>%
ggplot(aes(majorF)) +
geom_bar()
```
Here is an example of a two-level variable
```{r}
data_clean %>%
ggplot(aes(coffeeF)) +
geom_bar()
```
Here is an example of an 11-level variable
```{r}
data_clean %>%
ggplot(aes(phobia)) +
geom_bar()
```
--------------------------------
## Histograms
These plots are best for showing the distribution of a **single CONTINUOUS** variable. They help visually determine the shape, center *[mean, median, mode(s)]*, spread *[stadard deviation, range]*, and identify extreme or impossible values [@R-ggplot2].
```{r}
data_clean %>%
ggplot(aes(phobia)) +
geom_histogram()
```
> There must NOT be SPACE between the bars!
Notice how the bars do not touch. This is because the default includes too many bars, many of which are not included in the dataset.
There are TWO ways specify something other than the default:
(@) Change the NUMBER of bins: `bins = #`
(@) Change the WIDTH of the bins: `binwidth = #`
> If you try to do BOTH, only the first option will be used and the second will be ignored.
### Change the Number of Bins
```{r}
data_clean %>%
ggplot(aes(phobia)) +
geom_histogram(bins = 8)
```
### Change the Bin Width
```{r}
data_clean %>%
ggplot(aes(phobia)) +
geom_histogram(binwidth = 5)
```
### Make Seperate Panels -by- a Factor
To make separate plots based on another categorical variable, a **FACTOR**, we need to add a layer to the plot.
> Reminder: Steps before the `ggplot()` are combined with pipes `%>%`, whereas layers of the plot are combined with the addition symbol `+`.
```{r}
data_clean %>%
ggplot(aes(mathquiz)) +
geom_histogram(binwidth = 4) +
facet_grid(. ~ coffeeF)
```
```{r}
data_clean %>%
ggplot(aes(mathquiz)) +
geom_histogram(binwidth = 4) +
facet_grid(coffeeF ~ .)
```
------------------------------
## Percentiles
The `quantile(probs = c(#, #, ..., #))` function in the base download of R may be used to request the deciles, quartiles, or other percentiles for a `vector` for numbers [@R-base].
To use this function, we have to first **pull** out one variable of interest from our dataset *(data.frame)* and make it into a vector. This is done with a `dplyr::pull(varname)` step [@R-dplyr].
### Deciles
Deciles break the variable's values into 10% chunks.
```{r}
data_clean %>%
dplyr::pull(statquiz) %>%
quantile(probs = c(0, .10, .20, .30, .40, .50, .60, .70, .80, .90, 1))
```
#### With Missing Values {-}
If the variable have any missing values, an error message with be outputted instead of what you expect.
```{r, eval = FALSE}
data_clean %>%
dplyr::pull(mathquiz) %>%
quantile(probs = c(0, .10, .20, .30, .40, .50, .60, .70, .80, .90, 1))
```
`Error in quantile.default(., probs = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, : missing values and NaN's not allowed if 'na.rm' is FALSE`
#### Option to Ignore Missing Values {-}
To avoid getting this message and ignore the missing values, use the `na.rm = TRUE` option.
```{r}
data_clean %>%
dplyr::pull(mathquiz) %>%
quantile(probs = c(0, .10, .20, .30, .40, .50, .60, .70, .80, .90, 1),
na.rm =TRUE)
```
### Quartiles
Quartiles break variable's values into 4 chunks. If you include 0 *(minimum)* and 1 *(maximum)* you will get the **Five Number Summary**, which is:
* min, the minimum
* Q1, the 25th percentile
* Q2, the 50th percentile or the Median
* Q3, the 75th percentile
* max, the maximum
These values are used to create boxplots in the next chapter.
```{r}
data_clean %>%
dplyr::pull(statquiz) %>%
quantile(probs = c(0, .25, .50, .75, 1))
```
### Other Percentiles
You may also include any other percentile between 0 and 1.
```{r}
data_clean %>%
dplyr::pull(statquiz) %>%
quantile(probs = c(.01, .05, .173, .90))
```