How to make a custom function of update_geom_defaults work?

Hi ggplot2 experts,

I want to create a function that can update a series of geom defaults as to how I want them with ability to modify the inputs.

However, the function results in the value of the input taking on the argument name rather than the value of the argument.

Any ideas how to make it take on the value of the colour i.e. "red" in the example below?

library(tidyverse)

test <- function(colour = "red") {
  update_geom_defaults("point", ggplot2::aes(colour = colour))
}

test()

GeomPoint$default_aes
#> Aesthetic mapping: 
#> * `shape`  -> 19
#> * `colour` -> `colour`
#> * `size`   -> 1.5
#> * `fill`   -> NA
#> * `alpha`  -> NA
#> * `stroke` -> 0.5

Created on 2024-04-05 with reprex v2.1.0

Does this work for you?

library(tidyverse)

test <- function(colour = "red") {
  update_geom_defaults("point", ggplot2::aes(colour = !!colour))
}

test()

GeomPoint$default_aes
#> Aesthetic mapping: 
#> * `shape`  -> 19
#> * `colour` -> "red"
#> * `size`   -> 1.5
#> * `fill`   -> NA
#> * `alpha`  -> NA
#> * `stroke` -> 0.5

Created on 2024-04-04 with reprex v2.0.2

1 Like

or you could use the curly-curly syntax:

test <- function(colour = "red") {
  update_geom_defaults("point", ggplot2::aes(colour = {{colour}}))
}
 

See
rlang 0.4.0 for an explanation.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.