Everyday ggplot2 closereads 🫖
  • First closeread
  • sublayer modularity
  • Extension Gallery Functions
  • Posit Conf
  • ggalluvial

Who are the ggplot2 extenders?

conf2023 <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-01-14/conf2023.csv') |> mutate(year = 2023)
conf2024 <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-01-14/conf2024.csv') |> mutate(year = 2024)

names(conf2023) 
 [1] "speaker_name"        "speaker_affiliation" "session_type"       
 [4] "session_title"       "block_track_title"   "session_date"       
 [7] "session_start"       "session_length"      "session_abstract"   
[10] "year"               
names(conf2024)
[1] "talk_title"   "speaker_name" "track"        "description"  "yt_url"      
[6] "year"        
sample_n(conf2023, 20)
# A tibble: 20 × 10
   speaker_name speaker_affiliation session_type session_title block_track_title
   <chr>        <chr>               <chr>        <chr>         <chr>            
 1 Tiger Tang   CARFAX              regular      From Concept… Bridging the gap…
 2 Patrick Ten… Meadows Mental Hea… regular      The Gonzalez… Building effecti…
 3 Camila Saez… Dow, Inc.           lightning    Coding tools… Lightning talks  
 4 JP Flores    University of Nort… lightning    the people o… Lightning talks  
 5 Maria Grycuk Appsilon            regular      The Power of… Shiny user inter…
 6 Joe Roberts  Posit               regular      How I Learne… Managing packages
 7 Natalia And… Pfizer              regular      Building a C… Pharma           
 8 Mine Cetink… Posit               regular      Reproducible… Quarto (1)       
 9 Vedha Viyash Appsilon            lightning    Shiny Develo… Lightning talks  
10 Jadey Ryan   Washington State D… regular      Custom Quart… Elevating your r…
11 Erika Tyagi  Urban Institute     regular      Democratizin… End-to-end data …
12 Gordon Shot… Posit               regular      Diversify yo… Data science wit…
13 Andrew Patt… Infrastructure Lea… regular      10 solutions… The future is Sh…
14 Lydia Gibson California State U… regular      How the R fo… Developing your …
15 Thomas Mich… AXI                 regular      Combining R … R or Python? Why…
16 Michael Gar… Medable             regular      Integrating … Leave it to the …
17 Tesla DuBois Fox Chase Cancer C… regular      Solving a Se… Developing your …
18 James Balam… University of Illi… regular      Dynamic Inte… Quarto (1)       
19 Elaine McVey Chief               keynote      From Data Co… From Data Confus…
20 Davis Vaugh… Posit               lightning    dplyr 1.1.0 … Lightning talks  
# ℹ 5 more variables: session_date <date>, session_start <dttm>,
#   session_length <dbl>, session_abstract <chr>, year <dbl>
Tip
data_filter <- function(.keep, .by) {
  structure(list(keep_specification = rlang::enquo(.keep), 
                 by_specification = rlang::enquo(.by)), 
            class = "filterobs")
}

ggplot_add.filterobs <- function(object, plot, object_name) {
  
  new_data <- dplyr::filter(plot$data, 
                            !!object$keep_specification, 
                            .by = !!object$by_specification)
  plot$data <- new_data
  plot

}

data_nest <- function(.by) {
  structure(list(by_specification = rlang::enquo(.by)),
            class = "data_nestvar")
  
}

ggplot_add.data_nestvar <- function(object, plot, object_name) {

  
  new_data <- tidyr::nest(plot$data, 
                          .by = !! object$by_specification)
    
  plot$data <- new_data
  plot

}


data_unnest <- function(cols) {
  structure(list(),
            class = "data_unnestvar")
  
}

ggplot_add.data_unnestvar <- function(object, plot, object_name) {

  
  new_data <- tidyr::unnest(plot$data, cols = "data")
    
  plot$data <- new_data
  
  plot

}


#' @export
data_mutate <- function(.value, .by, var_name) {
  structure(list(value_specification = rlang::enquo(.value),
                 by_specification = rlang::enquo(.by),
                 var_name_specification = var_name),
            class = "data_mutate")
  
}

ggplot_add.data_mutate <- function(object, plot, object_name) {

  
  new_data <- dplyr::mutate(plot$data, 
                            .value = !! object$value_specification, 
                            .by = !! object$by_specification)
  
    message("New variable named '.value' created")
    

    if(object$var_name %in% names(new_data)){
      
      new_data[,object$var_name] <- new_data$.value
      
      new_data <- new_data %>% select(-.value)
    }else{
    names(new_data)[names(new_data) == ".value"] <- object$var_name
    }
    
    
  plot$data <- new_data
  plot

}

r paste(knitr::knit(text = to_closeread, quiet = F), collapse = “”)`

last_plot()$plot_data
NULL

Here is the complete ‘conversation’ with the dataset!

ggplot(data = conf2023) + # the data frame to be plotted is all the exported functions from the 
  aes(id = "All Speakers") + # let's look at a count of all the exported functions first
  ggcirclepack::geom_circlepack() + # Using circlepacking we automatically have circles size representing the number of observation, i.e. exported functions
  ggcirclepack::geom_circlepack_text() + # We need to add a label or things are hard to interpret
  coord_equal() + # and lets square up the circles
  ggchalkboard:::theme_glassboard() + # we'll add a theme
  theme(axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank()) + # And remove axes 
  aes(id = session_type) + # First we ask what packages - github repository names - are present
  scale_size(range = c(3.5, 8)) + # size range from defaults 1 to 6 and then we can see last category is keynote
  aes(id = session_date) + scale_size(range = 8) + # Then let's look at who is writing these exported functions
  aes(id = speaker_affiliation) + scale_size() + # Then let's look at who is writing these exported functions
  aes(fill = str_detect(speaker_affiliation, "Posit")) + guides(fill = "none") + # Let's just highlight some of the diversity in names
 # and just delete the fill guide - it's pretty obvious what's being highlighted
  scale_fill_manual(values = c("lightgrey", alpha("midnightblue", .2))) + # switch out fill
  data_mutate(var_name = "speaker_affiliation", ifelse(str_detect(speaker_affiliation, "Posit"), "Posit, PBC", speaker_affiliation)) + # consolidating
  aes(id = speaker_name) + # Let's look at repeat speakers just Tom Mock duplicate
  data_nest(.by = c(speaker_name, speaker_affiliation)) + scale_size(range = 1) + # let's nest the data to one speaker-affiliation
  aes(id = fct_lump_min(speaker_affiliation, min = 2, other_level = "Other Affiliation")) + scale_size(range = c(1.25, 6)) + # lump affiliations
  data_unnest() + # Include Toms two talks
  aes(label = str_wrap(after_stat(id), 12)) + # Wrapping text
  guides(size = "none")

© Copyright 2024, Gina Reynolds

 
  • Edit this page
  • Report an issue

Built with Quarto