Skip to content

Commit e2546e6

Browse files
committed
ggplot2 learning
1 parent a6dabea commit e2546e6

File tree

1 file changed

+166
-14
lines changed

1 file changed

+166
-14
lines changed

R/ggplot.md

+166-14
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,73 @@
11

22
## Background
33

4-
Remember that R is loose about args being positional or keyword. I'm going to use the keyword versions here because I think it is clearer
4+
* R is loose about args being positional or keyword. I'm going to use the keyword versions here because I think it is clearer
55
* R allows you to overload built-in operators (obvs not advised etc. etc.)
66
```r
77
rm('+')
88
'+' <- function(x,y) paste(x,y,sep="")
99
```
1010
* ggplot2 overloads `+` for combining objects of types it recognises.
1111
* `+` functions as a sort of "merge" function
12+
* Potential gotcha: If you give `ggplot` data that is not a `data.frame` it will coerce it to a `data.frame` via the `fortify()` function.
1213

13-
## ggplot(data = ?, mapping = ?)
14+
## ggplot()
1415

16+
* initializes a plot
1517
* Returns an S3 object which has classes `gg`, `ggplot`
16-
* Creates the base plot
1718
* Takes a data frame and an optional aesthetic mapping
1819
* aesthetic mappings can also be set on the individual layers
19-
* `ggplot` doesn't actually understand any aesthetics itself - it just passes them down to the layers who do
20+
* `ggplot` doesn't actually do anything with the data or aesthetics itself - it just makes them available for layers to (optionally) inherit them.
2021
22+
```r
23+
my_plot <- ggplot(data = somedata, mapping = aes(...))
2124
```
22-
ggplot S3 object:
23-
s3 classes: gg, ggplot
2425
25-
data: a reference to the data frame you passed in
26-
mapping: a reference to the mapping you passed in
2726
27+
```yaml
28+
# anatomy of a ggplot object
29+
ggplot_S3_object:
30+
s3_classes: gg, ggplot
31+
32+
data: # a reference to the data frame arg you passed in
33+
mapping: # a reference to the mapping arg you passed in
34+
35+
# these attributes are filled in by functions that you merge into this
36+
# object with `+`
2837
layers:
38+
- geom: NULL
39+
stat: NULL
40+
data: NULL
41+
mapping: NULL
42+
position: NULL
43+
params: list()
44+
inherit.aes: TRUE
45+
check.aes: TRUE
46+
check.param: TRUE
47+
show.legend: NA
48+
key_glyph: NULL
49+
layer_class: Layer
50+
- geom: NULL
51+
stat: NULL
52+
data: NULL
53+
mapping: NULL
54+
position: NULL
55+
params: list()
56+
inherit.aes: TRUE
57+
check.aes: TRUE
58+
check.param: TRUE
59+
show.legend: NA
60+
key_glyph: NULL
61+
layer_class: Layer
2962
scales:
30-
theme:
3163
coordinates:
3264
facet:
3365
plot_env:
3466
labels:
67+
theme:
3568
```
3669
37-
## aes(key = value, ...)
70+
## aes()
3871
3972
* Build an aesthetic map (map in the sense of dictionary alike object)
4073
* the map is an S3 class
@@ -46,7 +79,7 @@ ggplot S3 object:
4679
* You can save the mapping either
4780
1. at the "root" of the graph by passing it to `ggplot()` - this makes this aesthetic mapping available to all subsequent layers unless overrideen by the particular layer
4881
* this is just a developer convenience to allow you to reuse an aesthetic mapping easily
49-
2. or you pass it to a specific layer by passing it to a `geom_*()` function
82+
2. or you pass it to a specific layer i.e. `layer()` or one of its short-cut functions
5083
* if you pass a mapping in both places they will be combined by default (but this can be controlled by an arg to the layer functions)
5184
5285
```r
@@ -67,12 +100,131 @@ ggplot(data = rts_data, mapping = aes()) +
67100
geom_point(mapping = my_mapping)
68101
```
69102

70-
## geom_*
103+
## layer()
104+
105+
A layer is a combination of data, stat and geom with a potential position adjustment. Usually layers are created using geom_* or stat_* calls but it can also be created directly using this function.
71106

107+
```
108+
layer(
109+
geom = NULL,
110+
stat = NULL,
111+
data = NULL,
112+
mapping = NULL,
113+
position = NULL,
114+
params = list(),
115+
inherit.aes = TRUE,
116+
check.aes = TRUE,
117+
check.param = TRUE,
118+
show.legend = NA,
119+
key_glyph = NULL,
120+
layer_class = Layer
121+
)
122+
```
123+
124+
```r
125+
# geom calls are just a short cut for layer
126+
ggplot(mpg, aes(displ, hwy)) + geom_point()
127+
128+
# shortcut for
129+
130+
ggplot() +
131+
layer(
132+
data = mpg,
133+
mapping = aes(x = displ, y = hwy),
134+
geom = "point",
135+
stat = "identity",
136+
position = "identity",
137+
params = list(na.rm = FALSE)
138+
)
139+
140+
# use a function as data to plot a subset of global data
141+
ggplot(mpg, aes(displ, hwy)) +
142+
layer(geom = "point", stat = "identity", position = "identity",
143+
data = head, params = list(na.rm = FALSE)
144+
)
145+
```
146+
147+
148+
## geom_*, stat_*
149+
150+
* These functions are just shortcuts for the `layer()` function
72151
* A collection of functions whose names being with `geom_`
73-
* They create "layers" on your plot
74-
* each layer function understands a different set of aesthetics (i.e. the values in the aesthetic mapping)
152+
* They create "layers" on your plot and fill in some values for you e.g. the "geom" property of layers
153+
* each "geom" understands a different set of aesthetics (i.e. the values in the aesthetic mapping)
75154

76155
* they return an instance of an Environment (an S3 object which inherits from `LayerInstance Layer ggproto gg`
77156
* The return value is merged into the plot object via the overloaded `+` operator
78157

158+
```
159+
geom_point(
160+
mapping = NULL,
161+
data = NULL,
162+
stat = "identity",
163+
position = "identity",
164+
..., # extra args are passed on to layer()
165+
# these can be things like
166+
# color = "red"
167+
# size
168+
169+
na.rm = FALSE,
170+
show.legend = NA,
171+
inherit.aes = TRUE
172+
)
173+
174+
geom_point() understands the following aesthetics (required aesthetics are in bold):
175+
x (required)
176+
y (required)
177+
alpha
178+
colour
179+
fill
180+
group
181+
shape
182+
size
183+
stroke
184+
```
185+
186+
Q: so do aesthetics get passed to the geom or something else?
187+
188+
189+
## themes (theme_* functions)
190+
191+
* ggplot2 has a few built-in themes
192+
* the `theme_*` functions are mostly constructors which build S3 object instances of `S3 theme gg`)
193+
* the `+` operator saves the theme into the main plot object
194+
* some of the `theme_*` functions let you get an existing theme and tweak bits of it
195+
196+
```r
197+
ggplot(data = rts_data, mapping = aes()) +
198+
geom_point(mapping = my_aes) +
199+
theme_bw()
200+
```
201+
202+
## Anatomy of a full ggplot call
203+
204+
A plot needs
205+
206+
1. A base
207+
1. 1+ layers. Each layer has
208+
1. Data
209+
1. An aesthetic mapping
210+
1. A geometry
211+
1. A statistical transformation
212+
1. A position adjustment
213+
1. 1+ scales
214+
1. A coordinate system
215+
1. Faceting specification
216+
what gets faceted? layers?
217+
is faceting applied to the plot or to a particular layer?
218+
219+
1. a theme
220+
```r
221+
ggplot(
222+
data = rts_data,
223+
mapping = aes() # mapping inherited by all layers
224+
) +
225+
geom_point(
226+
mapping = my_aes,
227+
position = position_identity()
228+
) +
229+
theme_bw()
230+
```

0 commit comments

Comments
 (0)