`facet_wrap` vs `facet_col`: trying to force geom_tile to have equal sized tiles among facet panels and retain x-axis labels for each facet

Viewed 117

I am making a calendar using ggplot2. I'd like the calendar to look like this:

library(tidyverse)
library(lubridate)
library(ggforce)

datedb <- tibble(date = seq(as.Date("2021-09-01"), 
                                as.Date("2021-12-31"), by = 1),
                    day = day(date),
                     week_no = epiweek(date),
                     Month = month(date, label = TRUE, abbr = FALSE),
                     Wday = wday(date, label = TRUE, abbr = TRUE))

ggplot(datedb, aes(Wday, week_no)) +
  geom_tile(color = "black", fill = NA) +
  geom_text(aes(label = day), size = 3) +
  scale_y_reverse() +
  scale_x_discrete(position = "top") +
  facet_wrap(~ Month, ncol = 1, scales = "free",
             strip.position = "top") +
  theme_void() +
  theme(axis.text.x = element_text(size = 9),
        strip.placement = "outside",
        strip.text = element_text(size = 13))

Calendar with facet_wrap

However, I noticed that the tiles in the month of October are smaller than the other months - October has one more row of days than the other months. I'd like to make all the cells the same size. I can use the facet_col argument in ggforce instead of facet_wrap to do this: ggforce::facet_col(vars(Month), scales = "free_y", space = "free") + ... but then I loose my x-axis labels (see image below), which are important - I'd like each month to show days of the week.

Is there any "easy" solution to keep my grid cells the same size and my x-axis labels?

Calendar with facet_col

1 Answers

With the facet_col() approach; doesn't changing scales = "free_y" to scales = "free" solve the problem?

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(ggforce)

datedb <- tibble(date = seq(as.Date("2021-09-01"), 
                            as.Date("2021-12-31"), by = 1),
                 day = day(date),
                 week_no = epiweek(date),
                 Month = month(date, label = TRUE, abbr = FALSE),
                 Wday = wday(date, label = TRUE, abbr = TRUE))

ggplot(datedb, aes(Wday, week_no)) +
  geom_tile(color = "black", fill = NA) +
  geom_text(aes(label = day), size = 3) +
  scale_y_reverse() +
  scale_x_discrete(position = "top") +
  facet_col(~ Month, scales = "free", space = "free",
             strip.position = "top") +
  theme_void() +
  theme(axis.text.x = element_text(size = 9),
        strip.placement = "outside",
        strip.text = element_text(size = 13))

Created on 2021-06-16 by the reprex package (v1.0.0)

Related