Skip to content
R for the Rest of Us Logo

R in 3 Months (Fall 2026)

Week 9 Live Session (Fall 2026)

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

We started with the week's advanced data viz topics (reordering, color, labels, annotations), then critiqued a default bar chart together. David live coded a polished bar chart from a client report, covering rounded bars, a custom bar order, direct labels and a reference line. He then turned the chart code into a reusable function.

You'll learn:

  • How to spot and fix problems in a default chart: legend, gray background, grid lines, borders, titles and axis labels
  • How to set a custom order for bars by turning a variable into a factor, and how to add a gap between bars with a manual x position
  • How to format values as percentages with the percent function from {scales} and add them as text labels with geom_text
  • How to add a dynamic reference line by pulling a value from the data with pull and using geom_hline, and how to label it with annotate and str_glue
  • How to turn working plot code into a function with arguments for location and age group so one chart can be remade for any state

Resources mentioned:

Key moments:

  • 10:45 Participants suggest fixes for a default bar chart
  • 21:03 Mapping data to aesthetics for the chart
  • 27:04 Why bars stack, and fixing it with facet_wrap
  • 30:37 Rounded bar corners with geom_chiclet
  • 33:16 Setting a custom bar order with a factor
  • 34:52 Adding a gap with a manual x position
  • 40:17 Formatting percent labels with {scales}
  • 42:07 Adding a dashed reference line with geom_hline
  • 45:08 Calculating the state rate with pull
  • 47:12 Turning the plot code into a function
  • 49:16 Labeling the reference line with annotate and str_glue
  • 53:29 Resource for choosing colors in data viz
View code shown in video
#  Recreating this: https://show.rfor.us/cditiy

# Load Packages -----------------------------------------------------------

library(tidyverse)
library(ggchicklet)
library(scales)

# Import Data ------------------------------------------------------------

cbem <- read_csv("data-raw/cbem.csv")

# Plot -------------------------------------------------------------------

cbem_plot <- function(location_to_filter, age_group_to_filter) {
  state_rate <-
    cbem |>
    filter(location == location_to_filter) |>
    filter(group == "All Persons") |>
    filter(age_group == age_group_to_filter) |>
    pull(percent)

  cbem |>
    filter(location == location_to_filter) |>
    filter(group != "All Persons") |>
    filter(age_group == age_group_to_filter) |>
    mutate(
      group = fct(
        group,
        levels = c(
          "American Indian or Alaska Native",
          "Asian or Pacific Islander",
          "Black or African American",
          "White",
          "Hispanic or Latino"
        )
      )
    ) |>
    mutate(
      x_position = case_when(
        group == "American Indian or Alaska Native" ~ 1,
        group == "Asian or Pacific Islander" ~ 2,
        group == "Black or African American" ~ 3,
        group == "White" ~ 4,
        group == "Hispanic or Latino" ~ 5.5
      )
    ) |>
    mutate(percent_formatted = percent(percent)) |>
    ggplot(
      aes(
        x = x_position,
        y = percent,
        fill = group,
        label = percent_formatted
      )
    ) +
    geom_hline(
      yintercept = state_rate,
      linetype = "dashed",
      color = "grey40"
    ) +
    geom_chicklet() +
    geom_text(
      color = "white",
      vjust = 1.5
    ) +
    annotate(
      geom = "text",
      x = 5.5,
      y = state_rate,
      vjust = -1,
      color = "grey40",
      label = str_glue("CBEM State Rate\n{percent(state_rate)}")
    ) +
    scale_fill_manual(
      values = c(
        "American Indian or Alaska Native" = "#9CC892",
        "Asian or Pacific Islander" = "#0066cc",
        "Black or African American" = "#477A3E",
        "White" = "#6CC5E9",
        "Hispanic or Latino" = "#ff7400"
      )
    ) +
    theme_void() +
    theme(legend.position = "none")
}

cbem_plot(
  location_to_filter = "Alaska",
  age_group_to_filter = "Under 25"
)

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Course Content

143 Lessons

New from Ally Debug R error messages