Skip to content

Commit

Permalink
adjusting geom_* default colour based upon theme element
Browse files Browse the repository at this point in the history
a first hack: adding a new theme element `geom.colour` and passing theme to ggplot_build so that it can be called in `use_defaults` to override the `colour` default only when applicable and not set by `params` or mapped to data.
  • Loading branch information
dpseidel committed Jun 28, 2018
1 parent 1c09bae commit 42a3611
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
10 changes: 8 additions & 2 deletions R/geom-.r
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,27 @@ Geom <- ggproto("Geom",
setup_data = function(data, params) data,

# Combine data with defaults and set aesthetics from parameters
use_defaults = function(self, data, params = list()) {
use_defaults = function(self, data, params = list(), th) {

# Fill in missing aesthetics with their defaults
missing_aes <- setdiff(names(self$default_aes), names(data))

missing_eval <- lapply(self$default_aes[missing_aes], rlang::eval_tidy)
# Needed for geoms with defaults set to NULL (e.g. GeomSf)
missing_eval <- compact(missing_eval)

# transform defaults based on theme
if ("colour" %in% names(missing_eval) && !is.na(missing_eval$colour)) {
missing_eval$colour <- th$geom.colour
}

if (empty(data)) {
data <- as_gg_data_frame(missing_eval)
} else {
data[names(missing_eval)] <- missing_eval
}

# Override mappings with params
# Override mappings [and theme] with params
aes_params <- intersect(self$aesthetics(), names(params))
check_aesthetics(params[aes_params], nrow(data))
data[aes_params] <- params[aes_params]
Expand Down
4 changes: 2 additions & 2 deletions R/layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ Layer <- ggproto("Layer", NULL,
self$position$compute_layer(data, params, layout)
},

compute_geom_2 = function(self, data) {
compute_geom_2 = function(self, data, th) {
# Combine aesthetics, defaults, & params
if (empty(data)) return(data)

self$geom$use_defaults(data, self$aes_params)
self$geom$use_defaults(data, self$aes_params, th)
},

finish_statistics = function(self, data) {
Expand Down
5 changes: 3 additions & 2 deletions R/plot-build.r
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ ggplot_build.ggplot <- function(plot) {
}

# Fill in defaults etc.
data <- by_layer(function(l, d) l$compute_geom_2(d))
theme <- plot_theme(plot)
data <- by_layer(function(l, d) l$compute_geom_2(d, th = theme))

# Let layer stat have a final say before rendering
data <- by_layer(function(l, d) l$finish_statistics(d))
Expand Down Expand Up @@ -137,7 +138,7 @@ layer_grob <- function(plot, i = 1L) {
#' to (e.g.) make the legend box 2 cm wide, or combine multiple plots into
#' a single display, preserving aspect ratios across the plots.
#'
#' @seealso [print.ggplot()] and \code{link{benchplot}} for
#' @seealso [print.ggplot()] and \code{\link{benchplot}} for
#' for functions that contain the complete set of steps for generating
#' a ggplot2 plot.
#' @return a [gtable()] object
Expand Down
4 changes: 4 additions & 0 deletions R/theme-defaults.r
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ theme_grey <- function(base_size = 11, base_family = "",
margin = margin(), debug = FALSE
),

geom.colour = "black",
axis.line = element_blank(),
axis.line.x = NULL,
axis.line.y = NULL,
Expand Down Expand Up @@ -342,6 +343,9 @@ theme_dark <- function(base_size = 11, base_family = "",
# match legend key to panel.background
legend.key = element_rect(fill = "grey50", colour = NA),

# make geom stand out in white
geom.colour = "white",

# dark strips with light text (inverse contrast compared to theme_grey)
strip.background = element_rect(fill = "grey15", colour = NA),
strip.text = element_text(
Expand Down
1 change: 1 addition & 0 deletions R/theme-elements.r
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
rect = el_def("element_rect"),
text = el_def("element_text"),
title = el_def("element_text", "text"),
geom.colour = el_def("character"),
axis.line = el_def("element_line", "line"),
axis.text = el_def("element_text", "text"),
axis.title = el_def("element_text", "title"),
Expand Down
3 changes: 2 additions & 1 deletion R/theme.r
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#' @param title all title elements: plot, axes, legends (`element_text`;
#' inherits from `text`)
#' @param aspect.ratio aspect ratio of the panel
#'
#' @param geom.colour override default geom_* colour
#' @param axis.title label of axes (`element_text`; inherits from
#' `text`)
#' @param axis.title.x x axis label (`element_text`; inherits from
Expand Down Expand Up @@ -290,6 +290,7 @@ theme <- function(line,
text,
title,
aspect.ratio,
geom.colour,
axis.title,
axis.title.x,
axis.title.x.top,
Expand Down

0 comments on commit 42a3611

Please sign in to comment.