Intro Thoughts

Status Quo

library(tidyverse)





# use ggplot_add
# don't make assumption about name of var
facet_wrap_color <- function() {

  structure(
    list(), 
    class = "facet_wrap_color"
    )

}

#' @import ggplot2
#' @importFrom ggplot2 ggplot_add
#' @export
ggplot_add.facet_wrap_color <- function(object, plot, object_name) {
  
  color_mapping <- plot$mapping$colour |> _[[2]] |> as.expression() |> as.character()
  

  plot$data <- plot$data |> mutate(.color_temp = !!rlang::parse_expr(color_mapping))

  plot +
    ggplot2::facet_wrap(~.color_temp)

}



penguins |> 
  ggplot() + 
  aes(bill_len, 
      bill_dep, 
      color = species) + 
  geom_point(color = "grey", 
             layout = "fixed") +
  geom_point() + 
  facet_wrap_color() 

penguins |> 
  ggplot() + 
  aes(bill_len, 
      bill_dep, 
      color = species == "Adelie") + 
  geom_point(color = "grey", 
             layout = "fixed") +
  geom_point() + 
  facet_wrap_color()   

Caution about ggplot add and update… peaking a plot build isn’t wholistic

penguins |> 
  ggplot() + 
  aes(bill_len, 
      bill_dep, 
      color = species == "Adelie") + #<< two color specifications in play
  geom_point(color = "grey", 
             layout = "fixed") +
  geom_point() + 
  facet_wrap_color() + 
  aes(color = species) #<< two color specifications in play

Closing remarks, Other Relevant Work, Caveats