Skip to content

WIP: Register default scales via themes #3973

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

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,6 @@ Collate:
'zxx.r'
'zzz.r'
VignetteBuilder: knitr
RoxygenNote: 7.0.2
RoxygenNote: 7.1.0
Roxygen: list(markdown = TRUE)
Encoding: UTF-8
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ S3method(makeContext,dotstackGrob)
S3method(merge_element,default)
S3method(merge_element,element)
S3method(merge_element,element_blank)
S3method(merge_element,list)
S3method(plot,ggplot)
S3method(predictdf,default)
S3method(predictdf,glm)
Expand Down
5 changes: 2 additions & 3 deletions R/layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ Layer <- ggproto("Layer", NULL,
if (!is.null(self$geom_params$group)) {
aesthetics[["group"]] <- self$aes_params$group
}

scales_add_defaults(plot$scales, data, aesthetics, plot$plot_env)
scales_add_defaults(plot$scales, data, aesthetics, plot$plot_env, plot$theme)

# Evaluate aesthetics
env <- child_env(baseenv(), stage = stage)
Expand Down Expand Up @@ -320,7 +319,7 @@ Layer <- ggproto("Layer", NULL,
stat_data <- new_data_frame(compact(stat_data))

# Add any new scales, if needed
scales_add_defaults(plot$scales, data, new, plot$plot_env)
scales_add_defaults(plot$scales, data, new, plot$plot_env, plot$theme)
# Transform the values, if the scale say it's ok
# (see stat_spoke for one exception)
if (self$stat$retransform) {
Expand Down
4 changes: 3 additions & 1 deletion R/plot-build.r
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ ggplot_build.ggplot <- function(plot) {
}
out
}
## Build full theme combining supplied elements and current defaults
plot$theme <- plot_theme(plot)
Copy link
Member

Choose a reason for hiding this comment

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

This should be commented, just like the other parts of the code in this function.


# Allow all layers to make any final adjustments based
# on raw input data and plot info
Expand Down Expand Up @@ -163,7 +165,7 @@ ggplot_gtable.ggplot_built <- function(data) {
plot <- data$plot
layout <- data$layout
data <- data$data
theme <- plot_theme(plot)
theme <- plot$theme

geom_grobs <- Map(function(l, d) l$draw_geom(d, layout), plot$layers, data)
layout$setup_panel_guides(plot$guides, plot$layers, plot$mapping)
Expand Down
12 changes: 9 additions & 3 deletions R/scale-type.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
find_scale <- function(aes, x, env = parent.frame()) {
find_scale <- function(aes, x, env = parent.frame(), theme = NULL) {
# Inf is ambiguous; it can be used either with continuous scales or with
# discrete scales, so just skip in the hope that we will have a better guess
# with the other layers
Expand All @@ -8,11 +8,17 @@ find_scale <- function(aes, x, env = parent.frame()) {

type <- scale_type(x)
candidates <- paste("scale", aes, type, sep = "_")

for (scale in candidates) {
## First try lookup in theme (if supplied)
default_scales <- calc_element("default.scales", theme)
scale <- default_scales[[name]]
if (!is.null(scale) && isTRUE(mode(scale) == mode)) {
return(scale)
}
scale_f <- find_global(scale, env, mode = "function")
if (!is.null(scale_f))
if (!is.null(scale_f)) {
return(scale_f())
}
}

# Failure to find a scale is not an error because some "aesthetics" don't
Expand Down
4 changes: 2 additions & 2 deletions R/scales-.r
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ scales_transform_df <- function(scales, df) {

# @param aesthetics A list of aesthetic-variable mappings. The name of each
# item is the aesthetic, and the value of each item is the variable in data.
scales_add_defaults <- function(scales, data, aesthetics, env) {
scales_add_defaults <- function(scales, data, aesthetics, env, theme) {
if (is.null(aesthetics)) return()
names(aesthetics) <- unlist(lapply(names(aesthetics), aes_to_scale))

Expand All @@ -100,7 +100,7 @@ scales_add_defaults <- function(scales, data, aesthetics, env) {
datacols <- compact(datacols)

for (aes in names(datacols)) {
scales$add(find_scale(aes, datacols[[aes]], env))
scales$add(find_scale(aes, datacols[[aes]], env, theme))
}

}
Expand Down
1 change: 1 addition & 0 deletions R/theme-elements.r
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
strip.placement.y = el_def("character", "strip.placement"),
strip.switch.pad.grid = el_def("unit"),
strip.switch.pad.wrap = el_def("unit"),
default.scales = el_def("list"),
Copy link
Member

Choose a reason for hiding this comment

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

It's probably best to add a specific element class here. PR #2749 adds element_geom(), so maybe this one here could be called element_scales(). But I'd like to have input from @yutannihilation or @thomasp85 here.

Copy link
Member

Choose a reason for hiding this comment

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

Also, if we add a specific element class then we check then and there that the element values are functions and then we can avoid the mode() check in find_global() (to be moved into find_scale()).


plot.background = el_def("element_rect", "rect"),
plot.title = el_def("element_text", "title"),
Expand Down
9 changes: 9 additions & 0 deletions R/theme.r
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ theme <- function(line,
strip.text.y,
strip.switch.pad.grid,
strip.switch.pad.wrap,
default.scales,
...,
complete = FALSE,
validate = TRUE
Expand Down Expand Up @@ -643,6 +644,14 @@ merge_element.element <- function(new, old) {
new
}

#' @rdname merge_element
#' @export
merge_element.list <- function(new, old) {
overlap <- intersect(names(new), names(old))
old <- old[setdiff(names(old), overlap)]
append(old, new)
}

#' Combine the properties of two elements
#'
#' @param e1 An element object
Expand Down
6 changes: 4 additions & 2 deletions man/diamonds.Rd

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

8 changes: 6 additions & 2 deletions man/economics.Rd

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

6 changes: 4 additions & 2 deletions man/faithfuld.Rd

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

2 changes: 1 addition & 1 deletion man/ggplot2-package.Rd

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

6 changes: 5 additions & 1 deletion man/graphical-units.Rd

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

6 changes: 4 additions & 2 deletions man/luv_colours.Rd

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

3 changes: 3 additions & 0 deletions man/merge_element.Rd

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

6 changes: 4 additions & 2 deletions man/midwest.Rd

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

6 changes: 4 additions & 2 deletions man/mpg.Rd

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

6 changes: 4 additions & 2 deletions man/msleep.Rd

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

6 changes: 4 additions & 2 deletions man/presidential.Rd

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

4 changes: 2 additions & 2 deletions man/scale_discrete.Rd

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

4 changes: 2 additions & 2 deletions man/scale_manual.Rd

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

4 changes: 2 additions & 2 deletions man/scale_shape.Rd

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

4 changes: 3 additions & 1 deletion man/seals.Rd

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

1 change: 1 addition & 0 deletions man/theme.Rd

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

6 changes: 4 additions & 2 deletions man/txhousing.Rd

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