删除ggplot2中多个facet_grid中未使用的因子

时间:2015-03-01 00:17:17

标签: r ggplot2

我正在尝试从多面图中删除未使用的因子。我已经看了很多潜在的解决方案,但似乎都没有。我希望得到一些帮助。

最近在这里提出了这个问题:Removing unused factors from a facet_grid in ggplot2

但原始海报并没有为那些有兴趣回答帮助的人提供他的数据。所以我会因为我遇到了同样的问题。

dput(df)
structure(list(Study = c("Study 1", "Study 1", "Study 1", "Study 1", 
"Study 2", "Study 2", "Study 2", "Study 2", "Study 2", "Study 2"
), Subgroup = c("1", "1", "2", "2", "3", "3", "4", "4", "5", 
"5"), q.25. = c(254.850799560547, 161.833641052246, 27.8351449966431, 
18.8721599578857, 187.17, 30.74, 973.94, 155.62, 566.7, 296.09
), q.50. = c(645.594482421875, 736.949829101562, 72.1439666748047, 
52.3485088348389, 493.04, 84.11, 2164.89, 395.1, 2411.45, 750.5
), q.75. = c(981.924682617188, 1726.68432617188, 247.794635772705, 
239.050010681152, 1688.49, 243.02, 3633.31, 737.78, 4396.14, 
1565.57), Variable = c("Chemical 1", "Chemical 2", "Chemical 1", 
"Chemical 2", "Chemical 1", "Chemical 2", "Chemical 1", "Chemical 2", 
"Chemical 1", "Chemical 2")), .Names = c("Study", "Subgroup", 
"q.25.", "q.50.", "q.75.", "Variable"), row.names = c(NA, -10L
), class = "data.frame")

我正在尝试使用此代码制作一个情节:

ggplot(droplevels(df), aes(x = Subgroup, y = q.50., ymin = q.25., ymax = q.75.)) +
facet_grid(Study~ Variable, scales="free", drop = TRUE)+
geom_pointrange( position=position_dodge(width=1), size=1.1)+
ylab("Concentration (ng/g-lipid)")+
ggtitle("")+
scale_y_log10( )+
theme(axis.title.y = element_blank(),axis.text.x = element_text(angle = -30, hjust=0), axis.text.y = element_text(colour="grey20",face="bold"))+
coord_flip()

这是图: enter image description here

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以使用facet_wrap执行此操作,但遗憾的是您无法使用coord_flip()

library(ggplot2)
ggplot(droplevels(df), aes(x = Subgroup, y = q.50., ymin = q.25., ymax = q.75.)) +
  facet_wrap(Study ~ Variable, scales="free", drop = TRUE, ncol=2)+
  geom_pointrange( position=position_dodge(width=1), size=1.1)+
  ylab("Concentration (ng/g-lipid)")+
  ggtitle("")+
  scale_y_log10( )+
  theme(axis.title.y = element_blank(),axis.text.x = element_text(angle = -30, hjust=0), axis.text.y = element_text(colour="grey20",face="bold"))

输出:

enter image description here