Skip to content

Instantly share code, notes, and snippets.

@szimmer
Last active July 23, 2020 12:58
Show Gist options
  • Save szimmer/ed83da13b2dd41a249852e3e7f315986 to your computer and use it in GitHub Desktop.
Save szimmer/ed83da13b2dd41a249852e3e7f315986 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(survey)
library(srvyr)
data(api)
apisrs_mut <- apisrs %>%
as_tibble() %>%
mutate(stype_factor=parse_factor(as.character(stype), levels=c("E", "M", "H")),
stype_ordered_factor=parse_factor(as.character(stype), levels=c("E", "M", "H"), ordered = TRUE))
apisrs_mut %>% select(starts_with("stype")) %>% summary()
srs_design_srvyr <- apisrs_mut %>%
as_survey_design(ids = 1, fpc = fpc)
srs_design_srvyr %>%
survey_count(stype)
srs_design_srvyr %>%
survey_count(stype_factor)
srs_design_srvyr %>%
survey_count(stype_ordered_factor)
# Demonstrate order in plotting
out <- apisrs_mut %>%
mutate(hs_grad_pct = cut(hsg, c(0, 20, 100), include.lowest = TRUE,
labels = c("<20%", "20+%"))) %>%
group_by(stype, stype_factor, stype_ordered_factor, hs_grad_pct) %>%
summarize(api_diff = weighted.mean(api00 - api99, pw),
n = n())
ggplot(data = out, aes(x = stype, y = api_diff, group = hs_grad_pct, fill = hs_grad_pct)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(y = 0, label = n), position = position_dodge(width = 0.9), vjust = -1)
ggplot(data = out, aes(x = stype_factor, y = api_diff, group = hs_grad_pct, fill = hs_grad_pct)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(y = 0, label = n), position = position_dodge(width = 0.9), vjust = -1)
ggplot(data = out, aes(x = stype_ordered_factor, y = api_diff, group = hs_grad_pct, fill = hs_grad_pct)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(y = 0, label = n), position = position_dodge(width = 0.9), vjust = -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment