Skip to content

Enable more styling options for guide_colorbar(). #2541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,19 @@ up correct aspect ratio, and draws a graticule.
widths that are proportional to the base font size. One relative font size
was eliminated. (@clauswilke)

### Guides

* Make `guide_colorbar()` more configurable: enable styling of tick marks and
drawing of frame around the color bar via new arguments `ticks.colour`,
`ticks.linewidth`, `frame.colour`, `frame.linewidth`, and `frame.linetype`.
(@clauswilke)

* `guide_colorbar()` now uses `legend.spacing.x` and `legend.spacing.y` correctly,
and it can handle multi-line titles. Minor tweaks were made to `guide_legend()`
to make sure the two legend functions behave as similarly as possible.
(@clauswilke, #2397 and #2398)


### Other

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

* `guide_colorbar()` now correctly uses `legend.spacing.x` and `legend.spacing.y`,
and it can handle multi-line titles. Minor tweaks were made to `guide_legend()`
to make sure the two legend functions behave as similarly as possible.
(@clauswilke, #2397 and #2398)

* `map_data()` now works when purrr is loaded (tidyverse#66)

* New functions `summarise_layout()`, `summarise_coord()`, and `summarise_layers()`
Expand Down
49 changes: 47 additions & 2 deletions R/guide-colorbar.r
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
#' @param barheight A numeric or a [grid::unit()] object specifying
#' the height of the colorbar. Default value is `legend.key.height` or
#' `legend.key.size` in [theme()] or theme.
#' @param frame.colour A string specifying the colour of the frame
#' drawn around the bar. If `NULL` (the default), no frame is drawn.
#' @param frame.linewidth A numeric specifying the width of the frame
#' drawn around the bar.
#' @param frame.linetype A numeric specifying the linetype of the frame
#' drawn around the bar.
#' @param nbin A numeric specifying the number of bins for drawing colorbar. A
#' smoother colorbar for a larger value.
#' @param raster A logical. If `TRUE` then the colorbar is rendered as a
Expand All @@ -26,6 +32,8 @@
#' raster image.
#' @param ticks A logical specifying if tick marks on colorbar should be
#' visible.
#' @param ticks.colour A string specifying the color of the tick marks.
#' @param ticks.linewidth A numeric specifying the width of the tick marks.
#' @param draw.ulim A logical specifying if the upper limit tick marks should
#' be visible.
#' @param draw.llim A logical specifying if the lower limit tick marks should
Expand Down Expand Up @@ -109,8 +117,15 @@ guide_colourbar <- function(
nbin = 20,
raster = TRUE,

# frame
frame.colour = NULL,
frame.linewidth = 0.5,
frame.linetype = 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we used linewidth and linetype in other functions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linetype is a standard aesthetic, so I think that's fine. linewidth is usually called size throughout the rest of ggplot2. If you prefer size I'm happy to make the change, it's just that frame.size doesn't sound like it controls the thickness of the frame.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any particularly strong feelings here; I don't really understand how these options are supposed to fit in with the theme system.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. The question is whether every possible option to a guide function needs a theme equivalent. My personal opinion is no. I think there is a lot of room for improvement for making guide functions more flexible, and possibly providing different ones or frameworks for extending them, and it's impossible to add new theme components every time somebody makes some minor tweak to some guide function. Already, there are things the guide functions can do that the theme can't control, such as the draw.ulim option for guide_colorbar().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'm happy to merge this PR; I just wanted to make sure we at least talked over the issues.


# ticks
ticks = TRUE,
ticks.colour = "white",
ticks.linewidth = 0.5,
draw.ulim= TRUE,
draw.llim = TRUE,

Expand Down Expand Up @@ -146,8 +161,15 @@ guide_colourbar <- function(
nbin = nbin,
raster = raster,

# frame
frame.colour = frame.colour,
frame.linewidth = frame.linewidth,
frame.linetype = frame.linetype,

# ticks
ticks = ticks,
ticks.colour = ticks.colour,
ticks.linewidth = ticks.linewidth,
draw.ulim = draw.ulim,
draw.llim = draw.llim,

Expand Down Expand Up @@ -271,6 +293,23 @@ guide_gengrob.colorbar <- function(guide, theme) {
rectGrob(x = 0, y = by, vjust = 0, hjust = 0, width = barwidth, height = bh, default.units = "cm",
gp = gpar(col = NA, fill = guide$bar$colour))
})
}

# make frame around color bar if requested (colour is not NULL)
if (!is.null(guide$frame.colour)) {
grob.bar <- grobTree(
grob.bar,
rectGrob(
width = barwidth,
height = barheight,
default.units = "cm",
gp = gpar(
col = guide$frame.colour,
lwd = guide$frame.linewidth,
lty = guide$frame.linetype,
fill = NA)
)
)
}

# tick and label position
Expand Down Expand Up @@ -353,8 +392,14 @@ guide_gengrob.colorbar <- function(guide, theme) {
x1 = c(rep(barwidth * (1/5), nbreak), rep(barwidth, nbreak))
y1 = rep(tick_pos, 2)
})
segmentsGrob(x0 = x0, y0 = y0, x1 = x1, y1 = y1,
default.units = "cm", gp = gpar(col = "white", lwd = 0.5, lineend = "butt"))
segmentsGrob(
x0 = x0, y0 = y0, x1 = x1, y1 = y1,
default.units = "cm",
gp = gpar(
col = guide$ticks.colour,
lwd = guide$ticks.linewidth,
lineend = "butt")
)
}

# layout of bar and label
Expand Down
29 changes: 23 additions & 6 deletions man/guide_colourbar.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading