You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/extending-ggplot2.Rmd
+158Lines changed: 158 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1095,3 +1095,161 @@ What we are doing above is to intercept the `compute_layout` and `map_data` meth
1095
1095
2. Based on the FacetWrap implementation rewrite FacetTrans to take the strip.placement theme setting into account.
1096
1096
3. Think about which caveats there are in FacetBootstrap specifically related to adding multiple layers with the same data.
1097
1097
1098
+
## Creating new guides
1099
+
1100
+
Guides are closely related to scales and aesthetics, so an important part of guides is taking information from the scale and translating it to a graphic.
1101
+
This information is passed around inside guides as a `key` dataframe.
1102
+
For existing guides, you can glance at what a key contains by using the `get_guide_data()` function.
1103
+
Typical variables you may see in guides are the aesthetic mapped by the scale, such as the hexadecimal colours in the example below, what those aesthetic represent in the `.value` column and how they should be labelled in the `.label` column.
1104
+
Sometimes, the aesthetic is used in computations.
1105
+
To avoid interpreting the values and labels as aesthetics, it is customary to prefix these with `.`.
# Arguments passed on to the GuideKey$params field
1162
+
key = key, theme = theme, title = title, order = order, position = position,
1163
+
# Declare which aesthetics are supported
1164
+
available_aes = c("x", "y"),
1165
+
# Set the guide class
1166
+
super = GuideKey
1167
+
)
1168
+
}
1169
+
```
1170
+
1171
+
Our new guide can now be used inside the `guides()` function or as the `guide` argument in a position scale.
1172
+
1173
+
```{r key_example}
1174
+
#| fig.alt: >
1175
+
#| Scatterplot of engine displacement versus highway miles per
1176
+
#| gallon. The x-axis axis ticks are at 2.5, 3.5, 4.5, 5.5 and 6.5.
1177
+
1178
+
ggplot(mpg, aes(displ, hwy)) +
1179
+
geom_point() +
1180
+
scale_x_continuous(
1181
+
guide = guide_key(aesthetic = 2:6 + 0.5)
1182
+
)
1183
+
```
1184
+
1185
+
### Custom drawings
1186
+
1187
+
If we are feeling more adventurous, we can also alter they way guides are drawn.
1188
+
The majority of drawing code is in the `Guide$build_*()` methods, which is all orchestrated by the `Guide$draw()` method.
1189
+
For derived guides, such as the custom key guide we're extending here, overriding a `Guide$build_*()` method should be sufficient.
1190
+
If you are writing a completely novel guide that does not resemble the structure of any existing guide, overriding the `Guide$draw()` method might be wise.
1191
+
1192
+
In this example, we are changing the way the labels are drawn, so we should edit the `Guide$build_labels()` method.
1193
+
We'll edit the method so that the labels are drawn with a `colour` set in the key.
1194
+
In addition to the `key` and `params` variable we've seen before, we now also have an `elements` variable, which is a list of precomputed theme elements. We can use the `elements$text` element to draw a graphical object (grob) in the style of axis text.
1195
+
Perhaps the most finicky thing about drawing guides is that a lot of settings depend on the guide's `position` parameter.
0 commit comments