In the plot below, we use position=position_stack(vjust=0.5) to center the labels within each section of the stacked bars, and we use scales="free_x", space="free_x" to avoid blank spaces for empty x-axis levels.
If I want this to be a horizontal bar plot, the natural approach would be to add coord_flip, as in the code below. However, with coord_flip, scales="free_x", space="free_x" no longer removes the space for the empty levels.
An alternative would be to use geom_barh from ggstance as in the code below. However, position=position_stackv(hjust=0.5) doesn't work, because position_stackv doesn't take an hjust argument. It would be nice to have the hjust argument available so that the text labels can be centered within each bar portion.
I realize I can set the label x-values when I summarize the data, as in the code below, but it would be nice to just be able to do position=position_stackv(hjust=0.5) just as we can do position=position_stack(vjust=0.5) in the vertical bar plot.
Here's a reprex:
Summarize data for plot:
In the plot below, we use
position=position_stack(vjust=0.5)
to center the labels within each section of the stacked bars, and we usescales="free_x", space="free_x"
to avoid blank spaces for empty x-axis levels.If I want this to be a horizontal bar plot, the natural approach would be to add
coord_flip
, as in the code below. However, withcoord_flip
,scales="free_x", space="free_x"
no longer removes the space for the empty levels.An alternative would be to use
geom_barh
fromggstance
as in the code below. However,position=position_stackv(hjust=0.5)
doesn't work, becauseposition_stackv
doesn't take anhjust
argument. It would be nice to have thehjust
argument available so that the text labels can be centered within each bar portion.I realize I can set the label x-values when I summarize the data, as in the code below, but it would be nice to just be able to do
position=position_stackv(hjust=0.5)
just as we can doposition=position_stack(vjust=0.5)
in the vertical bar plot.