Skip to content

Allow default geom aesthetics to be set from theme #2749

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 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
19b0cc7
Add new theme element "geom"
dpseidel Jul 11, 2018
7dc76d5
Set default_aes from theme
dpseidel Jul 11, 2018
21acff9
Expand functionality to guides, update_geom_defaults(), and tests
dpseidel Jul 11, 2018
0bc4041
Add theme geom elements colour.accent1, colour.accent2, fill.accent, …
dpseidel Jul 17, 2018
ecf1356
Implement new theme$geom elements for all geoms
dpseidel Jul 17, 2018
42acbd3
Adjust geom_density, geom_quantile, and geom_sf
dpseidel Aug 1, 2018
9e1305f
merge master into theme_geom
dpseidel Aug 8, 2018
58a677d
Remove expr() and update aes names
dpseidel Aug 9, 2018
23fdf39
Update implementation given feedback
dpseidel Aug 9, 2018
4bc838c
Merge remote-tracking branch 'upstream/master' into theme_geom
dpseidel Sep 2, 2018
af8c462
Implement element_geom() as suggested by Claus
dpseidel Sep 6, 2018
a4f5c2f
Embed and implement from_theme() to evaluate aes
dpseidel Sep 6, 2018
bf34c2b
Minor edits to element_geom and documentation rebuild
dpseidel Sep 6, 2018
ff4c7ec
merge master to theme_geom branch
dpseidel Jan 30, 2020
1445665
pull new `defaults` argument through use_defaults function.
dpseidel Jan 31, 2020
0630120
eval_defaults retrieves the currently set default theme
dpseidel Jan 31, 2020
dc2daa0
evaluate from defaults using completed theme object
dpseidel Jan 31, 2020
ce57ba9
pull evaluated defaults through guides
dpseidel Jan 31, 2020
844df2a
evaluate defaults outside the condistion guide_geom.legend
dpseidel Feb 24, 2020
517f056
Functional aesthetic theming in geom_sf!
dpseidel Feb 24, 2020
ddad370
remove self referencing in theme element, dummy document `from_theme`…
dpseidel Feb 24, 2020
5f312e8
a few intial tests
dpseidel Feb 25, 2020
08feee1
minor fixes to preserve S3 method consistency for guide_geom
dpseidel Feb 26, 2020
c3f8387
Merge branch 'master' into theme_geom
dpseidel Mar 5, 2020
33e7dc6
silence rlang data mask warning
dpseidel Mar 5, 2020
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export(draw_key_vline)
export(draw_key_vpath)
export(dup_axis)
export(element_blank)
export(element_geom)
export(element_grob)
export(element_line)
export(element_rect)
Expand Down
5 changes: 4 additions & 1 deletion R/annotation-logticks.r
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ GeomLogticks <- ggproto("GeomLogticks", Geom,
gTree(children = do.call("gList", ticks))
},

default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = 1)
default_aes = expr(aes(
colour = theme$geom$colour, size = 0.5, linetype = 1,
alpha = theme$geom$alpha
))
)


Expand Down
13 changes: 10 additions & 3 deletions R/geom-.r
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,18 @@ 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(), theme) {
Copy link
Member

Choose a reason for hiding this comment

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

I think the code related to evaluating aesthetics in the special theme environment should be pulled out into its own function so that we can think about it in isolation, and then test it.


# evaluates defaults given plot theme
if (length(theme) == 0) theme <- theme_grey()
env <- new.env()
env$theme <- theme
defaults <- rlang::eval_tidy(self$default_aes, env)

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

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

Expand Down
7 changes: 6 additions & 1 deletion R/geom-abline.r
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ GeomAbline <- ggproto("GeomAbline", Geom,
GeomSegment$draw_panel(unique(data), panel_params, coord)
},

default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
default_aes = expr(aes(
colour = theme$geom$colour,
size = 0.5, linetype = 1,
alpha = theme$geom$alpha
)),

required_aes = c("slope", "intercept"),

draw_key = draw_key_abline
Expand Down
7 changes: 5 additions & 2 deletions R/geom-boxplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,11 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,

draw_key = draw_key_boxplot,

default_aes = aes(weight = 1, colour = "grey20", fill = "white", size = 0.5,
alpha = NA, shape = 19, linetype = "solid"),
default_aes = expr(aes(
weight = 1, colour = theme$geom$colour.accent1 ,
fill = theme$geom$fill.accent, size = 0.5,
alpha = theme$geom$alpha, shape = 19, linetype = "solid"
)),

required_aes = c("x", "lower", "upper", "middle", "ymin", "ymax")
)
6 changes: 4 additions & 2 deletions R/geom-contour.r
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ geom_contour <- function(mapping = NULL, data = NULL,
#' @export
#' @include geom-path.r
GeomContour <- ggproto("GeomContour", GeomPath,
default_aes = aes(weight = 1, colour = "#3366FF", size = 0.5, linetype = 1,
alpha = NA)
default_aes = expr(aes(
weight = 1, colour = theme$geom$colour.accent2,
size = 0.5, linetype = 1, alpha = theme$geom$alpha
))
)
6 changes: 4 additions & 2 deletions R/geom-crossbar.r
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
GeomErrorbar$setup_data(data, params)
},

default_aes = aes(colour = "black", fill = NA, size = 0.5, linetype = 1,
alpha = NA),
default_aes = expr(aes(
colour = theme$geom$colour, fill = NA, size = 0.5,
linetype = 1, alpha = theme$geom$alpha
)),

required_aes = c("x", "y", "ymin", "ymax"),

Expand Down
5 changes: 4 additions & 1 deletion R/geom-curve.r
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ geom_curve <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomCurve <- ggproto("GeomCurve", GeomSegment,
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
default_aes = expr(aes(
colour = theme$geom$colour, size = 0.5, linetype = 1,
alpha = theme$geom$alpha
)),
draw_panel = function(data, panel_params, coord, curvature = 0.5, angle = 90,
ncp = 5, arrow = NULL, arrow.fill = NULL, lineend = "butt", na.rm = FALSE) {

Expand Down
6 changes: 5 additions & 1 deletion R/geom-defaults.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
#' @rdname update_defaults
update_geom_defaults <- function(geom, new) {
g <- check_subclass(geom, "Geom", env = parent.frame())
old <- g$default_aes

env <- new.env()
Copy link
Member

Choose a reason for hiding this comment

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

If geoms are themeable, I think we can deprecate this function, so we should just leave as is and in the documentation mark it as internal, and point people towards the new theme system.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we deprecate this, we will presumably need to make all aesthetics themeable so as to not lose the functionality?

Copy link
Member

Choose a reason for hiding this comment

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

Oh yeah. In that case we should pull this out into a function that can be shared with eval_defaults()

env$theme <- theme_get()
old <- rlang::eval_tidy(g$default_aes, env)

g$default_aes <- defaults(rename_aes(new), old)
invisible()
}
Expand Down
8 changes: 4 additions & 4 deletions R/geom-density.r
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ geom_density <- function(mapping = NULL, data = NULL,
#' @export
#' @include geom-ribbon.r
GeomDensity <- ggproto("GeomDensity", GeomArea,
default_aes = defaults(
aes(fill = NA, weight = 1, colour = "black", alpha = NA),
GeomArea$default_aes
)
default_aes = expr(aes(
fill = NA, weight = 1, colour = theme$geom$colour,
alpha = theme$geom$alpha, size = 0.5, linetype = 1
))
)
5 changes: 4 additions & 1 deletion R/geom-density2d.r
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@ geom_density2d <- geom_density_2d
#' @usage NULL
#' @export
GeomDensity2d <- ggproto("GeomDensity2d", GeomPath,
default_aes = aes(colour = "#3366FF", size = 0.5, linetype = 1, alpha = NA)
default_aes = expr(aes(
colour = theme$geom$colour.accent2,
size = 0.5, linetype = 1, alpha = theme$geom$alpha
))
)
6 changes: 5 additions & 1 deletion R/geom-dotplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,
required_aes = c("x", "y"),
non_missing_aes = c("size", "shape"),

default_aes = aes(colour = "black", fill = "black", alpha = NA),
default_aes = expr(aes(
colour = theme$geom$colour,
fill = theme$geom$colour,
alpha = theme$geom$alpha
)),

setup_data = function(data, params) {
data$width <- data$width %||%
Expand Down
7 changes: 5 additions & 2 deletions R/geom-errorbar.r
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ geom_errorbar <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomErrorbar <- ggproto("GeomErrorbar", Geom,
default_aes = aes(colour = "black", size = 0.5, linetype = 1, width = 0.5,
alpha = NA),
default_aes = expr(aes(
colour = theme$geom$colour,
size = 0.5, linetype = 1, width = 0.5,
alpha = theme$geom$alpha
)),

draw_key = draw_key_path,

Expand Down
9 changes: 6 additions & 3 deletions R/geom-errorbarh.r
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ geom_errorbarh <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomErrorbarh <- ggproto("GeomErrorbarh", Geom,
default_aes = aes(colour = "black", size = 0.5, linetype = 1, height = 0.5,
alpha = NA),
default_aes = expr(aes(
colour = theme$geom$colour,
size = 0.5, linetype = 1, height = 0.5,
alpha = theme$geom$alpha
)),

draw_key = draw_key_path,

Expand All @@ -67,7 +70,7 @@ GeomErrorbarh <- ggproto("GeomErrorbarh", Geom,
draw_panel = function(data, panel_params, coord, height = NULL) {
GeomPath$draw_panel(data.frame(
x = as.vector(rbind(data$xmax, data$xmax, NA, data$xmax, data$xmin, NA, data$xmin, data$xmin)),
y = as.vector(rbind(data$ymin, data$ymax, NA, data$y, data$y, NA, data$ymin, data$ymax)),
y = as.vector(rbind(data$ymin, data$ymax, NA, data$y, data$y, NA, data$ymin, data$ymax)),
colour = rep(data$colour, each = 8),
alpha = rep(data$alpha, each = 8),
size = rep(data$size, each = 8),
Expand Down
8 changes: 4 additions & 4 deletions R/geom-hex.r
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ GeomHex <- ggproto("GeomHex", Geom,

required_aes = c("x", "y"),

default_aes = aes(
default_aes = expr(aes(
colour = NA,
fill = "grey50",
fill = theme$geom$fill,
size = 0.5,
linetype = 1,
alpha = NA
),
alpha = theme$geom$alpha
)),

draw_key = draw_key_polygon
)
Expand Down
6 changes: 5 additions & 1 deletion R/geom-hline.r
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ GeomHline <- ggproto("GeomHline", Geom,
GeomSegment$draw_panel(unique(data), panel_params, coord)
},

default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
default_aes = expr(aes(
colour = theme$geom$colour,
size = 0.5, linetype = 1,
alpha = theme$geom$alpha
)),
required_aes = "yintercept",

draw_key = draw_key_path
Expand Down
11 changes: 6 additions & 5 deletions R/geom-label.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ geom_label <- function(mapping = NULL, data = NULL,
GeomLabel <- ggproto("GeomLabel", Geom,
required_aes = c("x", "y", "label"),

default_aes = aes(
colour = "black", fill = "white", size = 3.88, angle = 0,
hjust = 0.5, vjust = 0.5, alpha = NA, family = "", fontface = 1,
lineheight = 1.2
),
default_aes = expr(aes(
colour = theme$text$colour, fill = theme$geom$fill.accent,
size = 3.88, angle = 0, hjust = 0.5, vjust = 0.5,
alpha = theme$geom$alpha, family = theme$text$family,
fontface = theme$text$face, lineheight = theme$text$lineheight
)),

draw_panel = function(self, data, panel_params, coord, parse = FALSE,
na.rm = FALSE,
Expand Down
6 changes: 5 additions & 1 deletion R/geom-linerange.r
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ geom_linerange <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomLinerange <- ggproto("GeomLinerange", Geom,
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
default_aes = expr(aes(
colour = theme$geom$colour,
size = 0.5, linetype = 1,
alpha = theme$geom$alpha
)),

draw_key = draw_key_vpath,

Expand Down
5 changes: 4 additions & 1 deletion R/geom-path.r
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ geom_path <- function(mapping = NULL, data = NULL,
GeomPath <- ggproto("GeomPath", Geom,
required_aes = c("x", "y"),

default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
default_aes = expr(aes(
colour = theme$geom$colour, size = 0.5, linetype = 1,
alpha = theme$geom$alpha
)),

handle_na = function(data, params) {
# Drop missing values at the start or end of a line - can't drop in the
Expand Down
8 changes: 4 additions & 4 deletions R/geom-point.r
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ geom_point <- function(mapping = NULL, data = NULL,
GeomPoint <- ggproto("GeomPoint", Geom,
required_aes = c("x", "y"),
non_missing_aes = c("size", "shape", "colour"),
default_aes = aes(
shape = 19, colour = "black", size = 1.5, fill = NA,
alpha = NA, stroke = 0.5
),
default_aes = expr(aes(
shape = 19, colour = theme$geom$colour, size = 1.5,
fill = NA, alpha = theme$geom$alpha, stroke = 0.5
)),

draw_panel = function(data, panel_params, coord, na.rm = FALSE) {
if (is.character(data$shape)) {
Expand Down
9 changes: 7 additions & 2 deletions R/geom-pointrange.r
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ geom_pointrange <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomPointrange <- ggproto("GeomPointrange", Geom,
default_aes = aes(colour = "black", size = 0.5, linetype = 1, shape = 19,
fill = NA, alpha = NA, stroke = 1),
default_aes = expr(aes(
colour = theme$geom$colour,
size = 0.5, linetype = 1,
shape = 19, fill = NA,
alpha = theme$geom$alpha,
stroke = 1
)),

draw_key = draw_key_pointrange,

Expand Down
8 changes: 6 additions & 2 deletions R/geom-polygon.r
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ GeomPolygon <- ggproto("GeomPolygon", Geom,
)
},

default_aes = aes(colour = "NA", fill = "grey20", size = 0.5, linetype = 1,
alpha = NA),
default_aes = expr(aes(
colour = NA,
fill = theme$geom$fill,
size = 0.5, linetype = 1,
alpha = theme$geom$alpha
)),

handle_na = function(data, params) {
data
Expand Down
8 changes: 4 additions & 4 deletions R/geom-quantile.r
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ geom_quantile <- function(mapping = NULL, data = NULL,
#' @export
#' @include geom-path.r
GeomQuantile <- ggproto("GeomQuantile", GeomPath,
default_aes = defaults(
aes(weight = 1, colour = "#3366FF", size = 0.5),
GeomPath$default_aes
)
default_aes = expr(aes(
weight = 1, colour = theme$geom$colour.accent2, size = 0.5, linetype = 1,
alpha = theme$geom$alpha
))
)
2 changes: 1 addition & 1 deletion R/geom-raster.r
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ geom_raster <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomRaster <- ggproto("GeomRaster", Geom,
default_aes = aes(fill = "grey20", alpha = NA),
default_aes = expr(aes(fill = theme$geom$fill, alpha = theme$geom$alpha)),
non_missing_aes = "fill",
required_aes = c("x", "y"),

Expand Down
9 changes: 7 additions & 2 deletions R/geom-rect.r
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ geom_rect <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomRect <- ggproto("GeomRect", Geom,
default_aes = aes(colour = NA, fill = "grey35", size = 0.5, linetype = 1,
alpha = NA),
default_aes = expr(aes(
colour = NA,
fill = theme$geom$fill,
size = 0.5,
linetype = 1,
alpha = theme$geom$alpha
)),

required_aes = c("xmin", "xmax", "ymin", "ymax"),

Expand Down
12 changes: 8 additions & 4 deletions R/geom-ribbon.r
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ geom_ribbon <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
GeomRibbon <- ggproto("GeomRibbon", Geom,
default_aes = aes(colour = NA, fill = "grey20", size = 0.5, linetype = 1,
alpha = NA),
default_aes = expr(aes(
colour = NA, fill = theme$geom$fill,
size = 0.5, linetype = 1, alpha = theme$geom$alpha
)),

required_aes = c("x", "ymin", "ymax"),

Expand Down Expand Up @@ -131,8 +133,10 @@ geom_area <- function(mapping = NULL, data = NULL, stat = "identity",
#' @usage NULL
#' @export
GeomArea <- ggproto("GeomArea", GeomRibbon,
default_aes = aes(colour = NA, fill = "grey20", size = 0.5, linetype = 1,
alpha = NA),
default_aes = expr(aes(
colour = NA, fill = theme$geom$fill,
size = 0.5, linetype = 1, alpha = theme$geom$alpha
)),

required_aes = c("x", "y"),

Expand Down
6 changes: 5 additions & 1 deletion R/geom-rug.r
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ GeomRug <- ggproto("GeomRug", Geom,
gTree(children = do.call("gList", rugs))
},

default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
default_aes = expr(aes(
colour = theme$geom$colour,
size = 0.5, linetype = 1,
alpha = theme$geom$alpha
)),

draw_key = draw_key_path
)
Loading