As of now, I’ve created a barplot that shows the percent of each race category that contributes to various childcare activities, with the figure being faceted by childcare activity.
How would I go about adding additional variables to the y axis?
For example, if I wanted to also calculate the percent of males and females and add them to the y axis, below the race categories (via the ‘sex’ variable in the data). My initial attempts only managed to create a new singular ‘race by sex’ variable, which is not what I want.
dtest %>%
filter(race2 != "Other NH") %>%
pivot_longer(cols = starts_with("ccare_"),
names_to = "ccare_variable",
values_to = "response") %>%
group_by(ccare_variable, race2) %>%
summarise(percent_yes = weighted.mean(response == 1, useweight, na.rm = TRUE) * 100, .groups = "drop") %>%
ggplot(aes(x = reorder(race2, percent_yes), y = percent_yes, fill = race2)) +
geom_bar(stat = "identity",
color = "black",
alpha = 0.7) +
coord_flip() +
labs(x = NULL,
y = "Percent") +
scale_x_discrete(labels = levels(dtest$race2)) +
scale_fill_brewer(palette = "Set1") +
facet_wrap(~ ccare_variable, scales = "free_y") +
theme_minimal(base_size = 15) +
theme(panel.grid = element_blank(),
legend.position = "bottom")
dtest <- structure(list(ccare_livewlong_person = structure(c(NA, NA, NA,
0, NA, NA, NA, 0, NA, NA, NA, 1, NA, NA, 0, 0, 1, 0, NA, NA,
0, NA, NA, NA, NA, NA, NA, NA, NA, NA), format.stata = "%12.0g", labels = c(No = 0,
Yes = 1), class = c("haven_labelled", "vctrs_vctr", "double")),
ccare_staywith_person = structure(c(NA, NA, NA, 0, NA, NA,
NA, 1, NA, NA, NA, 0, NA, NA, 1, 1, 0, 0, NA, NA, 0, NA,
NA, NA, NA, NA, NA, NA, NA, NA), format.stata = "%12.0g", labels = c(No = 0,
Yes = 1), class = c("haven_labelled", "vctrs_vctr", "double"
)), ccare_buyclothes_person = structure(c(NA, NA, NA, 0,
NA, NA, NA, 1, 1, 1, NA, 1, NA, 0, 0, 0, 0, 0, NA, NA,
0, NA, NA, NA, NA, 1, 1, 1, 1, NA), format.stata = "%12.0g", labels = c(No = 0,
Yes = 1), class = c("haven_labelled", "vctrs_vctr", "double"
)), ccare_buyfood_person = structure(c(NA, NA, NA, 0, NA,
NA, NA, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, NA, NA, 0,
NA, NA, NA, NA, 1, 0, NA, NA, NA), format.stata = "%12.0g", labels = c(No = 0,
Yes = 1), class = c("haven_labelled", "vctrs_vctr", "double"
)), ccare_buytoys_person = structure(c(NA, NA, NA, 0, NA,
NA, NA, 0, 1, 1, NA, 0, NA, NA, 0, 0, 1, 0, NA, NA, 0,
NA, NA, NA, NA, 1, NA, NA, NA, NA), format.stata = "%12.0g", labels = c(No = 0,
Yes = 1), class = c("haven_labelled", "vctrs_vctr", "double"
)), ccare_payeduc_person = structure(c(NA, NA, NA, 0, NA,
NA, NA, 0, 1, NA, 0, 1, 0, 0, 0, 0, 1, 0, NA, NA, 1,
NA, NA, NA, NA, NA, NA, NA, 0, NA), format.stata = "%12.0g", labels = c(No = 0,
Yes = 1), class = c("haven_labelled", "vctrs_vctr", "double"
)), race2 = structure(c(1L, 1L, 2L, 1L, 1L, 1L, 1L, 4L, 4L,
1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 5L, 1L, 1L, 5L, 1L,
1L, 1L, 5L, 5L, 1L, 1L), levels = c("White NH", "Black NH",
"Hispanic", "Nat Am NH", "Other NH"), class = "factor"),
sex = structure(c(2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 2L, 2L), levels = c("M", "F"), class = "factor"),
useweight = structure(c(0.12316239125, 1.10825384945, 0.56176074875,
0.55257337975, 0.4109339601, 0.64619988635, 0.31264390035,
0.26356887575, 0.26356887575, 0.18757221085, 0.3727184159,
0.36235486655, 0.23228683015, 0.43385255745, 0.8295457014,
0.8295457014, 0.3762255673, 0.2436195251, 0.5951818102, 0.116057867,
0.24554129075, 0.38657110555, 0.0957898253, 0.97274405485,
0.97274405485, 0.1407062719, 0.2918493845, 0.2918493845,
0.21775501005, 0.2139179732), label = "Weights", format.stata = "%10.0g")), row.names = c(NA,
-30L), class = c("tbl_df", "tbl", "data.frame"))