首页 > 解决方案 > 如何使用 ggarrange() 或类似方法手动调整合并图的高度

问题描述

我已经制作了这个图,它基本上描述了从 Cox 回归得到的预测的 1 年和 3 年进展风险,以及作为累积剂量函数的某个肿瘤。

问题:我曾经ggarrange()把两个地块合并在一起,但不幸的是,由于申请theme(legend.position = "bottom")了这个地块,上面的地块被更多地挤在一起,但show.legend = F为了下层地块。因此,它们y-axes具有不同的高度。

问题:有没有办法手动调整排列图的高度以获得相同的高度,y-axes同时保留上图的图例?如果可以解决问题,我愿意使用其他情节合并方法。

在此处输入图像描述

我的脚本:

# Defining theme
theme <- theme(axis.line = element_line(colour = "black"),
               panel.grid.major = element_line(colour = "gray95"),
               panel.grid.minor = element_line(colour = "gray95"),
               panel.border = element_blank(),
               panel.background = element_blank(),
               axis.text.x = element_text(color = "grey20", size =16), 
               axis.title.x = element_text(color = "grey20", size = 16, face="bold", margin=ggplot2::margin(t=10)),
               axis.text.y = element_text(color = "grey20", size = 16), 
               axis.title.y = element_text(color = "grey20", size = 17, face="bold", margin=ggplot2::margin(r=10)),
               legend.key = element_rect(fill = "white"),
               plot.title = element_text(color = "grey20", size = 20, face="bold",hjust = 0),
               plot.subtitle = element_text(hjust = 0.5),
               legend.text=element_text(size=17), legend.title=element_text(size=20)) 

ggarrange(
ggplot(df_yr1, aes(x = total.ssa, y = value, colour = name, group = name))  + 
  
  theme + theme(axis.title.x = element_text(size=15),
                plot.title = element_text(hjust = 0.5, size=20),
                plot.subtitle=element_text(size=18, hjust=0.5),
                legend.position = "bottom") + 
  
  ggtitle(label = "1 year: predicted risk of progression") +
  
  geom_area(aes(fill = name), position = "stack", alpha = 0.15) +
  geom_line(colour = "white", size = 3, position = "stack") +
  geom_line(position = "stack", show.legend = F) +
  geom_point(position = "stack", data = df_yr1[c(1:3, -2:0 + nrow(df_yr1)), ], 
             shape=21, fill="white", stroke=1, size=3) +
  scale_fill_manual(values = c("#E1B930", "#8B3A62", "#6DBCC3"),
                    labels = c("WHO-3", "WHO-2", "WHO-1"), 
                    name="",
                    guide = guide_legend(reverse = TRUE)) +
  scale_colour_manual(values = c("#E1B930", "#8B3A62", "#6DBCC3"),
                      labels = c("WHO-3", "WHO-2", "WHO-1"), 
                      name="",
                      guide = guide_legend(reverse = TRUE)) +
  scale_y_continuous(breaks = seq(0, 1, .2),
                     labels = paste0(seq(0, 100, 20), "%"),
                     name="",
                     limits = c(0, 1)) +
  scale_x_continuous(breaks = seq(0, 1000, 200), 
                     limits = c(0, 1000),
                     name="Somatostatin analogues (cumulative mg)\n"),
  ggplot(df_yr3, aes(x = total.ssa, y = value, colour = name, group = name))  + 
    
    theme + theme(axis.title.x = element_text(size=15),
                  plot.title = element_text(hjust = 0.5, size=20),
                  plot.subtitle=element_text(size=18, hjust=0.5)) + 
    
    ggtitle(label = "\n3 years: predicted risk of progression") +
    
    geom_area(aes(fill = name), position = "stack", alpha = 0.15, show.legend = F) +
    geom_line(colour = "white", size = 3, position = "stack", show.legend = F) +
    geom_line(position = "stack", show.legend = F) +
    geom_point(position = "stack", data = df_yr3[c(1:3, -2:0 + nrow(df_yr3)), ], 
               shape=21, fill="white", stroke=1, size=3, show.legend = F) +
  scale_fill_manual(values = c("#E1B930", "#8B3A62", "#6DBCC3"),
                    labels = c("WHO-3", "WHO-2", "WHO-1"), 
                    name="",
                    guide = guide_legend(reverse = TRUE)) +
    scale_colour_manual(values = c("#E1B930", "#8B3A62", "#6DBCC3"),
                        labels = c("WHO-3", "WHO-2", "WHO-1"), 
                        name="",
                        guide = guide_legend(reverse = TRUE)) +
    scale_y_continuous(breaks = seq(0, 1, .2),
                       labels = paste0(seq(0, 100, 20), "%"),
                       name="",
                       limits = c(0, 1)) +
    scale_x_continuous(breaks = seq(0, 1000, 200), 
                       limits = c(0, 1000),
                       name="Somatostatin analogues (cumulative mg)\n"),
  ncol = 1, nrow = 2
)

我的数据

df_yr1 <- structure(list(total.ssa = c(0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 
3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 
10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 
15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 
21, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 
26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 
31, 32, 32, 32, 33, 33, 33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 
37, 37, 37, 38, 38, 38, 39, 39, 39, 40, 40, 40, 41, 41, 41, 42, 
42, 42, 43, 43, 43, 44, 44, 44, 45, 45, 45, 46, 46, 46, 47, 47, 
47, 48, 48, 48, 49, 49, 49, 50, 50, 50, 51, 51, 51, 52, 52, 52, 
53, 53, 53, 54, 54, 54, 55, 55, 55, 56, 56, 56, 57, 57, 57, 58, 
58, 58, 59, 59, 59, 60, 60, 60, 61, 61, 61, 62, 62, 62, 63, 63, 
63, 64, 64, 64, 65, 65, 65, 66, 66), name = structure(c(1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L), .Label = c("who3", "who2", "who1"), class = "factor"), 
    value = c(0.029, 0.064, 0.907, 0.03, 0.064, 0.906, 0.03, 
    0.065, 0.905, 0.031, 0.065, 0.904, 0.031, 0.066, 0.903, 0.032, 
    0.066, 0.902, 0.032, 0.067, 0.901, 0.033, 0.068, 0.9, 0.033, 
    0.068, 0.899, 0.034, 0.069, 0.897, 0.034, 0.069, 0.896, 0.035, 
    0.07, 0.895, 0.035, 0.07, 0.894, 0.036, 0.071, 0.893, 0.036, 
    0.071, 0.892, 0.037, 0.072, 0.891, 0.038, 0.073, 0.89, 0.038, 
    0.073, 0.888, 0.039, 0.074, 0.887, 0.039, 0.074, 0.886, 0.04, 
    0.075, 0.885, 0.041, 0.075, 0.884, 0.041, 0.076, 0.883, 0.042, 
    0.077, 0.881, 0.042, 0.077, 0.88, 0.043, 0.078, 0.879, 0.044, 
    0.078, 0.878, 0.044, 0.079, 0.877, 0.045, 0.079, 0.875, 0.046, 
    0.08, 0.874, 0.046, 0.08, 0.873, 0.047, 0.081, 0.872, 0.048, 
    0.082, 0.87, 0.048, 0.082, 0.869, 0.049, 0.083, 0.868, 0.05, 
    0.083, 0.867, 0.05, 0.084, 0.865, 0.051, 0.084, 0.864, 0.052, 
    0.085, 0.863, 0.052, 0.086, 0.861, 0.053, 0.086, 0.86, 0.054, 
    0.087, 0.859, 0.055, 0.087, 0.858, 0.055, 0.088, 0.856, 0.056, 
    0.088, 0.855, 0.057, 0.089, 0.854, 0.057, 0.089, 0.852, 0.058, 
    0.09, 0.851, 0.059, 0.091, 0.85, 0.06, 0.091, 0.848, 0.061, 
    0.092, 0.847, 0.061, 0.092, 0.846, 0.062, 0.093, 0.844, 0.063, 
    0.093, 0.843, 0.064, 0.094, 0.841, 0.065, 0.094, 0.84, 0.065, 
    0.095, 0.839, 0.066, 0.095, 0.837, 0.067, 0.096, 0.836, 0.068, 
    0.096, 0.834, 0.069, 0.097, 0.833, 0.069, 0.098, 0.832, 0.07, 
    0.098, 0.83, 0.071, 0.099, 0.829, 0.072, 0.099, 0.827, 0.073, 
    0.1, 0.826, 0.074, 0.1)), row.names = c(NA, -200L), class = c("tbl_df", 
"tbl", "data.frame"))

df_yr3 <- structure(list(total.ssa = c(0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 
3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 
10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 
15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 
21, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 
26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 
31, 32, 32, 32, 33, 33, 33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 
37, 37, 37, 38, 38, 38, 39, 39, 39, 40, 40, 40, 41, 41, 41, 42, 
42, 42, 43, 43, 43, 44, 44, 44, 45, 45, 45, 46, 46, 46, 47, 47, 
47, 48, 48, 48, 49, 49, 49, 50, 50, 50, 51, 51, 51, 52, 52, 52, 
53, 53, 53, 54, 54, 54, 55, 55, 55, 56, 56, 56, 57, 57, 57, 58, 
58, 58, 59, 59, 59, 60, 60, 60,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0, 0, 1, 0, 0, 1, 0, 0.001, 0.999, 0, 0.001, 
    0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 
    0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 
    0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 
    0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 
    0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 
    0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 
    0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 
    0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 
    0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 0, 0.001, 0.999, 
    0, 0.001, 0.998, 0, 0.002, 0.998, 0, 0.002, 0.998, 0, 0.002, 
    0.998, 0, 0.002, 0.998, 0, 0.002, 0.998, 0, 0.002, 0.998, 
    0, 0.002, 0.998, 0, 0.002, 0.998, 0, 0.002, 0.998, 0, 0.002, 
    0.998, 0, 0.002, 0.998, 0, 0.002, 0.998, 0, 0.002, 0.998, 
    0, 0.002, 0.998, 0, 0.002, 0.998, 0, 0.002, 0.998, 0, 0.002, 
    0.997, 0, 0.002)), row.names = c(NA, -200L), class = c("tbl_df", 
"tbl", "data.frame"))

标签: rggplot2plotlegend

解决方案


更新: OP 对patchwork解决方案的需求。

将你的地块命名为 p1 和 p2:

library(patchwork)

p1 + p2 + plot_layout(ncol=1,heights=c(2,2))

在此处输入图像描述

原始答案: 试试这个:在代码末尾替换

ncol = 1, nrow = 2

经过

heights = c(4, 3.5), nrow = 2, align = "v"

我无法重现您的情节:但使用上面的代码,您可以调整情节的高度。只是为了展示情节的高度: 在此处输入图像描述


推荐阅读