Skip to content

Commit 21b5579

Browse files
clauswilkehadley
authored andcommitted
Enable more styling options for guide_colorbar(). (#2541)
* enable more styling options for guide_colorbar(). * Fix typo in News * fix issues due to rebase, convert to tidyverse coding style * improve NEWS.md, add visual test cases
1 parent d25ae78 commit 21b5579

6 files changed

+249
-13
lines changed

NEWS.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,19 @@ up correct aspect ratio, and draws a graticule.
316316
widths that are proportional to the base font size. One relative font size
317317
was eliminated. (@clauswilke)
318318

319+
### Guides
320+
321+
* Make `guide_colorbar()` more configurable: enable styling of tick marks and
322+
drawing of frame around the color bar via new arguments `ticks.colour`,
323+
`ticks.linewidth`, `frame.colour`, `frame.linewidth`, and `frame.linetype`.
324+
(@clauswilke)
325+
326+
* `guide_colorbar()` now uses `legend.spacing.x` and `legend.spacing.y` correctly,
327+
and it can handle multi-line titles. Minor tweaks were made to `guide_legend()`
328+
to make sure the two legend functions behave as similarly as possible.
329+
(@clauswilke, #2397 and #2398)
330+
331+
319332
### Other
320333

321334
* Clipping to the plot panel is now configurable, through a `clip` argument
@@ -343,11 +356,6 @@ up correct aspect ratio, and draws a graticule.
343356
output from `ggplot_build()`. Also, the object returned from
344357
`ggplot_build()` now has the class `"ggplot_built"`. (#2034)
345358

346-
* `guide_colorbar()` now correctly uses `legend.spacing.x` and `legend.spacing.y`,
347-
and it can handle multi-line titles. Minor tweaks were made to `guide_legend()`
348-
to make sure the two legend functions behave as similarly as possible.
349-
(@clauswilke, #2397 and #2398)
350-
351359
* `map_data()` now works when purrr is loaded (tidyverse#66)
352360

353361
* New functions `summarise_layout()`, `summarise_coord()`, and `summarise_layers()`

R/guide-colorbar.r

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
#' @param barheight A numeric or a [grid::unit()] object specifying
1919
#' the height of the colorbar. Default value is `legend.key.height` or
2020
#' `legend.key.size` in [theme()] or theme.
21+
#' @param frame.colour A string specifying the colour of the frame
22+
#' drawn around the bar. If `NULL` (the default), no frame is drawn.
23+
#' @param frame.linewidth A numeric specifying the width of the frame
24+
#' drawn around the bar.
25+
#' @param frame.linetype A numeric specifying the linetype of the frame
26+
#' drawn around the bar.
2127
#' @param nbin A numeric specifying the number of bins for drawing colorbar. A
2228
#' smoother colorbar for a larger value.
2329
#' @param raster A logical. If `TRUE` then the colorbar is rendered as a
@@ -26,6 +32,8 @@
2632
#' raster image.
2733
#' @param ticks A logical specifying if tick marks on colorbar should be
2834
#' visible.
35+
#' @param ticks.colour A string specifying the color of the tick marks.
36+
#' @param ticks.linewidth A numeric specifying the width of the tick marks.
2937
#' @param draw.ulim A logical specifying if the upper limit tick marks should
3038
#' be visible.
3139
#' @param draw.llim A logical specifying if the lower limit tick marks should
@@ -109,8 +117,15 @@ guide_colourbar <- function(
109117
nbin = 20,
110118
raster = TRUE,
111119

120+
# frame
121+
frame.colour = NULL,
122+
frame.linewidth = 0.5,
123+
frame.linetype = 1,
124+
112125
# ticks
113126
ticks = TRUE,
127+
ticks.colour = "white",
128+
ticks.linewidth = 0.5,
114129
draw.ulim= TRUE,
115130
draw.llim = TRUE,
116131

@@ -146,8 +161,15 @@ guide_colourbar <- function(
146161
nbin = nbin,
147162
raster = raster,
148163

164+
# frame
165+
frame.colour = frame.colour,
166+
frame.linewidth = frame.linewidth,
167+
frame.linetype = frame.linetype,
168+
149169
# ticks
150170
ticks = ticks,
171+
ticks.colour = ticks.colour,
172+
ticks.linewidth = ticks.linewidth,
151173
draw.ulim = draw.ulim,
152174
draw.llim = draw.llim,
153175

@@ -271,6 +293,23 @@ guide_gengrob.colorbar <- function(guide, theme) {
271293
rectGrob(x = 0, y = by, vjust = 0, hjust = 0, width = barwidth, height = bh, default.units = "cm",
272294
gp = gpar(col = NA, fill = guide$bar$colour))
273295
})
296+
}
297+
298+
# make frame around color bar if requested (colour is not NULL)
299+
if (!is.null(guide$frame.colour)) {
300+
grob.bar <- grobTree(
301+
grob.bar,
302+
rectGrob(
303+
width = barwidth,
304+
height = barheight,
305+
default.units = "cm",
306+
gp = gpar(
307+
col = guide$frame.colour,
308+
lwd = guide$frame.linewidth,
309+
lty = guide$frame.linetype,
310+
fill = NA)
311+
)
312+
)
274313
}
275314

276315
# tick and label position
@@ -353,8 +392,14 @@ guide_gengrob.colorbar <- function(guide, theme) {
353392
x1 = c(rep(barwidth * (1/5), nbreak), rep(barwidth, nbreak))
354393
y1 = rep(tick_pos, 2)
355394
})
356-
segmentsGrob(x0 = x0, y0 = y0, x1 = x1, y1 = y1,
357-
default.units = "cm", gp = gpar(col = "white", lwd = 0.5, lineend = "butt"))
395+
segmentsGrob(
396+
x0 = x0, y0 = y0, x1 = x1, y1 = y1,
397+
default.units = "cm",
398+
gp = gpar(
399+
col = guide$ticks.colour,
400+
lwd = guide$ticks.linewidth,
401+
lineend = "butt")
402+
)
358403
}
359404

360405
# layout of bar and label

man/guide_colourbar.Rd

Lines changed: 23 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 73 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)