微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

`facet_wrap` 与 `facet_col`:试图强制 geom_tile 在分面面板之间具有相同大小的图块,并为每个分面保留 x 轴标签

如何解决`facet_wrap` 与 `facet_col`:试图强制 geom_tile 在分面面板之间具有相同大小的图块,并为每个分面保留 x 轴标签

我正在使用 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,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

但是,我注意到 10 月份的图块比其他月份小 - 10 月份的天数比其他月份多一排。我想让所有单元格的大小相同。我可以使用 facet_col 中的 ggforce 参数代替 facet_wrap 来执行此操作:ggforce::facet_col(vars(Month),scales = "free_y",space = "free") + ...但后来我松开了我的 x 轴标签(见下图),这很重要 - 我希望每个月都显示一周中的几天。

是否有任何“简单”的解决方案可以让我的网格单元保持相同的大小我的 x 轴标签

Calendar with facet_col

解决方法

使用 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,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 日创建

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?