Home > other >  ggplot2: Trim wasted space on stacked horizontal bar chart
ggplot2: Trim wasted space on stacked horizontal bar chart

Time:10-04

I have a horizontally stacked bar chart that is almost perfect, sans the wasted vertical space.

ggplot(category_df, aes(x = OPERATION, y = CATEGORY_LOT_COUNT, fill=HOLD_CATEGORY, label=CATEGORY_LOT_COUNT))  
            geom_bar(position="stack", stat="identity", width=0.1)  
            xlab("")  
            theme(axis.title.x = element_blank(),
                  axis.ticks.x=element_blank(),
                  axis.text.x=element_blank(),
                  legend.title.align = 0.5,
                  legend.direction = 'horizontal',
                  legend.position = 'bottom')  
            geom_text(position = position_stack(vjust = 0.5), size=10, color='white', face = 'bold')  
            coord_fixed(ratio = .2)  
            coord_flip() 

Produces:

enter image description here

I would like it to look like this:

enter image description here

As you can see above I tried to use coord_fixed with no success.

thx

CodePudding user response:

It depends on how you export the graphic. In RStudio you might drag and adjust the plots-window prior to exporting. For anything else than a quick export, however, I would recommend to use ggplot2::ggsave() and set the width, height and unit-parameters according to your desire. As Stefan pointed out: width=0.1 will shrink the bar width to something rather slim.

NB: This is a non-reproducible example, as we don't have access to category_df.

  • Related