r - 如何防止 gridExtra::grid.arrage 在 R 中自动缩放 x 轴?
问题描述
我在循环中运行以下每个图都没有问题,但由于某种原因,当我尝试在 grid.arrange 中组合它们时,轴会自动缩放。我不知道如何不自动缩放。我实际上只是想将 ggplots 组合在一起(就像我使用 par(mfrow = c(2,3)) 和 base R 一样)。
我认为 X 轴实际上是重新格式化的日期时间 (as.posixct) 的事实正在抛弃它。理想情况下,我希望 X 轴只显示小时。
library(ggplot2)
library(bp)
df <- data(hypnos_data)
ids <- unique(df$ID)
id_tab <- cbind(ids, c(1:5))
index <- c(8:23, 0:7)
plot_list <- list()
for(i in ids){
subs <- df[which(df$ID == i & df$VISIT == 1),]
subs$DATE.TIME <- as.POSIXct(subs$DATE.TIME)
subs$hour_rec <- lubridate::hour(subs$DATE.TIME)
subs$hour_rec <- factor(subs$hour_rec, levels = as.character(index), ordered = T)
row.names(subs) <- NULL
tmp <- subs %>% dplyr::filter(WAKE == 0)
min(tmp$hour_rec)
p <- ggplot2::ggplot(subs, ggplot2::aes(x = DATE.TIME, y = SYST)) +
geom_rect(aes(xmin = min(tmp$DATE.TIME), xmax = max(tmp$DATE.TIME), ymin = -Inf, ymax = Inf),
fill = "navajowhite2", alpha = 0.03) +
ggplot2::geom_point(aes(y = SYST), col = 'blue') +
ggplot2::geom_smooth(aes(y = SYST), method = "loess", col = 'blue') +
ggplot2::geom_point(aes(y = DIAST), col = 'red') +
ggplot2::geom_smooth(aes(y = DIAST), method = "loess", col = 'red') +
scale_x_datetime(date_label = "%H:%M", date_breaks = "1 hour") +
theme(axis.text.x = element_text(angle=45)) +
ggtitle( paste("BP Profile for Subject: ", i, sep = "") )
plot_list[[match(i, id_tab)]] <- p
print(p)
#grid::grid.newpage()
}
上面的代码应该产生 5 个与此类似的图(这是正确的):
单独的绘图输出
gridExtra::grid.arrange(grobs = plot_list, ncol = 2 )
但是,尝试使用 grid.arrange 组合它们会产生以下结果:
grid.arrange 绘图输出
解决方案
问题不在于 .grid.arrange
这是由于ggplot2
. 一些轴数据是参考值,而不是图中存储的值。这会导致每次交互时subs
&发生变化时轴的变化。这是一种使用tmp
for-loop
eval(substitute(...))
library(ggplot2)
library(bp)
library(doParallel)
#> Loading required package: foreach
#> Loading required package: iterators
#> Loading required package: parallel
data(hypnos_data)
df <- hypnos_data
ids <- unique(df$ID)
id_tab <- cbind(ids, c(1:5))
index <- c(8:23, 0:7)
plot_list <- list()
plot_list <- foreach(i = ids) %do% {
eval(substitute({
subs <- df[which(df$ID == i & df$VISIT == 1),]
subs$DATE.TIME <- as.POSIXct(subs$DATE.TIME)
subs$hour_rec <- lubridate::hour(subs$DATE.TIME)
subs$hour_rec <- factor(subs$hour_rec, levels = as.character(index), ordered = T)
row.names(subs) <- NULL
tmp <- subs %>% dplyr::filter(WAKE == 0)
min(tmp$hour_rec)
p <- ggplot2::ggplot(subs, ggplot2::aes(x = DATE.TIME, y = SYST)) +
geom_rect(aes(xmin = min(tmp$DATE.TIME), xmax = max(tmp$DATE.TIME), ymin = -Inf, ymax = Inf),
fill = "navajowhite2", alpha = 0.03) +
ggplot2::geom_point(aes(y = SYST), col = 'blue') +
ggplot2::geom_smooth(aes(y = SYST), method = "loess", col = 'blue') +
ggplot2::geom_point(aes(y = DIAST), col = 'red') +
ggplot2::geom_smooth(aes(y = DIAST), method = "loess", col = 'red') +
scale_x_datetime(date_label = "%H:%M", date_breaks = "1 hour") +
theme(axis.text.x = element_text(angle=45)) +
ggtitle( paste("BP Profile for Subject: ", i, sep = "") )
}, list(subs = as.name(paste0("subs_", i)), tmp = as.name(paste0("tmp_", i)))))
p
}
gridExtra::grid.arrange(grobs = plot_list)
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
由reprex 包于 2021-03-31 创建(v1.0.0)
推荐阅读
- r - 如何使用 bind_rows() 合并数据集,同时将数据集的名称保留为变量
- php - 使用 PHP 访问 JSON 字段
- linux - 如果我映射一个没有设置访问位的内存区域,mlockall 是否仍然强制它由物理内存支持?
- amazon-web-services - 如何在 Kubernetes 部署中使用 Same EFS 挂载多个目录
- unity3d - 在 Unity 的 vfx 图中找不到现有节点
- c# - C# 使用 wuapi 安装 Windows 10 功能更新
- android - Android DefaultHttpClient、HttpPost 等已弃用 (SDK 30)
- python - 转换和求和熊猫数据框列内嵌套列表中的元素
- helidon - 在 WSL 上安装 Helidon
- c++ - 将一个用户定义的类转换为另一个用户定义的类时出现问题