R语言 ggplot中的手动森林图,如何调整变量标题?

plicqrtu  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(264)

我试图用一些荟萃分析的结果建立一个森林图(由我自己执行,所以我不想使用meta包)。您可以复制的结果如下:

structure(list(
cohort = c("cohort1", "cohort2", "cohort3", "cohort4", "cohort5", "cohort6", "cohort7"), 
beta = c(-0.238927428673765, -0.015095974676985, 0.364330350386939, -0.111741616010383, -0.196697525708519, -0.019993786083488, 0.0530815000660616), 
IC95_low = c(-0.339291563342713, -0.0857064739555255, -0.0365912072567037, -0.505483167285389, -0.472341750731663, -0.369903598485953, -0.147466346165432), 
IC95_high = c(-0.138563294004818, 0.0555145246015554, 0.765251908030581, 0.281999935264622, 0.0789466993146241, 0.329916026318977, 0.253629346297556), 
n = c(6199L, 531L, 109L, 452L, 529L, 826L, 924L), 
age = c(8.6, 2.5, 8.5, 7.8, 5.8, 4.2, 6.9)), 
row.names = c(NA, -7L), class = "data.frame")

我已经使用ggplot2gridExtra的以下代码实现了这些结果:

forest = result %>%
    ggplot(aes(y = cohort, x = beta)) +
    geom_point(aes(size = n), shape = 18) +
    geom_errorbarh(aes(xmin = IC95_low, xmax = IC95_high), height = 0.15) +
    geom_vline(xintercept = 0, color = "red", linetype = "dashed", cex = 0.7, alpha = 0.5) +
    labs(x = "mbmi beta", y = "") +
    theme(axis.text.y=element_blank(),
          axis.ticks.y=element_blank(),
          legend.position = "none"
    ) +
    scale_size_continuous(range = c(2,6))
  
  data_table <- ggplot(data = result, aes(y = cohort)) +
    geom_text(aes(x = 0, label = cohort), hjust = 0) +
    geom_text(aes(x = 0.7, label = n)) +
    geom_text(aes(x = 1, label = age), hjust = 1) +
    scale_colour_identity() +
    theme_void() + 
    theme(plot.margin = margin(5, 0, 35, 0))
  
  grid.arrange(data_table, forest, ncol = 2, widths = c(1,3),
               top = textGrob(paste("Results from available cohorts in:", outcome),gp=gpar(fontsize=20,font=4)))

然而,我想把表结构中的变量名称放在左边,并加上COHORT、n和mean_age......这感觉是不可能的,我不知道应该用哪种方法来做。
我们能讨论一下吗?

jljoyd4f

jljoyd4f1#

一个选项是添加列标题标签作为轴文本或标签。此外,我切换到patchwork来粘合您的绘图,恕我直言,它在对齐绘图时做得相当不错:

library(ggplot2)
library(patchwork)
outcome <- "gc"

forest <- result |>
  ggplot(aes(y = cohort, x = beta)) +
  geom_point(aes(size = n), shape = 18) +
  geom_errorbarh(aes(xmin = IC95_low, xmax = IC95_high), height = 0.15) +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed", cex = 0.7, alpha = 0.5) +
  labs(x = "mbmi beta", y = "") +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    legend.position = "none",
    plot.title = element_text(size = 24, face = "bold.italic")
  ) +
  scale_size_continuous(range = c(2, 6)) +
  labs(title = paste0("Results from available cohorts in: ", outcome))

data_table <- ggplot(data = result, aes(y = cohort)) +
  geom_text(aes(x = 0, label = cohort), hjust = 0) +
  geom_text(aes(x = 0.7, label = n)) +
  geom_text(aes(x = 1, label = age), hjust = 1) +
  scale_x_continuous(position = "top", breaks = c(0, .7, 1), labels = c("Cohort", "n", "age")) +
  scale_colour_identity() +
  theme_void() +
  theme(axis.text.x = element_text(hjust = c(0, .5, 1), face = "bold"))

data_table + forest & 
  plot_layout(ncol = 2, widths = c(1, 3))

相关问题