我如何从ggplot2方面中删除空因素? [英] How can I remove empty factors from ggplot2 facets?

查看:127
本文介绍了我如何从ggplot2方面中删除空因素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图修改一个简单林的示例根据一个因子变量引入分面。



假设这个结构的数据:

<$ p (特征结构(c(1L,2L,3L,1L,2L
),。标签= c(因子1,因子2)) ,因子3),类别=因子),
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 (特征,ES,
ci_low,ci_upp,label,set),class =data.frame,row.names = c(NA,
-5L))

并运行代码:

  p < -  ggplot(test,aes(x = characteristic,y = es ,ymin = ci_low,ymax = ci_upp))+ geom_pointrange()+ 
coord_flip()+ geom_hline(aes(x = 0),lty = 2)+
facet_wrap(〜set,ncol = 1) +
theme_bw()+
opts(strip.text.x = theme_text())

产生如下输出:





迄今为止都很好。但是,我想从我的下面板中摆脱空的Factor3级别,并且找不到这样做的方法。有没有办法做到这一点?



感谢您的帮助。 p> 编辑已更新至ggplot2 0.9.3



这是另一种解决方案。它使用 facet_grid space =free;它还使用 geom_point() geom_errorbarh(),因此不需要 coord.flip()。此外,x轴刻度标记标签仅出现在下面板上。在下面的代码中,主题命令不是必需的 - 它用于将条形文本旋转为水平显示。使用上面的 test dataframe,下面的代码应该产生你想要的:

  $ bp < -  ggplot(test,aes(y = characteristic,x = es,xmin = ci_low,xmax = ci_upp))+ 
geom_point()+
geom_errorbarh(height = 0)+
facet_grid(set〜。,scales =free,space =free)+
theme_bw()+
主题(strip。 text.y = element_text(angle = 0))

p

解决方案基于Wickham的ggplot2书中第124页的示例。

I'm trying to modify an example of a simple forest plot by introducing facets according to a factor variable.

Assuming data of this structure:

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

And running the code:

p <- ggplot(test, aes(x=characteristic, y=es, ymin=ci_low, ymax=ci_upp)) + geom_pointrange() +
  coord_flip() + geom_hline(aes(x=0), lty=2) + 
  facet_wrap(~ set, ncol = 1) +
  theme_bw() + 
  opts(strip.text.x = theme_text())

Produces output like that:

All good so far. However, I'd like to get rid of empty Factor3 level from my lower panel and cannot find a way to do that. Is there any way to do that?

Thanks for help.

解决方案

EDIT Updated to ggplot2 0.9.3

Here's another solution. It uses facet_grid and space = "free"; also it uses geom_point() and geom_errorbarh(), and thus there is no need for coord.flip(). Also, the x-axis tick mark labels appear on the lower panel only. In the code below, the theme command is not essential - it is used to rotate the strip text to appear horizontally. Using the test dataframe from above, the following code should produce what you want:

library(ggplot2)

p <- ggplot(test, aes(y = characteristic, x = es, xmin = ci_low, xmax = ci_upp)) +
   geom_point() +
   geom_errorbarh(height = 0) +
   facet_grid(set ~ ., scales = "free", space = "free") +
   theme_bw() +
   theme(strip.text.y = element_text(angle = 0))

p

The solution is based on the example on page 124 in Wickham's ggplot2 book.

这篇关于我如何从ggplot2方面中删除空因素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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