Home > Mobile >  ggplot2 facet_wrap strip.position different for last column
ggplot2 facet_wrap strip.position different for last column

Time:10-07

I would like to have strip.position = "right" for the last column in my facet_wrap. I attempted to implement this enter image description here

Attempt

library(ggplot2)

p1 <- ggplot(dta, aes(x = level, y = N))   
  geom_col(aes(fill = level))   
  geom_text(aes(label = label), vjust = -0.1)  
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        legend.position = "left")   
  scale_y_continuous(expand = expansion(mult = c(0, .4)))   
  labs(fill = "")   
  facet_wrap(~ month,
             ncol = 3)

p2 <- ggplot(dta, aes(x = level, y = N))   
  geom_col(aes(fill = level))   
  geom_text(aes(label = label), vjust = -0.1)  
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        legend.position = "left")   
  scale_y_continuous(expand = expansion(mult = c(0, .4)))   
  labs(fill = "")   
  facet_wrap(~ month,
             ncol = 3,
             strip.position = "right")

left  <- ggplotGrob(p1)
right <- ggplotGrob(p2)

panels_left  <- panel_cols(left)$l
panels_right <- panel_cols(right)$l

gt <- cbind(left[1:20, 1:14],
            right[, 14:18])

grid::grid.newpage(); grid::grid.draw(gt)

enter image description here

Question

  1. Unless there is a different method out there for accomplishing this, how do I combine the first two columns (3x2) grid from p1 with the last column (3x1) of p2 so they align?
  2. How can I resize the elements from p1 and p2 so that I can adjust their sizes as needed?

CodePudding user response:

Instead of fiddling around with the gtable one option would be to create the first two columns and the third columns as separate plots by filtering the data. One drawback is that you have to fiddle around with plot widths so that the bars have the same width. I used a ratio of 2.5:1 which at least in my eye works fine.

library(ggplot2)
library(patchwork)

limits <- c(0, max(dta$N))

p1 <- ggplot(subset(dta, !grepl("^Wave", month)), aes(x = level, y = N))  
  geom_col(aes(fill = level))  
  geom_text(aes(label = label), vjust = -0.1)  
  scale_y_continuous(expand = expansion(mult = c(0, .4)), limits = limits)  
  labs(fill = NULL)  
  facet_wrap(~month, ncol = 2)  
  theme(
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank(),
    legend.position = "left",
    plot.margin = margin(5.5, r =  5.5 / 4, 5.5, 5.5)
  )

p2 <- ggplot(subset(dta, grepl("^Wave", month)), aes(x = level, y = N))  
  geom_col(aes(fill = level))  
  geom_text(aes(label = label), vjust = -0.1)  
  scale_y_continuous(expand = expansion(mult = c(0, .4)), limits = limits)  
  labs(fill = NULL)  
  facet_wrap(~month, ncol = 1, strip.position = "right")  
  theme(
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank(),
    legend.position = "none",
    plot.margin = margin(5.5, 5.5, 5.5, l = 5.5 / 4)
  )

gridExtra::grid.arrange(p1, p2, nrow = 1, widths = c(2.5, 1))

  • Related