Skip to content

Instantly share code, notes, and snippets.

@jkaupp
Created February 26, 2020 01:32
Show Gist options
  • Save jkaupp/96d0655bf52447a92073b211318e1efd to your computer and use it in GitHub Desktop.
Save jkaupp/96d0655bf52447a92073b211318e1efd to your computer and use it in GitHub Desktop.

Revisions

  1. jkaupp created this gist Feb 26, 2020.
    25 changes: 25 additions & 0 deletions quasirandom.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    library(tidyverse)
    library(ggbeeswarm)

    data <- read_csv("https://gist.github.com/dgkeyes/a9b47cfa9a7bd8313e5603d18cf3d444/raw/3de0e4074db23636ebd420af78e90c4009aa10c7/oregon-economic-mobility-data.csv") %>%
    set_names(c("county", "percent")) %>%
    mutate(percent = parse_number(percent))

    mean <- mean(data$percent)

    ggplot(data, aes(x = percent, y = 0)) +
    geom_hline(yintercept = 0, color = 'grey80', size = 0.1) +
    geom_vline(xintercept = mean, color = "firebrick", size = 0.5, linetype = "dashed") +
    geom_quasirandom(groupOnX = FALSE, bandwidth = 0.1, width = 0.1, size = 3) +
    annotate("text", x = 5, y = 0, label = "5%", fontface = "bold", family = "Futura", size = 6) +
    annotate("text", x = 15, y = 0, label = "15%", fontface = "bold", family = "Futura", size = 6) +
    annotate("text", x = mean, y = 0.1, label = paste0(round(mean,2), "%"), color = "firebrick", fontface = "bold", family = "Futura", size = 5, hjust = -0.1) +
    scale_x_continuous(limits = c(5, 15), labels = NULL) +
    labs(x = NULL,
    y = NULL) +
    theme_minimal() +
    theme(panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.x = element_blank(),
    axis.text.y = element_blank())