使用ggplot2制作嵌套分面图

8
如果我有一个嵌套因素,在这种情况下,我有多个“家庭”级别包含在因素“订单”中,我希望可能创建一个
facet_grid(Family / Order ~.)

现在应该改为

facet_grid(Family + Order ~.) 

基本上,每个订单需要一个带有该订单内每个家族的所有条带的条带。我知道facet_grid(Family / Order ~。)目前是不可能的,但我该如何实现这种效果?可以使用theme()吗?非常感谢。--SB
我应该在上面指定Family和Order都是因素。数据值B是属于具有家族水平和订单水平的物种。这是我的绘图代码:
p <- ggplot(models, aes(B,Species)) + geom_point() + facet_grid(Family + Order ~
 .,scales="free",space="free")

这里是一些示例数据:

structure(list(Species = c("Acanthocyclops robustus", "Acroperus harpae", 
"Alona affinis", "Ascaphus truei", "Bosmina longirostris"), Intercept = c(-36.1182388331068, 
-27.2140776216155, -25.7920464721491, -39.2233884219763, -31.4301301084581
), B = c(0.919397836908493, 0.716601987210452, 0.685455190113372, 
1.04159758611351, 0.81077051300147), Bconf = c(0.407917065756464, 
0.181611850119198, 0.254101713856315, 0.708582768458448, 0.234313394549538
), Order = c("Cyclopoida", "Diplostraca", "Diplostraca", "Anura", 
"Diplostraca"), Family = c("Cyclopidae", "Chydoridae", "Chydoridae", 
"Leiopelmatidae", "Bosminidae")), .Names = c("Species", "Intercept", 
"B", "Bconf", "Order", "Family"), row.names = c(NA, 5L), class = "data.frame")

2
你应该附上一些代码作为例子。根据你的描述,很难看出 facet_grid(Order ~ Family) 为什么不能给你想要的输出结果。 - emhart
我在上面发布了更多详细信息。谢谢。 - user1536207
2
请提供一个示例数据集。如果没有关于您的数据结构的一些信息,无法回答您的问题。 - Sven Hohenstein
我在上面添加了一些数据。 - user1536207
2个回答

3

使用 facet_gridfacet_wrap 无法构建你正在尝试构建的图形。但是,你可以构建一个图形列表,然后通过 gridExtra::grid.arrange 绘制它们。这里是一个例子:

library(ggplot2)
library(gridExtra)
library(dplyr)

dat <-
  structure(list(Species = c("Acanthocyclops robustus", "Acroperus harpae", 
  "Alona affinis", "Ascaphus truei", "Bosmina longirostris"), Intercept = c(-36.1182388331068, 
  -27.2140776216155, -25.7920464721491, -39.2233884219763, -31.4301301084581
  ), B = c(0.919397836908493, 0.716601987210452, 0.685455190113372, 
  1.04159758611351, 0.81077051300147), Bconf = c(0.407917065756464, 
  0.181611850119198, 0.254101713856315, 0.708582768458448, 0.234313394549538
  ), Order = c("Cyclopoida", "Diplostraca", "Diplostraca", "Anura", 
  "Diplostraca"), Family = c("Cyclopidae", "Chydoridae", "Chydoridae", 
  "Leiopelmatidae", "Bosminidae")), .Names = c("Species", "Intercept", 
  "B", "Bconf", "Order", "Family"), row.names = c(NA, 5L), class = "data.frame")

dat

# A ggplot object with NO data.  Omit the order from the facet_grid call
g <- 
  ggplot() +
  aes(Species, B) +
  geom_point() +
  facet_grid(. ~ Family,
             scales = "free", space = "free") +
  ylim(range(dat$B)) +
  xlab("")

# Build a seperate graphic for each Order and title
plots <-
  lapply(unique(dat$Order), function(o) {
           g %+% dplyr::filter_(dat, ~ Order == o) + ggtitle(o)
             })

# build as Grobs and plot via gridExtra::grid.arrange
plots %>%
  lapply(ggplotGrob) %>%
  arrangeGrob(grobs = .) %>%
  grid.arrange(., ncol = 1)

enter image description here


0
这里有一个简单的解决方案:在您的数据中添加一个变量foo,将内部因子的级别合并,使得interaction(foo, outer)具有与inner相同的级别集。我知道我漏掉了一些标签,如果有人能够找到快速填充标签的方法,我会编辑到我的答案中。
library(ggplot2)
library(gridExtra)
library(dplyr)

dat <-
  structure(list(Species = c("Acanthocyclops robustus", "Acroperus harpae", 
                             "Alona affinis", "Ascaphus truei", "Bosmina longirostris"), 
                 Intercept = c(-36.1182388331068, -27.2140776216155, -25.7920464721491, 
                               -39.2233884219763, -31.4301301084581), 
                 B = c(0.919397836908493, 0.716601987210452, 0.685455190113372, 
                          1.04159758611351, 0.81077051300147), 
                 Bconf = c(0.407917065756464, 
                           0.181611850119198, 0.254101713856315, 0.708582768458448, 0.234313394549538
                          ), 
                 Order = c("Cyclopoida", "Diplostraca", "Diplostraca", "Anura", 
                           "Diplostraca"), 
                 Family = c("Cyclopidae", "Chydoridae", "Chydoridae", 
                            "Leiopelmatidae", "Bosminidae")), 
            .Names = c("Species", "Intercept", 
                       "B", "Bconf", "Order", "Family"), row.names = c(NA, 5L), class = "data.frame")

replace_with_int_rank = function (x) as.numeric(as.factor(x))
collapse_nested_factor = function( inner, outer ){
  ave(as.character(inner), outer, FUN = replace_with_int_rank )
}
dat$Family_collapsed = collapse_nested_factor(inner = dat$Family, dat$Order)
p <- ggplot(dat) + geom_point(aes(B,Species)) + facet_grid(Order~Family_collapsed, scales = "free")

Nested ggplot faceting


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接