2012-11-30 42 views
5

如果我有一個嵌套的因素,在這種情況下,我必須包含在因素「訂單」多「家庭」的水平,我想有可能創建一個嵌套小情節

facet_grid(Family/Order ~.) 

代替目前

facet_grid(Family + Order ~.) 

的根本 - 一條帶爲每一筆訂單 - 包含在它旁邊的所有條爲每個家庭該命令內。我知道facet_grid(Family/Order〜。)目前是不可能的,但我將如何實現這一效果?可以用主題()完成嗎?非常感謝。 --SB

我應該在上面指出家庭和訂單都是因素。數據值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

+0

我上面發佈了更多細節。謝謝。 – user1536207

+2

請提供一個示例數據集。沒有關於數據結構的一些信息,不可能回答你的問題。 –

回答

0

使用facet_gridfacet_wrap不會建立你正在試圖建立的圖形。但是,您可以創建圖形列表,然後通過gridEtrax::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