Closed nsbatra closed 3 weeks ago
a <- tout %>% filter(! indicateur %in% c(vals_age, vals_denom, vals_repro)) %>% filter(sexe == "total") %>% group_by(lieu, base_equipe, ind_cat) %>% summarise( cases = sum(value, na.rm=T)) %>% filter(cases != 0) %>% ungroup(lieu) %>% complete(lieu, ind_cat)
ggplot(data = a, aes(x = lieu, y = cases, fill = ind_cat))+ geom_col(position = position_dodge(preserve = 'single'))+ facet_wrap(~lieu, scale = "free")+ theme_minimal()+ theme(legend.position = "bottom")+
labs( y = "Nombre des cases", x = "Lieu" )+ labs( title = "Cas de cliniques mobiles par site et par groupe de maladies (cumulatif)", subtitle = str_glue("MSF-OCB, Sud Departement, Haiti; donnees jusq'ua {format(max(tout$date, na.rm=T),'%d %b %Y')}"), fill = "Groupe de maladies" )
On top of ensuring that the bars are the same width (see above), use complete(a, b) in the dataset ensures the bars are the same position in each facet. See last comment in this: https://stackoverflow.com/questions/11020437/consistent-width-for-geom-bar-in-the-event-of-missing-data
This has been included in the latest update within the Factors chapter
https://www.aj2duncan.com/blog/missing-data-ggplot2-barplots/ https://stackoverflow.com/questions/10834382/ggplot2-keep-unused-levels-barplot
Also make more clear in Factors page how o keep all levels in a plot. Note you may need to use scale_x_discrete drop=F, as well as scalefill drop=F
how to deal with some values missing in some facets of barplots.
the solution that worked for me was: geom_col(position = position_dodge(preserve = 'single'))+