## ----setup, include = FALSE--------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ## ----------------------------------------------------------------------------- library(ggspec) library(ggplot2) ## ----------------------------------------------------------------------------- p <- ggplot(mpg, aes(displ, hwy)) + geom_point(aes(colour = class)) + geom_smooth(method = "lm", se = FALSE) + facet_wrap(~drv) + scale_colour_brewer(palette = "Set1") + labs(title = "Engine displacement vs highway MPG", x = "Displacement (L)", y = "Highway MPG (hwy)") p ## ----------------------------------------------------------------------------- spec_layers(p) ## ----------------------------------------------------------------------------- # Inspect the mapping for layer 1 (geom_point) spec_layers(p)$mapping[[1]] ## ----------------------------------------------------------------------------- # inherit = FALSE: only mappings explicitly set on the layer spec_layers(p, inherit = FALSE)$mapping ## ----------------------------------------------------------------------------- spec_aes(p) ## ----------------------------------------------------------------------------- library(dplyr) spec_aes(p) |> filter(aesthetic == "colour") ## ----------------------------------------------------------------------------- spec_scales(p) ## ----------------------------------------------------------------------------- spec_facets(p) ## ----------------------------------------------------------------------------- spec_labels(p) ## ----------------------------------------------------------------------------- spec_coord(p) # Flipped coordinates spec_coord(p + coord_flip()) ## ----------------------------------------------------------------------------- sp <- spec_plot(p) sp # Access per-layer aesthetics sp$aes_long[[1]] # Access the facet spec (same in every row) sp$facets[[1]]