Nested facet plot with ggplot2

后端 未结 2 1182
感情败类
感情败类 2020-12-18 02:53

If I have a nested factor, in this case I have multiple \"Family\" levels that are contained in the factor \"Order\", I would like to potentially create a

         


        
相关标签:
2条回答
  • 2020-12-18 03:40

    Here's a simple solution: add a variable foo to your data that collapses the levels of the inner factor such that interaction(foo, outer) has the same level sets as inner. I know I'm missing some labels, so if someone can figure out a quick way to fill in the labels I'll edit it into my answer.

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

    0 讨论(0)
  • 2020-12-18 03:44

    Using facet_grid or facet_wrap will not build the graphic you are trying to build. You can, however, build a list of graphics and then plot them via gridExtra::grid.arrange. Here is an example

    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)
    

    0 讨论(0)
提交回复
热议问题