teunbrand / ggnomics

A small project to add ggplot2 extensions
https://teunbrand.github.io/ggnomics/
Other
80 stars 4 forks source link

Uneven Facets in facet_nested() #36

Closed dbro970 closed 4 years ago

dbro970 commented 4 years ago

Hey, I'm using your facet_nested code to generate stacked bar plots in R. (Super useful function so thanks for that by the way) Unfortunately the variables I'm using to facet by have an uneven number of observations, so in some of them the bars appear huge while in others they appear small. I'm still a bit new to the more in depth coding side of R, so I may have missed this but is there a way to vary the size of the nested facets and "even" the observation bars so they're all about the same size? i.e. Make facets with more observations wider so that the bar width for each observation is the same?

teunbrand commented 4 years ago

Hi there, Is this approximately what you would mean?

library(ggnomics)

df <- mpg
americancars <- c("chevrolet", "dodge", "ford", 
                  "jeep", 'land rover', "lincoln")
df$nationality <- ifelse(df$manufacturer %in% americancars,
                   "American", "Other")

ggplot(df, aes(class, hwy, fill = trans)) +
  geom_col(position = "stack") +
  facet_nested(~ nationality + manufacturer, 
               scales = "free_x", space = "free_x")

image

Note specifically the scales = "free_x", space = "free_x" options. If this is not what you mean, could you maybe provide a minimal reproducible example that shows what the issue is?

dbro970 commented 4 years ago

Ah yes thats exactly what I was after! Thanks heaps