简体   繁体   中英

geom_jitter within facet_wrap in ggplot

I have an admittedly convoluted data frame and I am trying to display the averages for each of 7 dimensions (var_type2) by demographic groups (sex and race).

However, I also have these variables (v1) for multiple waves (either 2 or 3, depending on the variable).

I essentially got stuck where you see the code below, with the final step being to somehow get each of the 2 or 3 bars to extend horizontally beside each other (instead of being stacked like they are now). Sort of like manually doing a geom_jitter() by wave, within var_group?

(A more minor/secondary thing that I'm sure I could figure out but, bonus points to get the rows of demographic characteristics rearranged so that "all" is the top row, followed by White, Black, Hispanic, Male, Female. I can deal with coloring them in manually afterwards. EDIT: Figured this out, updated code below)

Code:

ggplot(d_test, aes(x = avg, y = var, fill = var_group)) +
  geom_col(orientation = "y",
           width = .8,
           alpha = .7,
           color = "black",
           size = 1) +
  facet_grid(var_group~var_type2,
             scales = "free",
             space = "free_y") +
  theme_minimal() +
  theme(legend.position = "none",
        strip.text.y = element_blank())

Data:

d_test <- structure(list(v1 = c("jjs_tot_w1", "jjs_tot_w2", "jjs_tot_w3", 
"jjs_wo_w1", "jjs_wo_w2", "jjs_wo_w3", "jjs_wa_w1", "jjs_wa_w2", 
"jjs_wa_w3", "poverty_w2", "poverty_w3", "welfare_w2", "welfare_w3", 
"ppp_score_w1", "ppp_score_w3", "pss_score_w1", "pss_score_w3", 
"jjs_tot_w1", "jjs_tot_w2", "jjs_tot_w3", "jjs_wo_w1", "jjs_wo_w2", 
"jjs_wo_w3", "jjs_wa_w1", "jjs_wa_w2", "jjs_wa_w3", "poverty_w2", 
"poverty_w3", "welfare_w2", "welfare_w3", "ppp_score_w1", "ppp_score_w3", 
"pss_score_w1", "pss_score_w3", "jjs_tot_w1", "jjs_tot_w2", "jjs_tot_w3", 
"jjs_wo_w1", "jjs_wo_w2", "jjs_wo_w3", "jjs_wa_w1", "jjs_wa_w2", 
"jjs_wa_w3", "poverty_w2", "poverty_w3", "welfare_w2", "welfare_w3", 
"ppp_score_w1", "ppp_score_w3", "pss_score_w1", "pss_score_w3", 
"jjs_tot_w1", "jjs_tot_w2", "jjs_tot_w3", "jjs_wo_w1", "jjs_wo_w2", 
"jjs_wo_w3", "jjs_wa_w1", "jjs_wa_w2", "jjs_wa_w3", "poverty_w2", 
"poverty_w3", "welfare_w2", "welfare_w3", "ppp_score_w1", "ppp_score_w3", 
"pss_score_w1", "pss_score_w3", "jjs_tot_w1", "jjs_tot_w2", "jjs_tot_w3", 
"jjs_wo_w1", "jjs_wo_w2", "jjs_wo_w3", "jjs_wa_w1", "jjs_wa_w2", 
"jjs_wa_w3", "poverty_w2", "poverty_w3", "welfare_w2", "welfare_w3", 
"ppp_score_w1", "ppp_score_w3", "pss_score_w1", "pss_score_w3", 
"jjs_tot_w1", "jjs_tot_w2", "jjs_tot_w3", "jjs_wo_w1", "jjs_wo_w2", 
"jjs_wo_w3", "jjs_wa_w1", "jjs_wa_w2", "jjs_wa_w3", "poverty_w2", 
"poverty_w3", "welfare_w2", "welfare_w3", "ppp_score_w1", "ppp_score_w3", 
"pss_score_w1", "pss_score_w3"), var = c("all", "all", "all", 
"all", "all", "all", "all", "all", "all", "all", "all", "all", 
"all", "all", "all", "all", "all", "White", "White", "White", 
"White", "White", "White", "White", "White", "White", "White", 
"White", "White", "White", "White", "White", "White", "White", 
"Black", "Black", "Black", "Black", "Black", "Black", "Black", 
"Black", "Black", "Black", "Black", "Black", "Black", "Black", 
"Black", "Black", "Black", "Hispanic", "Hispanic", "Hispanic", 
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic", 
"Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic", 
"Hispanic", "Hispanic", "Female", "Female", "Female", "Female", 
"Female", "Female", "Female", "Female", "Female", "Female", "Female", 
"Female", "Female", "Female", "Female", "Female", "Female", "Male", 
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male", 
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male"
), avg = c(17.2, 18.6, 21.11, 6.27, 7.46, 7.98, 10.23, 6.76, 
6.61, 0.64, 0.55, -0.28, 0.01, -1.04, -1.11, -1.06, -1.01, 23.96, 
18.22, 17.45, 5.05, 6.27, 6.41, 8.46, 6.18, 5.61, -0.28, -0.43, 
-0.79, -0.91, -0.65, -0.64, -0.87, -0.76, 31.28, 22.55, 23.51, 
5.88, 7.17, 8.15, 11.72, 8.17, 8.23, 1.38, 1.23, -0.16, -0.55, 
-1.03, -1.07, -1.12, -1.12, 22.3, 20.41, 20.22, 7, 8.01, 8.38, 
9.64, 5.85, 5.5, 0.38, 0.35, -0.22, -0.62, -1.17, -1.29, -1.06, 
0.3, 28.13, 19.68, 20.9, 6.27, 7.46, 8.33, 9.55, 6.14, 6.37, 
0.7, 0.62, -0.25, 0.82, 0.37, 0.32, 0.46, 0.5, 31.95, 23.47, 
22.75, 7.71, 8.89, 9.07, 12.33, 8.81, 8.28, 1.1, 1.9, 1.13, 0.79, 
0.42, 0.31, 0.28, 0.34), var_type = c("JJS", "JJS", "JJS", "JJS", 
"JJS", "JJS", "JJS", "JJS", "JJS", "Hardship", "Hardship", "Hardship", 
"Hardship", "Support", "Support", "Support", "Support", "JJS", 
"JJS", "JJS", "JJS", "JJS", "JJS", "JJS", "JJS", "JJS", "Hardship", 
"Hardship", "Hardship", "Hardship", "Support", "Support", "Support", 
"Support", "JJS", "JJS", "JJS", "JJS", "JJS", "JJS", "JJS", "JJS", 
"JJS", "Hardship", "Hardship", "Hardship", "Hardship", "Support", 
"Support", "Support", "Support", "JJS", "JJS", "JJS", "JJS", 
"JJS", "JJS", "JJS", "JJS", "JJS", "Hardship", "Hardship", "Hardship", 
"Hardship", "Support", "Support", "Support", "Support", "JJS", 
"JJS", "JJS", "JJS", "JJS", "JJS", "JJS", "JJS", "JJS", "Hardship", 
"Hardship", "Hardship", "Hardship", "Support", "Support", "Support", 
"Support", "JJS", "JJS", "JJS", "JJS", "JJS", "JJS", "JJS", "JJS", 
"JJS", "Hardship", "Hardship", "Hardship", "Hardship", "Support", 
"Support", "Support", "Support"), var_type2 = c("jjs Tot", "jjs Tot", 
"jjs Tot", "jjs wo", "jjs wo", "jjs wo", "jjs wa", "jjs wa", 
"jjs wa", "Poverty", "Poverty", "Welfare", "Welfare", "P Support", 
"P Support", "S Support", "S Support", "jjs Tot", "jjs Tot", 
"jjs Tot", "jjs wo", "jjs wo", "jjs wo", "jjs wa", "jjs wa", 
"jjs wa", "Poverty", "Poverty", "Welfare", "Welfare", "P Support", 
"P Support", "S Support", "S Support", "jjs Tot", "jjs Tot", 
"jjs Tot", "jjs wo", "jjs wo", "jjs wo", "jjs wa", "jjs wa", 
"jjs wa", "Poverty", "Poverty", "Welfare", "Welfare", "P Support", 
"P Support", "S Support", "S Support", "jjs Tot", "jjs Tot", 
"jjs Tot", "jjs wo", "jjs wo", "jjs wo", "jjs wa", "jjs wa", 
"jjs wa", "Poverty", "Poverty", "Welfare", "Welfare", "P Support", 
"P Support", "S Support", "S Support", "jjs Tot", "jjs Tot", 
"jjs Tot", "jjs wo", "jjs wo", "jjs wo", "jjs wa", "jjs wa", 
"jjs wa", "Poverty", "Poverty", "Welfare", "Welfare", "P Support", 
"P Support", "S Support", "S Support", "jjs Tot", "jjs Tot", 
"jjs Tot", "jjs wo", "jjs wo", "jjs wo", "jjs wa", "jjs wa", 
"jjs wa", "Poverty", "Poverty", "Welfare", "Welfare", "P Support", 
"P Support", "S Support", "S Support"), var_group = c("All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "Race", "Race", 
"Race", "Race", "Race", "Race", "Race", "Race", "Race", "Race", 
"Race", "Race", "Race", "Race", "Race", "Race", "Race", "Race", 
"Race", "Race", "Race", "Race", "Race", "Race", "Race", "Race", 
"Race", "Race", "Race", "Race", "Race", "Race", "Race", "Race", 
"Race", "Race", "Race", "Race", "Race", "Race", "Race", "Race", 
"Race", "Race", "Race", "Race", "Race", "Race", "Race", "Race", 
"Race", "Sex", "Sex", "Sex", "Sex", "Sex", "Sex", "Sex", "Sex", 
"Sex", "Sex", "Sex", "Sex", "Sex", "Sex", "Sex", "Sex", "Sex", 
"Sex", "Sex", "Sex", "Sex", "Sex", "Sex", "Sex", "Sex", "Sex", 
"Sex", "Sex", "Sex", "Sex", "Sex", "Sex", "Sex", "Sex"), wave = c(1, 
2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 
2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 
2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 
2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 
2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -102L), spec = structure(list(
    cols = list(v1 = structure(list(), class = c("collector_character", 
    "collector")), var = structure(list(), class = c("collector_character", 
    "collector")), avg = structure(list(), class = c("collector_double", 
    "collector")), var_type = structure(list(), class = c("collector_character", 
    "collector")), var_type2 = structure(list(), class = c("collector_character", 
    "collector")), var_group = structure(list(), class = c("collector_character", 
    "collector")), wave = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

Several options are available. I think you are looking for the effect of position_dodge2() https://ggplot2.tidyverse.org/reference/position_dodge.html

Other position adjustments: position_identity(), position_jitterdodge(), position_jitter(), position_nudge(), position_stack()

library(tidyverse)
ggplot(d_test, aes(x = avg, y = var, fill = var_group)) +
  geom_col(orientation = "y",
           width = .8,
           alpha = .7,
           color = "black",
           size = 1,
           position = position_dodge2()) + # <----------
  facet_grid(var_group~var_type2,
             scales = "free",
             space = "free_y") +
  theme_minimal() +
  theme(legend.position = "none",
        strip.text.y = element_blank())

带位置闪避的条形图 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM