I have some data that are classified into 25 categories and 6 parent groups, with an uneven number of categories in each parent group (between 3 and 5). For each parent group, I used facet_wrap to create separate panels based on category, then displayed the parent group plots together using grid.arrange. The issue is that, since the number of categories in each parent group varies, the plot widths are automatically resized. Is there a way to manually set the plot widths or create empty panels so that the widths are consistent?
Simple recreation here:
library(dplyr)
library(gridExtra)
x <- c(1, 2, 3, 4, 1, 2)
y <- c(3, 3, 3, 3, 3, 3)
category <- c("category a", "category b", "category c", "category d", "category e", "category f")
group <- c("group one", "group one", "group one", "group one", "group two", "group two")
df <- data.frame(x, y, category, group)
group_one <- df %>% filter(group=="group one")
g1 <- ggplot(group_one, aes(x=x, y=y)) +
geom_point() +
facet_wrap(~category, nrow=1) +
labs(title="Group One")
group_two <- df %>% filter(group=="group two")
g2 <- ggplot(group_two, aes(x=x, y=y)) +
geom_point() +
facet_wrap(~category, nrow=1) +
labs(title="Group Two")
grid.arrange(g1, g2)
If there's an easier alternative way to create this type of plot arrangement with subheadings according to group, I'd love to hear it! Thanks!
