Repository navigation
Expand file tree
/
Copy pathtutorial.Rmd
More file actions
271 lines (217 loc) · 6.6 KB
/
Copy pathtutorial.Rmd
File metadata and controls
271 lines (217 loc) · 6.6 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
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
---
title: "ggplot2 tutorial"
author: "Amelia McNamara"
date: "April 22, 2015"
output: ioslides_presentation
---
## ggplot2
ggplot2 is an R package by Hadley Wickham that lets you make beautiful R graphics (relatively) easily.
It's part of the Hadleyverse, which I recommend everyone get to know (dplyr, reshape2, stringr, lubridate... and many more).
The name ggplot2 refers to a famous book on data visualization theory called The Grammar of Graphics.
```{r, echo=FALSE, message=FALSE, warning=FALSE}
require(knitr)
opts_chunk$set(cache=TRUE, warning=FALSE, message=FALSE, fig.align='center')
```
## Getting started
First, we need to install and load the package,
```{r}
#install.packages("ggplot2")
library(ggplot2)
```
## Diamonds data
To start, I'm going to use the diamonds data that comes with ggplot2,
```{r}
str(diamonds)
```
## qplot()-- the easy way out
```{r, plot.height=2}
qplot(carat, data=diamonds)
```
## ggplot2 syntax
```{r, eval=FALSE}
qplot(carat, data=diamonds)
```
qplot() performs similar functionality to the base R graphics function plot(). But it aleady might seem a little different, because we're not using the $ operator.
Instead, you're listing the name of the variable(s) and then telling R where to "look" for that variable with "data=". This is like what we do when modeling using functions like lm().
## More qplot()
```{r}
qplot(clarity, fill=cut, data=diamonds)
```
## ggplot()
But, in order to really harness the power of ggplot2 you need to use the more general ggplot() command. The idea of the package is you can "layer" pieces on top of a plot to build it up over time.
You always need to use a ggplot() call to initialize the plot. I usually put my dataset in here, and at least some of my "aesthetics." But, one of the things that can make ggplot2 tough to understand is that there are no hard and fast rules.
```{r}
p1 <- ggplot(aes(x=clarity, fill=cut), data=diamonds)
```
If you try to show p1 at this point, you will get "Error: No layers in plot." This is because we haven't given it any geometric objects yet.
## geoms
In order to get a plot to work, you need to use "geoms" (geometric objects) to specify the way you want your variables mapped to graphical parameters.
```{r, message=FALSE, fig.height=3}
p1 + geom_bar()
```
## geoms have options
```{r}
p1 + geom_bar(position="dodge")
```
## Lots of options
```{r}
p1 + geom_bar(position="fill")
```
## Two variables
```{r}
p2 <- ggplot(aes(x=carat, y=depth), data=diamonds)
p2 + geom_point()
```
## Same data, different geom
```{r}
p2 + geom_bin2d()
```
## Saving your work (or not)
Notice that I'm not saving these geom layers-- I'm just running
p2 + [something]
to see what happens. But, I can save the new version to start building up my plot,
```{r}
p2 <- p2 + geom_bin2d()
```
## Better labels
```{r}
p2 <- p2 + xlab("Carat") + ylab("Depth") +
guides(fill=guide_legend(title="Number of diamonds"))
p2
```
## Different breaks
```{r}
p2 + scale_fill_continuous(breaks=c(1500, 2500, 3500,4500))
```
## Log scale
```{r}
p2 + scale_fill_continuous(trans="log")
```
## Log scale, different breaks
```{r}
p2 + scale_fill_continuous(trans="log", breaks=c(1,10,100,1000,5000))
```
## Faceting (wrapping)
```{r}
p2 + facet_wrap(~color)
```
## Faceting (grid)
```{r}
p2 + facet_grid(color~clarity)
```
## Working with dates
```{r}
# install.packages("lubridate")
# install.packages("dplyr")
library(lubridate)
library(dplyr)
```
## Data with dates
```{r}
econ <- economics
str(econ)
econ$date <- ymd(econ$date)
old <- filter(econ, year(date)<"1970")
old$date <- ymd(old$date)
```
##
```{r}
p3 <- ggplot(data=old) + geom_line(aes(x=date, y=pce))
p3
```
## For nicer date formatting
```{r}
# install.packages("scales")
library(scales)
```
##
```{r}
p3 +
scale_x_datetime(breaks = date_breaks("6 months"),
labels = date_format("%b %Y"))
```
##
```{r}
p3 +
scale_x_datetime(breaks = date_breaks("6 months"),
labels = date_format("%B %y"))
```
## Working with geographic data
```{r}
#install.packages("ggmap")
library(ggmap)
```
##
```{r}
str(crime) # crime data from ggmap package
```
##
```{r}
midlat <- mean(crime$lat, na.rm=TRUE)
midlon <- mean(crime$lon, na.rm=TRUE)
m1 <- get_map(source='stamen', location=c(midlon, midlat),
maptype="watercolor", zoom=11)
p4 <- ggmap(m1)
p4
```
## With points
```{r}
p4 + geom_point(aes(x=lon, y=lat), data=crime)
```
##
```{r}
p4 + geom_hex(aes(x=lon, y=lat), data=crime, alpha=0.8) +
guides(fill=guide_legend(alpha=0.5, title="Number of crimes")) +
xlab("") + ylab("") + theme(axis.line=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank())
```
## Many layers
```{r, eval=FALSE}
baseplot <- ggplot() +
aes(x=citystate, y=Freq, fill = Response, order=Response) +
facet_wrap(~year, nrow=3)+geom_bar(data = trial2$neg, stat = "identity") +
scale_fill_manual(breaks=c("Not at all satisfied", "2", "3", "4",
"Extremely satisfied"), values=colorsB,
name="Response") +
geom_bar(data = trial2$pos, stat = "identity") + coord_flip() +
ggtitle("Community satisfaction") + xlab("") +ylab("") +
scale_y_continuous(limits=c(-0.5, 1),
breaks=seq(from=-0.5, to=0.75, by=0.25),
labels=c("50%", "25%", "0", "25%", "50%", "75%")) +
theme(legend.text=element_text(size=14),
legend.title=element_text(size=16),
axis.text=element_text(size=14),
strip.text=element_text(size=14))
baseplot
```
## {.flexbox .vcenter}
<img src="communitysat.jpg" width="380">
## Colors
```{r}
# install.packages("RColorBrewer")
library(RColorBrewer)
display.brewer.all(n=5, type="qual")
```
##
```{r}
p1 + geom_bar(position="dodge") + scale_fill_brewer(palette="Dark2")
```
## Saving your work
```{r, echo=FALSE}
p4 <- p4 + geom_hex(aes(x=lon, y=lat), data=crime, alpha=0.8) +
guides(fill=guide_legend(alpha=0.5, title="Number of crimes")) +
xlab("") + ylab("") + theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())
```
```{r, eval=FALSE}
ggsave("beautiful_hex_map.jpg", plot=p4)
```
## Resources for ggplot2
* [ggplot2 cheatsheet](http://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf)
* [R graphics cookbook](http://www.cookbook-r.com/Graphs/)