Skip to content

Instantly share code, notes, and snippets.

@paleolimbot
Created July 16, 2019 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paleolimbot/52a0f530b7d73f89b5bd258689408d21 to your computer and use it in GitHub Desktop.
Save paleolimbot/52a0f530b7d73f89b5bd258689408d21 to your computer and use it in GitHub Desktop.
library(ggplot2)
library(ggdebug) # remotes::install_github("paleolimbot/ggdebug")
# the classic ggplot!
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, colour = class))
# ...is really this:
p <- ggplot(mpg) +
geom_point(
aes(x = displ, y = hwy, colour = class),
stat = StatIdentity,
position = PositionIdentity
) +
facet_null() +
coord_cartesian() +
scale_x_continuous() +
scale_y_continuous() +
scale_colour_hue()
# and is displayed in three steps
built <- ggplot_build(p)
drawn <- ggplot_gtable(built)
grid::grid.draw(drawn) # render
# Let's look under the hood!
ggdebug(
# build
FacetNull$compute_layout, FacetNull$map_data,
Facet$init_scales, Facet$train_scales,
StatIdentity$compute_layer,
PositionIdentity$compute_layer,
# Facet$init_scales, Facet$train_scales,
CoordCartesian$setup_panel_params,
# draw
# Facet and Layout assemble all the parts
CoordCartesian$render_bg,
CoordCartesian$render_axis_h, CoordCartesian$render_axis_v,
GeomPoint$draw_panel
)
built <- ggplot_build(p)
drawn <- ggplot_gtable(built)
grid::grid.draw(drawn) # render
ggundebug()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment