如何在ggplot2中添加第二个轴标签? [英] How can I add second axis labels in ggplot2?

查看:196
本文介绍了如何在ggplot2中添加第二个轴标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的。然而,这些只是另一组标签..蚂蚁似乎是森林地块中的一种习俗。



我如何在ggplot中做到这一点?

解决方案

编辑更新到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 =表达式(paste(ES%+ - %ci)),x = 0.78,y = .92,
gp = gpar(fontsize = 18))

产生:


Building on my previous question I'd like to add second axis label on the opposite side of the plot.

The data frame looks like:

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

Using Tyler's solution, a graph of it looks like that at the moment:

In a similar way to a forest plot I'd like to add a second set of labels (label variable in my data frame) representing graphed values, preferably on the right side of panels. So that it all mimics forest plot similar to this example:

I know that second axis seems to be frowned upon. However these are just another set of labels.. ant it seems to be a custom among forest plots.

How can I do that in ggplot?

解决方案

EDIT updating to ggplot2 0.9.3

Adding your set of labels in your test dataframe to the faceted chart is straightforward. Use geom_text with aesthetics for the labels, and the x and y positions of the labels. In the code below, xlim creates a little more space for the labels. The following code:

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

produces:

这篇关于如何在ggplot2中添加第二个轴标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆