-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday4_ggplotSlides.Rmd
More file actions
207 lines (155 loc) · 5.98 KB
/
day4_ggplotSlides.Rmd
File metadata and controls
207 lines (155 loc) · 5.98 KB
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
---
title: "`ggplot` graphics"
author: "ECON 122"
date: "Day 4"
output:
ioslides_presentation:
incremental: true
widescreen: true
keep_md: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse=TRUE, prompt=TRUE, eval=TRUE, message=F, include=T,comment=NULL, warning=FALSE, error=TRUE)
```
```{r packageCheck, include=FALSE}
mypacks <- c("ggplot2") # what packages are needed?
packs <- installed.packages() # find installed package list
install.me <- mypacks[!(mypacks %in% packs[,"Package"])] #what needs to be installed?
if (length(install.me) >= 1) install.packages(install.me, repos = "http://cran.us.r-project.org") # install (if needed)
lapply(mypacks, library, character.only=TRUE) # load all packages
```
## `ggplot2`: a grammar for graphics {.build}
- created by Hadley Wickham
- purposefully written to follow good graphing taxonomy
- allows you to create a graph in small, *readable* code chunks
- install the `ggplot2` package on your computer (if needed)
```{r, message = FALSE}
library(ggplot2)
packageVersion("ggplot2")
```
## `ggplot2` grammar {.build}
- Basic syntac is
```
ggplot(data) + # data
<geometry_funs>(aes(<variables>)) + # aesthetic variable mapping
<label_funs> + # add context
<facet_funs> + # add facets (optional)
<coordinate_funs> + # play with coords (optional)
<scale_funs> + # play with scales (optional)
<theme_funs> # play with axes, colors, etc (optional)
```
- can **layer** geometry functions
- can plot **stats** from raw data
## `Survey` data {.build}
- the dataset `Survey` provides student data from a survey:
```{r}
survey <- read.csv("http://raw.githubusercontent.com/mgelman/data/master/Survey.csv")
```
- Look first at GPA (Q15) and possible sci or non-sci major (Q16)
## Data
- The first layer sets the graphical environment
```{r, fig.height=3.5}
ggplot(data=survey)
```
## Aesthetics {.build}
- Aesthetics describes the mapping of variables to your graph
- for numeric, default scale is Cartesian
- `aes` aesthetics can also be given in the `ggplot` command
```{r, fig.height=3.5}
ggplot(data=survey, aes(x=Question.15))
```
## Geometry {.build}
- The geometry determines what form the plot has.
+ Here a `histogram` makes sense:
```{r, fig.height=3.5}
ggplot(data=survey, aes(x=Question.15)) + geom_histogram()
```
## EDA for data clean up {.build}
- Obviously there were some typos in the GPA data.
- Subset the data so only cases with a reasonable GPA are included.
```{r, fig.height=3.5}
survey <- survey[survey$Question.15 <=4.0 & survey$Question.15 >0,]
ggplot(data=survey, aes(x=Question.15)) + geom_histogram()
```
## Faceting {.build}
- Can add a third variable, science or non-sci major (Q16), with a facet:
```{r, fig.height=3.5}
ggplot(data=survey, aes(x=Question.15)) + geom_histogram() + facet_wrap(~Question.16)
```
## Another option {.build}
- Could also use side-by-side boxplots:
```{r, fig.height=3.5}
ggplot(data=survey, aes(y=Question.15, x=Question.16)) + geom_boxplot()
```
## `ggplot` and `NA`'s {.build}
- Need to omit `NA` rows from data if you want them omitted from your graphics
```{r, fig.height=3.5}
survey <- survey[!is.na(survey$Question.16),]
ggplot(data=survey, aes(y=Question.15, x=Question.16)) + geom_boxplot()
```
## Adjusting coordinates {.build}
- We can flip the x/y coordinates (`boxplot` always wants `x` to be categorical and `y` numeric)
```{r, fig.height=3.5}
ggplot(data=survey, aes(y=Question.15, x=Question.16)) + geom_boxplot() + coord_flip()
```
## Context! Add labels {.build}
- Options include `ggtitle` and `labs`, change text size too
```{r, fig.height=3.5}
ggplot(data=survey, aes(y=Question.15, x=Question.16)) + geom_boxplot() + coord_flip() +
labs(title="GPA by major", x="Major Area", y="GPA") +
theme(text=element_text(size=18))
```
## How to proceed {.build}
- Get to know basic command structure (`ggplot + geom`)
- Use cheat sheet to see `aes` options for each `geom`
- Then add context layers: labels, font sizes, etc
- `?theme`: non-data features (fonts, legends, axes)
- `?scale_`: scale (x,y,fill, shape) data features
## Scatterplot
- relationship between number of facebook friends (Q12) and GPA (Q15)
```{r, fig.height=3.5}
ggplot(survey, aes(x=Question.12,y=Question.15)) + geom_point()
```
## Scatterplot
Change symbol size and shape
```{r, fig.height=3.5}
ggplot(survey, aes(x=Question.12,y=Question.15)) + geom_point(size=3,shape=2)
```
## Scatterplot
Change symbol shape by `major` (Q16): requires `aes` argument!
```{r, fig.height=3.5}
ggplot(survey, aes(x=Question.12,y=Question.15)) +
geom_point(aes(shape=Question.16), size=3)
```
## Scatterplot
Change symbol color by `major` (Q16): requires `aes` argument!
- the `color` is the `discrete` variable "major", that is why `scale_color_discrete` is used
```{r, fig.height=3.5}
ggplot(survey, aes(x=Question.12,y=Question.15)) +
geom_point(aes(color=Question.16), size=3) + scale_color_discrete(name="major")
```
## Scatterplot
Change symbol size by number of tv hours/week (Q14): requires `aes` argument!
```{r, fig.height=3.5}
ggplot(survey, aes(x=Question.12,y=Question.15)) +
geom_point(aes(color=Question.16, size=Question.14)) + scale_color_discrete(name="major")
```
## Scatterplot
The `size` is the `continuous` variable tv, that is why `scale_size_continous` is used
```{r, fig.height=3.5}
ggplot(survey, aes(x=Question.12,y=Question.15)) +
geom_point(aes(color=Question.16, size=Question.14)) + scale_color_discrete(name="major") +
scale_size_continuous(name="tv hours/week")
```
## Scatterplot
What `scale` commands change legend names?
```{r, fig.height=3.5}
ggplot(survey, aes(x=Question.12,y=Question.15)) +
geom_point(aes(shape=Question.16, color=Question.14))
```
## Bar graphs
How does political comfort level (Q9) vary by religious group (Q8)?
```{r, fig.height=3.5, fig.width=8}
levels(survey$Question.8) <- c("not religious","religious, active", "religious, not active")
ggplot(survey, aes(x=Question.8, fill=Question.9)) + geom_bar(position="fill")
```