after_stat()
after_scale()
after_stat()
before & afterafter_scale()
before & afterself
You can install {ggtrace} from Github. More on the package website.
# remotes::install_github("yjunechoe/ggtrace")
library(ggtrace) # v0.3.7
library(ggplot2)
library(rlang)
library(dplyr)
# Bar plot using computed/"mapped" aesthetics with `after_stat()` and `after_scale()`
barplot_plot <- ggplot(data = palmerpenguins::penguins) +
geom_bar(
mapping = aes(
x = species, # Discrete x-axis representing species
y = after_stat(count / sum(count)), # Bars represent count of species as proportions
color = species, # The outline of the bars are colored by species
fill = after_scale(alpha(color, 0.5)) # The fill of the bars are lighter than the outline color
),
size = 3
)
barplot_plot
after_stat()
ggtrace(
method = ggplot2:::Layer$map_statistic, # Layer is not exported so need the `:::`
trace_steps = c(1, 1, -1),
trace_exprs = exprs(
traceback = trace_back(), # Log the traceback when the method is called
before = data, # What does the data look like BEFORE resolving `after_stat()`?
after = ~step # What does the data look like AFTER resolving `after_stat()`?
# - The `~step` keyword runs the step and returns its output
),
verbose = FALSE
)
after_scale()
ggtrace(
method = Geom$use_defaults,
trace_steps = c(1, 6, 7),
trace_exprs = exprs(
traceback = trace_back(), # Log the traceback when the method is called
before = data, # What does the data look like BEFORE resolving `after_scale()`
after = data # What does the data look like AFTER resolving `after_scale()`
),
verbose = FALSE
)
# Print the plot and trigger the traces
# Note that with the default `once = TRUE`, the traces are removed on exit
barplot_plot
tracedump <- global_ggtrace()
names(tracedump)
## [1] "ggplot2:::Layer$map_statistic-000000001CE607B8"
## [2] "Geom$use_defaults-000000001FB67E60"
names(tracedump) <- c("after_stat", "after_scale")
lapply(tracedump, names)
## $after_stat
## [1] "traceback" "before" "after"
##
## $after_scale
## [1] "traceback" "before" "after"
after_stat()
before & aftertracedump$after_stat$before
count <dbl> | prop <dbl> | x <int> | width <dbl> | flipped_aes <lgl> | colour <fct> | PANEL <fct> | group <int> |
---|---|---|---|---|---|---|---|
152 | 1 | 1 | 0.9 | FALSE | Adelie | 1 | 1 |
68 | 1 | 2 | 0.9 | FALSE | Chinstrap | 1 | 2 |
124 | 1 | 3 | 0.9 | FALSE | Gentoo | 1 | 3 |
tracedump$after_stat$after
y <dbl> | count <dbl> | prop <dbl> | x <int> | width <dbl> | flipped_aes <lgl> | colour <fct> | PANEL <fct> | group <int> |
---|---|---|---|---|---|---|---|---|
0.4418605 | 152 | 1 | 1 | 0.9 | FALSE | Adelie | 1 | 1 |
0.1976744 | 68 | 1 | 2 | 0.9 | FALSE | Chinstrap | 1 | 2 |
0.3604651 | 124 | 1 | 3 | 0.9 | FALSE | Gentoo | 1 | 3 |
identical(
tracedump$after_stat$before %>%
mutate(y = count / sum(count)) %>%
relocate(y, .before = 1),
tracedump$after_stat$after
)
## [1] TRUE
after_scale()
before & aftertracedump$after_scale$before
colour <chr> | y <dbl> | count <dbl> | prop <dbl> | x <mppd_dsc> | flipped_aes <lgl> | PANEL <fct> | group <int> | ymin <dbl> | ymax <dbl> | |
---|---|---|---|---|---|---|---|---|---|---|
#F8766D | 0.4418605 | 152 | 1 | 1 | FALSE | 1 | 1 | 0 | 0.4418605 | |
#00BA38 | 0.1976744 | 68 | 1 | 2 | FALSE | 1 | 2 | 0 | 0.1976744 | |
#619CFF | 0.3604651 | 124 | 1 | 3 | FALSE | 1 | 3 | 0 | 0.3604651 |
tracedump$after_scale$after
fill <chr> | colour <chr> | y <dbl> | count <dbl> | prop <dbl> | x <mppd_dsc> | flipped_aes <lgl> | PANEL <fct> | group <int> | ymin <dbl> | |
---|---|---|---|---|---|---|---|---|---|---|
#F8766D80 | #F8766D | 0.4418605 | 152 | 1 | 1 | FALSE | 1 | 1 | 0 | |
#00BA3880 | #00BA38 | 0.1976744 | 68 | 1 | 2 | FALSE | 1 | 2 | 0 | |
#619CFF80 | #619CFF | 0.3604651 | 124 | 1 | 3 | FALSE | 1 | 3 | 0 |
identical(
tracedump$after_scale$before %>%
mutate(fill = alpha(colour, 0.5)) %>%
relocate(fill, .before = 1),
tracedump$after_scale$after
)
## [1] TRUE
layer_data(barplot_plot, 1)
fill <chr> | colour <chr> | y <dbl> | count <dbl> | prop <dbl> | x <mppd_dsc> | flipped_aes <lgl> | PANEL <fct> | group <int> | ymin <dbl> | |
---|---|---|---|---|---|---|---|---|---|---|
#F8766D80 | #F8766D | 0.4418605 | 152 | 1 | 1 | FALSE | 1 | 1 | 0 | |
#00BA3880 | #00BA38 | 0.1976744 | 68 | 1 | 2 | FALSE | 1 | 2 | 0 | |
#619CFF80 | #619CFF | 0.3604651 | 124 | 1 | 3 | FALSE | 1 | 3 | 0 |
# What about the tracebacks?
# - traceback contains information about the "call stack" (what called the method?)
# We see that they're called from different places in `ggplot_build.ggplot(x)`
# - Layer$map_statistic in Line 73, Layer$compute_geom_2 (which calls Geom$use_defaults) in Line 100
# - https://github.com/tidyverse/ggplot2/blob/de5d63f7eba06e507187f34780bea071cb31fee4/R/plot-build.r
lapply(tracedump, `[[`, "traceback")
## $after_stat
## x
## 1. \-ggplot2:::print.ggplot(x)
## 2. +-ggplot2::ggplot_build(x)
## 3. \-ggplot2:::ggplot_build.ggplot(x)
## 4. \-ggplot2:::by_layer(function(l, d) l$map_statistic(d, plot))
## 5. \-ggplot2:::f(l = layers[[i]], d = data[[i]])
## 6. \-l$map_statistic(d, plot)
## 7. \-ggplot2:::f(..., self = self)
##
## $after_scale
## x
## 1. \-ggplot2:::print.ggplot(x)
## 2. +-ggplot2::ggplot_build(x)
## 3. \-ggplot2:::ggplot_build.ggplot(x)
## 4. \-ggplot2:::by_layer(function(l, d) l$compute_geom_2(d))
## 5. \-ggplot2:::f(l = layers[[i]], d = data[[i]])
## 6. \-l$compute_geom_2(d)
## 7. \-ggplot2:::f(..., self = self)
## 8. \-self$geom$use_defaults(data, self$aes_params, modifiers)
## 9. \-ggplot2:::f(..., self = self)
self
# We can also get the context for self
# First, clear the global tracedump
clear_global_ggtrace()
## NULL
# Return `self` inside the method
# `self` should be contextualized to the Layer and the Geom
ggtrace(ggplot2:::Layer$compute_geom_2, 1, quote(self), verbose = FALSE)
ggtrace(Geom$use_defaults, 1, quote(self), verbose = FALSE)
# Force evaluation of plot code without printing it
invisible(ggplot_build(barplot_plot))
traced_self <- unlist(global_ggtrace(), recursive = FALSE)
names(traced_self) <- c("layer", "geom")
traced_self
## $layer
## mapping: x = ~species, y = ~after_stat(count/sum(count)), colour = ~species, fill = ~after_scale(alpha(color, 0.5))
## geom_bar: width = NULL, na.rm = FALSE, orientation = NA
## stat_count: width = NULL, na.rm = FALSE, orientation = NA
## position_stack
##
## $geom
## <ggproto object: Class GeomBar, GeomRect, Geom, gg>
## aesthetics: function
## default_aes: uneval
## draw_group: function
## draw_key: function
## draw_layer: function
## draw_panel: function
## extra_params: na.rm orientation
## handle_na: function
## non_missing_aes: xmin xmax ymin ymax
## optional_aes:
## parameters: function
## required_aes: x y
## setup_data: function
## setup_params: function
## use_defaults: function
## super: <ggproto object: Class GeomRect, Geom, gg>
# The geom is GeomBar
identical(traced_self$geom, GeomBar)
## [1] TRUE
# This is also the default geom for the layer returned by `geom_bar()`
identical(traced_self$geom, geom_bar()$geom)
## [1] TRUE
# Which is also the geom of the actual Layer that we traced
identical(traced_self$geom, traced_self$layer$geom)
## [1] TRUE
# The stat of the traced layer is StatCount
identical(traced_self$layer$stat, StatCount)
## [1] TRUE
# This is the default for `geom_bar()` (default is `geom_bar(stat = "count")`)
identical(traced_self$layer$stat, geom_bar()$stat)
## [1] TRUE
sessioninfo::session_info()
## - Session info ---------------------------------------------------------------
## setting value
## version R version 4.1.1 (2021-08-10)
## os Windows 10 x64
## system x86_64, mingw32
## ui RTerm
## language (EN)
## collate English_United States.1252
## ctype English_United States.1252
## tz America/New_York
## date 2021-10-25
##
## - Packages -------------------------------------------------------------------
## package * version date lib source
## assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0)
## bslib 0.3.1 2021-10-06 [1] CRAN (R 4.1.1)
## cli 3.0.1 2021-07-17 [1] CRAN (R 4.1.0)
## colorspace 2.0-2 2021-06-24 [1] CRAN (R 4.1.0)
## crayon 1.4.1 2021-02-08 [1] CRAN (R 4.0.3)
## DBI 1.1.1 2021-01-15 [1] CRAN (R 4.0.3)
## digest 0.6.28 2021-09-23 [1] CRAN (R 4.1.1)
## dplyr * 1.0.7 2021-06-18 [1] CRAN (R 4.1.0)
## ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.0)
## evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.0)
## fansi 0.5.0 2021-05-25 [1] CRAN (R 4.1.1)
## farver 2.1.0 2021-02-28 [1] CRAN (R 4.1.0)
## fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.1)
## generics 0.1.0 2020-10-31 [1] CRAN (R 4.0.3)
## ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.1.0)
## ggtrace * 0.3.7 2021-10-23 [1] local
## glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.3)
## gtable 0.3.0 2019-03-25 [1] CRAN (R 4.0.0)
## highr 0.8 2019-03-20 [1] CRAN (R 4.0.0)
## htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.1)
## jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.1.1)
## jsonlite 1.7.2 2020-12-09 [1] CRAN (R 4.0.3)
## knitr 1.36 2021-09-29 [1] CRAN (R 4.1.1)
## labeling 0.4.2 2020-10-20 [1] CRAN (R 4.0.3)
## lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.1)
## magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.3)
## munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.0)
## palmerpenguins 0.1.0 2020-07-23 [1] CRAN (R 4.0.2)
## pillar 1.6.4 2021-10-18 [1] CRAN (R 4.1.1)
## pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.0)
## purrr 0.3.4 2020-04-17 [1] CRAN (R 4.0.2)
## R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.1)
## rlang * 0.4.11 2021-04-30 [1] CRAN (R 4.1.1)
## rmarkdown 2.11 2021-09-14 [1] CRAN (R 4.1.1)
## rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.0.3)
## sass 0.4.0 2021-05-12 [1] CRAN (R 4.1.0)
## scales 1.1.1 2020-05-11 [1] CRAN (R 4.0.2)
## sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.4)
## stringi 1.7.5 2021-10-04 [1] CRAN (R 4.1.0)
## stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.0)
## tibble 3.1.5 2021-09-30 [1] CRAN (R 4.1.0)
## tidyselect 1.1.1 2021-04-30 [1] CRAN (R 4.1.0)
## utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.1)
## vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.1.0)
## withr 2.4.2 2021-04-18 [1] CRAN (R 4.1.0)
## xfun 0.27 2021-10-18 [1] CRAN (R 4.1.1)
## yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.0)
##
## [1] C:/Users/jchoe/R/win-library/packages
## [2] C:/Program Files/R/R-4.1.1/library