`facet_wrap` 与 `facet_col`:试图强制 geom_tile 在分面面板中具有相同大小的图块并为每个分面保留 x 轴标签
`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
我正在使用 ggplot2 制作日历。我希望日历看起来像这样:
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))
但是,我注意到 10 月份的图块比其他月份小 - 10 月份的天数比其他月份多一排。我想让所有的单元格都一样大。我可以使用 ggforce
中的 facet_col
参数而不是 facet_wrap
来执行此操作:ggforce::facet_col(vars(Month), scales = "free_y", space = "free") +
...但后来我松开了我的 x 轴标签(见下图),这很重要 - 我希望每个月都显示星期几。
是否有任何“简单”的解决方案可以使我的网格单元保持相同大小和我的 x 轴标签?
采用facet_col()
方法;将 scales = "free_y"
更改为 scales = "free"
不能解决问题吗?
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))
由 reprex package (v1.0.0)
于 2021 年 6 月 16 日创建
我正在使用 ggplot2 制作日历。我希望日历看起来像这样:
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))
但是,我注意到 10 月份的图块比其他月份小 - 10 月份的天数比其他月份多一排。我想让所有的单元格都一样大。我可以使用 ggforce
中的 facet_col
参数而不是 facet_wrap
来执行此操作:ggforce::facet_col(vars(Month), scales = "free_y", space = "free") +
...但后来我松开了我的 x 轴标签(见下图),这很重要 - 我希望每个月都显示星期几。
是否有任何“简单”的解决方案可以使我的网格单元保持相同大小和我的 x 轴标签?
采用facet_col()
方法;将 scales = "free_y"
更改为 scales = "free"
不能解决问题吗?
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))
由 reprex package (v1.0.0)
于 2021 年 6 月 16 日创建