Extract number of rows from faceted ggplot

前端 未结 3 608
猫巷女王i
猫巷女王i 2021-01-22 06:15

Consider a faceted ggplot

plotdf <- data.frame(x = 1:21, 
                     y = 3*(1:21)+4, 
                     z = c(rep(1,3), rep(2,3), rep(3,3), rep(4         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-22 07:03

    Following up on @Aditya's answer, this is (as of ggplot 3.1.1):

    library(magrittr)
    gg_facet_nrow_ng <- function(p){
     assertive.types::assert_is_any_of(p, 'ggplot')
     p %>%
       ggplot2::ggplot_build() %>%
       magrittr::extract2('layout') %>% 
       magrittr::extract2('layout') %>%
       magrittr::extract2('ROW') %>%
       unique() %>%
       length()
    }
    

提交回复
热议问题