-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
315 lines (255 loc) · 7.59 KB
/
README.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
dev.args = list(png = list(type = "cairo")),
fig.retina = 2
)
```
# ggcolormeter
<!-- badges: start -->
[![](https://img.shields.io/badge/devel%20version-`r as.character(packageVersion('ggcolormeter'))`-gogreen.svg)](https://github.com/yjunechoe/ggcolormeter)
[![R-CMD-check](https://github.com/yjunechoe/ggcolormeter/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/yjunechoe/ggcolormeter/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/yjunechoe/ggcolormeter/branch/main/graph/badge.svg)](https://app.codecov.io/gh/yjunechoe/ggcolormeter?branch=main)
<!-- badges: end -->
The `{ggcolormeter}` package provides a single function [`guide_colormeter()`](https://yjunechoe.github.io/ggcolormeter/reference/guide_colormeter.html), which is a `{ggplot2}` color/fill legend **guide extension** in the style of a dashboard meter.
## Installation
```{r, eval=FALSE}
install.packages("ggcolormeter")
# or
remotes::install_github("yjunechoe/ggcolormeter")
```
## Simple usage
```{r simple-usage}
library(ggplot2)
library(ggcolormeter)
theme_set(theme_classic())
p <- ggplot(mtcars, aes(drat, hp)) +
geom_point(aes(color = mpg))
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter()
)
```
# Colormeter guide theme components
The colormeter guide has argument families for styling 5 distinct components of the guide:
1) [Size and aspect](https://github.com/yjunechoe/ggcolormeter#1-size-and-aspect)
2) [Arc](https://github.com/yjunechoe/ggcolormeter#2-arc)
3) [Label](https://github.com/yjunechoe/ggcolormeter#3-label)
4) [Dashboard](https://github.com/yjunechoe/ggcolormeter#4-dashboard)
5) [Frame](https://github.com/yjunechoe/ggcolormeter#5-frame)
Position of these theme elements is relative to the [guide-internal coordinate system](https://yjunechoe.github.io/ggcolormeter/reference/legend-coords.html), which you can inspect with the `debug = TRUE` argument.
## 1) Size and aspect
The colormeter legend is a bit pecular in that its size doesn't expand with more keys: the colormeter has a fixed size and shape, and its elements are packed inside it.
You can primarily control the size of the legend with `legend_size` and `aspect.ratio`
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(
legend_size = unit(3, "cm"),
aspect.ratio = .8
)
)
```
Note that the usual legend background is still present and different from the dashboard circle:
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(
legend_size = unit(3, "cm"),
aspect.ratio = .8
)
) +
theme(legend.background = element_rect(color = "red", fill = "pink"))
```
Most of the time you'd want to remove this legend background, as the dashboard serves that purpose:
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(
)
) +
theme(
legend.position = c(.85, .75),
legend.background = element_blank()
)
```
## 2) Arc
```{r}
formals(guide_colormeter)[grepl("arc", names(formals(guide_colormeter)))]
```
Non-positional aesthetic arguments for the color arc:
```{r arc-theme}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(
arc_width = 1/6, # thinner arcs
arc_gap = 1/3, # bigger gaps
arc_rounding = 0.03 # rounded corners
)
)
```
By default, label and dashboard radii are derived from `arc_radius`:
```{r arc-radius}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(
arc_radius = 1.2
)
)
```
You can use `arc_range` to set the start and end angles of the color meter, which may yield different shapes:
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(5), # half the arcs
guide = guide_colormeter(
arc_range = c(-pi/2, 0) # quarter circle
)
)
```
The defaults aren't great for when you change from the dashboard shape. Some manual adjustments may be desirable:
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(5),
guide = guide_colormeter(
arc_range = c(-pi/2, 0),
title_position = c(-.2, .2), # moves title left and up from center
legend_padding = grid::unit(0.7, "lines") # pads relative to legend label
)
)
```
## 3) Label
```{r}
formals(guide_colormeter)[grepl("label", names(formals(guide_colormeter)))]
```
By default, the dashboard radius is derived from `label_radius`:
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(
label_radius = 1.5
)
)
```
Like `ggplot2::guide_colorsteps()`, the argument `show.limits` controls labeling the limits of the scale:
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(
label_radius = 1.3,
show.limits = TRUE
)
)
```
## 4) Dashboard
```{r}
formals(guide_colormeter)[grepl("dashboard", names(formals(guide_colormeter)))]
```
`dashboard_radius` controls the radius of just the dashboard circle
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(
dashboard_radius = 1.2,
)
)
```
By default, the dashboard is clipped to the legend boundary, which can be turned off:
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(
title = "mpg<br><br>
<span style='color:darkgrey;font-size:12px'>miles per gallon</span>",
clip_dashboard = FALSE
)
) +
theme(legend.title = ggtext::element_markdown(vjust = -.6))
```
Non-positional aesthetic arguments for the dashboard:
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(
dashboard_fill = "skyblue",
dashboard_color = "steelblue",
dashboard_linetype = 5,
dashboard_linewidth = 4
)
)
```
## 5) Frame
```{r}
formals(guide_colormeter)[grepl("frame", names(formals(guide_colormeter)))]
```
Frames simply decorate the color arcs/bars:
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(
frame_color = "black",
frame_linewidth = .3
)
)
```
## Miscellaneous
Set `debug = TRUE` to inspect the internal legend coordinate system (for deciding on `dashboard_radius`, `arc_width`, `title_position`, etc.):
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(10),
guide = guide_colormeter(debug = TRUE)
)
```
Puttings labels inside the arc:
```{r}
p +
scale_color_viridis_c(
option = "inferno",
breaks = scales::breaks_pretty(5),
guide = guide_colormeter(
arc_width = 0.1,
label_radius = .7,
aspect.ratio = 1.1,
dashboard_color = NA
)
) +
theme(legend.position = c(.85, .75))
```
## Acknowledgments
- Thomas Lin Pedersen for [`{ggforce}`](https://github.com/thomasp85/ggforce), whose several unexported functions are used in this package.