Skip to content

Instantly share code, notes, and snippets.

@chrishaid
Last active July 14, 2017 15:50
Show Gist options
  • Save chrishaid/0ec8805aa9330568121e082cc8688b64 to your computer and use it in GitHub Desktop.
Save chrishaid/0ec8805aa9330568121e082cc8688b64 to your computer and use it in GitHub Desktop.
Color background for reference in ggplot2 (check out the ggplot2 call itself)
# This might be a bit much and requried a coord switch
cutpoints <- tribble(
~Level, ~low, ~high, ~x,
"3", 1, 2, 0,
"2", 2, 3, 0,
"2+", 3, 3.5, 0,
"1", 3.5, 4, 0,
"1+", 4, 5, 0,
"3", 1, 2, 1,
"2", 2, 3, 1,
"2+", 3, 3.5, 1,
"1", 3.5, 4, 1,
"1+", 4, 5, 1,
"3", 1, 2, 2,
"2", 2, 3, 2,
"2+", 3, 3.5, 2,
"1", 3.5, 4, 2,
"1+", 4, 5, 2,
"3", 1, 2, 3,
"2", 2, 3, 3,
"2+", 3, 3.5, 3,
"1", 3.5, 4, 3,
"1+", 4, 5, 3,
"3", 1, 2, 4,
"2", 2, 3, 4,
"2+", 3, 3.5, 4,
"1", 3.5, 4, 4,
"1+", 4, 5, 4
) %>%
mutate(Level = forcats::fct_inorder(Level, ordered=TRUE))
# Pay attention to all the tricks here with integer vs factor vs character representation of
# school names (basically order them as need be ==> grab the factor() %>% as.character()
sqrp_estimates_plot <-
sqrp_levels_all %>%
select(school, ada, points) %>%
tidyr::spread(ada, points) %>%
janitor::clean_names() %>%
mutate(school = forcats::as_factor(school),
school_int = as.integer(school),
ymin = ifelse(school == "B", x0_94 - .1, x0_95 -.1),
ymax = x0_95+.05)
sqrp_estimates_plot %>%
ggplot(aes(x = school_int, ymin = ymin, ymax = ymax)) +
geom_ribbon(data = cutpoints, aes(x = x, ymin = low, ymax = high, fill = Level), alpha = .3) +
geom_linerange(size = 2.5) +
geom_text(aes(y = ymin-.01, label = round(ymin,1)), hjust = 1) +
geom_text(aes(y = ymax+.01, label = round(ymax,1)), hjust = 0) +
scale_x_continuous(breaks = 1:3, labels = c("A", "B", "C")) +
coord_flip() +
labs(y = "Points",
x = "",
fill = "Level") +
theme_linedraw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment