Normalising column width whilst using facet_wrap and coord_flip in ggplot2

I would like to create a plot like the one below with each column adjusted to be the same width (instead of the facets being the same width. I have tried various methods but am unable to keep the columns the same when I use coord_flip() in the code.

Data set looks something like:
taxa LDA Analysis
A -3.7 One
B -3.5 One
C -3.8 Two
A -3.2 Three
C -3.4 Three
E -3.8 Three
F -3.7 Three

This is the code I have used to create the graph above so far.

plot2a <- ggplot(cca, aes(taxa,LDA, fill=Analysis))  + theme_classic()+ labs(y="score",x="Names") + theme (legend.position = 'none') + coord_flip(ylim=(c(-5,5)))  + geom_bar(stat = "identity")+ scale_y_continuous(breaks = c(-5,-4,-3,-2,-1,0,1,2,3,4,5)) + facet_wrap(~Analysis, scales = "free",ncol = 1)
plot2a

Any help much appreciated.

You might be looking for the space = "free_y" argument to `facet_grid(). Here's an example:

library(tidyverse)

mtcars %>% 
  rownames_to_column("car") %>% 
  ggplot(aes(car, disp)) +
  geom_col() +
  coord_flip() +
  facet_grid(rows = vars(cyl), scales = "free_y", space = "free_y", switch = "y")

Created on 2020-06-22 by the reprex package (v0.3.0)

2 Likes

Thanks, yes I have tried that already:

It just looks really messy though (I have adjusted height and width etc during export to try and account for this) as the facet variable names are long. It would be better to have the facet names above heading each section as a wrap rather than a grid if possible with the facet name on the side if possible. I have tried both 0 and 90 degree angles too. I just wondered if it was possible.

Many thanks.

The blue rectangles are where the facet names would be.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.