-
Notifications
You must be signed in to change notification settings - Fork 0
/
14 -Building a Plot Layer by Layer.Rmd
167 lines (138 loc) · 3.66 KB
/
14 -Building a Plot Layer by Layer.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
---
title: "ggplot2 Elegant Graphics"
author: "Brandon Foltz"
date: "2023-03-15"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
```{r}
p <- ggplot(mpg, aes(displ, hwy))
p
p + geom_point()
p + layer(
mapping = NULL, # uses the default from the ggplot()
data = NULL, # uses the default from the ggplot()
geom = "point",
stat = "identity", # keep data as-is
position = "identity"
)
```
Different data on layers
```{r}
mod <- loess(hwy ~ displ, data = mpg)
grid <- tibble(displ = seq(min(mpg$displ), max(mpg$displ), length = 50))
grid$hwy <- predict(mod, newdata = grid)
grid
std_resid <- resid(mod) / mod$s
outlier <- filter(mpg, abs(std_resid) > 2)
outlier
```
```{r}
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_line(data = grid, color = "blue", size = 1.5) +
geom_text(data = outlier, aes(label = model))
```
```{r}
ggplot(mapping = aes(displ, hwy)) +
geom_point(data = mpg) +
geom_line(data = grid) +
geom_text(data = outlier, aes(label = model))
```
Aesthetic mappings
```{r}
# all the same plot result
ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point()
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = class))
ggplot(mpg, aes(displ)) +
geom_point(aes(y = hwy, color = class))
ggplot(mpg) +
geom_point(aes(displ, hwy, color = class))
```
```{r}
ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
theme(legend.position = "none")
# one line
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = class)) +
geom_smooth(method = "lm", se = FALSE) + #color class set in the point
theme(legend.position = "none")
```
Setting vs mapping
```{r}
ggplot(mpg, aes(cty, hwy)) +
geom_point(color = "darkblue")
ggplot(mpg, aes(cty, hwy)) +
geom_point(aes(color = "darkblue"))
```
```{r}
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(aes(color = "loess"), method = "loess", se = FALSE) +
geom_smooth(aes(color = "lm"), method = "lm", se = FALSE) +
labs(color = "Method")
```
```{r}
ggplot(mpg, aes(displ, hwy)) +
geom_rug()
```
```{r}
recent <- economics[economics$date > as.Date("2013-01-01"), ]
ggplot(recent, aes(date, unemploy)) + geom_line()
ggplot(recent, aes(date, unemploy)) + geom_step()
```
```{r}
# very handy
ggplot(mpg, aes(trans, cty)) +
geom_point() +
stat_summary(geom = "point", fun = "mean", color = "red", size = 4)
ggplot(mpg, aes(trans, cty)) +
geom_point() +
geom_point(stat = "summary", fun = "mean", color = "red", size = 4)
```
```{r}
ggplot(diamonds, aes(price)) +
geom_histogram(binwidth = 500)
ggplot(diamonds, aes(price)) +
geom_histogram(aes(y = after_stat(density)), binwidth = 500)
```
Standardize area
```{r}
ggplot(diamonds, aes(price, color = cut)) +
geom_freqpoly(binwidth = 500) +
theme(legend.position = "none")
ggplot(diamonds, aes(price, color = cut)) +
geom_freqpoly(aes(y = after_stat(density)), binwidth = 500) +
theme(legend.position = "none")
```
Position Adjustments
```{r}
dplot <- ggplot(diamonds, aes(color, fill = cut)) +
xlab(NULL) +
ylab(NULL) +
theme(legend.position = "none")
dplot + geom_bar()
dplot + geom_bar(position = "fill")
dplot + geom_bar(position = "dodge")
dplot + geom_bar(position = "identity", alpha = 1/2, color = "grey50")
ggplot(diamonds, aes(color, color = cut)) +
geom_line(aes(group = cut), stat = "count") +
xlab(NULL) +
ylab(NULL) +
theme(legend.position = "none")
```
```{r}
ggplot(mpg, aes(displ, hwy)) +
geom_point(position = "jitter")
ggplot(mpg, aes(displ, hwy)) +
geom_point(position = position_jitter(width = 0.05, height = 0.5))
ggplot(mpg, aes(displ, hwy)) +
geom_jitter(width = 0.05, height = 0.5)
```