我怎样才能在GGPLOT2添加第二轴标签?(How can I add second axis la

2019-07-29 00:28发布

建立在我的前面的问题 ,我想对剧情的另一侧添加第二轴标签。

数据帧是这样的:

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))

利用泰勒的解决方案 ,它的图形看起来像在时刻:

以类似的方式,以一个森林图我想添加第二组标签( label变量在我的数据帧),代表值作图,优选在面板的右侧。 因此,它类似于此示例中,所有模仿森林图:

我知道,第二轴似乎在被皱起了眉头 。 然而,这些都只是另一套标签..蚂蚁似乎是森林图中的自定义。

我怎样才能做到这一点在ggplot?

Answer 1:

编辑更新到0.9.3 GGPLOT2

将您的标签集测试数据帧到多面的图表很简单。 使用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))

生产:



文章来源: How can I add second axis labels in ggplot2?
标签: r ggplot2