7

基于我之前的问题,我想在绘图的另一侧添加第二个轴标签。

数据框如下所示:

test <- structure(list(characteristic = structure(c(1L, 2L, 3L, 1L, 2L
), .Label = c("Factor1", "Factor2", "Factor3"), class = "factor"), 
    es = c(1.2, 1.4, 1.6, 1.3, 1.5), ci_low = c(1.1, 1.3, 1.5, 
    1.2, 1.4), ci_upp = c(1.3, 1.5, 1.7, 1.4, 1.6), label = structure(c(1L, 
    3L, 5L, 2L, 4L), .Label = c("1.2 (1.1, 1.3)", "1.3 (1.2, 1.4)", 
    "1.4 (1.3, 1.5)", "1.5 (1.4, 1.6)", "1.6 (1.5, 1.7)"), class = "factor"), 
    set = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("H", "S"
    ), class = "factor")), .Names = c("characteristic", "es", 
"ci_low", "ci_upp", "label", "set"), class = "data.frame", row.names = c(NA, 
-5L))

使用Tyler 的解决方案,它的图形现在看起来像这样:

在此处输入图像描述

以类似于森林图的方式,我想添加第二组标签(label我的数据框中的变量)代表图形值,最好在面板的右侧。所以这一切都模仿了类似于这个例子的森林图:

在此处输入图像描述

我知道第二个轴似乎不受欢迎。然而,这些只是另一组标签。它似乎是森林地块中的一种习俗。

我怎么能在ggplot中做到这一点?

4

2 回答 2

6

编辑更新到 ggplot2 0.9.3

将测试数据框中的一组标签添加到多面图表非常简单。geom_text与标签的美学以及标签的 x 和 y 位置一起使用。在下面的代码中,xlim为标签创建了更多空间。以下代码:

library(gridExtra)
library(ggplot2)

p <- ggplot(test, aes(y = characteristic, x = es, xmin = ci_low, xmax = ci_upp)) + 
  geom_point() +   
  geom_errorbarh(height = 0) +
  geom_text(aes(label = label, x = 2, y = characteristic)) + 
  scale_x_continuous(limits = c(1, 2.2), breaks = c(1, 1.2, 1.4, 1.6, 1.8),
    labels=c("1.0", "1.2", "1.4", "1.6", "1.8")) +
  facet_grid(set ~ ., scales = "free", space = "free") +
  theme_bw() + 
  theme(strip.text.y = element_text(angle = 0),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank())
p

grid.text(expression(paste("ES " %+-% " ci")), x = 0.78,   y = .92,
   gp = gpar(fontsize = 18))

产生:

在此处输入图像描述

于 2012-04-19T01:49:51.643 回答
0

这是我的答案:(由于没有找到完整的答案,我不死帖子。我仍在寻找添加“钻石”摘要)

它非常“自动化”。

  • lgOR = 具有估计值和 CI 95% 的数据。

  • ci_lower = 95% CI 更低

  • ci_upper = 95% CI 上限

  • 作者=作者/日期

  • Model_pars <- paste( orH2, ort2, orI2, orModel, orModel_sig, sep=" | ") # 这些是 Heterogeneity stats - as subtitle.

     min_v     <- floor(min(lgOR$ci_lower))
     max_v     <- ceiling(max(lgOR$ci_upper))
     OR_v      <- as.matrix( round(lgOR$OR,2) )
     ORci      <- paste("(", round(lgOR$ci_lower,2), "|", round(lgOR$ci_upper,2), ")" )
    
     ggplot(data=lgOR, aes(y=1:nrow(lgOR), x=OR, xmin=ci_lower, xmax=ci_upper)) +
       geom_point() + 
       geom_errorbarh(height=.3) +
       scale_y_continuous(name = "", breaks=1:nrow(lgOR), labels=lgOR$author ) +
       scale_x_continuous(limits = c( min_v, max_v + 3 ), 
                          breaks = c( min_v:max_v, max_v + 3 ),
                          expand = c(0,0) )+
       labs(title='Effect Size by Study', x=Model_pars, y = 'Study') +
       geom_vline(xintercept=0, color='black', linetype='dashed', alpha=.5) +
       theme( axis.title.x = element_text(size = 8),
              axis.text.x = element_text(size = 7),
              axis.title.y = element_text(size = 1)) +
       geom_text(aes(x=9, label= paste(OR_v, ORci ))) 
    
于 2021-10-11T11:08:58.510 回答