0

I am trying to recreate a bar graph (below) that has both stacked values (Yes/No/Not responding) and grouped values (e.g., the three bars under "Group activity" and the three bars under "In-cell activities").

Example of grouped and stacked graph

I currently have a stacked graph using the following code

ggplot(f15dta,aes(x=number,y=factor(activity,level=rev(activities)),fill=fill,order=desc(fill)))+
  geom_bar(stat="identity")+
  guides(fill=guide_legend(reverse=T))

which produces the below stacked graph. stacked graph

I have another variable in the data frame for the groups I want (e.g., "Group activity"). I tried using facets, but it looks like a complete mess (there are five groups/facets I want, but it graphs all the different bar graphs onto each one, so there's 90 bars instead of 18--see below)

facet graph

Does anyone have any ideas on how to better approach stacked and grouped graphs in ggplot? I feel like such graphs have to be fairly common, but am unsure how to proceed. Normally I would group using fill and position=dodge, but since I am stacking with fill, I am slightly unsure what to do. Thanks for the help.

babybonobo
  • 89
  • 8
  • 3
    Set the facet scales to "free_y" so that the labels. Depending on whether you used facet wrap or grid, you can adjust the spacing as well – camille Mar 06 '22 at 03:18
  • That worked! Thank you so much. For future reference for others, I did facet_grid(facet ~ .,scales="free_y") – babybonobo Mar 06 '22 at 03:29

0 Answers0