geom_tile and facet_grid/facet_wrap for same height of tiles

前端 未结 2 1083
夕颜
夕颜 2020-12-01 18:24

Using ggplot, I would like represent a graph tile with panel, but with same height tile for each panel. I have this graph :

dataSta <- list(sites=rep(pas         


        
相关标签:
2条回答
  • 2020-12-01 19:10

    You can look at what ggplot contains before plotting, and rescale the panels accordingly.

    g <- ggplot_build(p) 
    ## find out how many y-breaks are in each panel
    ## to infer the number of tiles
    vtiles <- sapply(lapply(g$panel$ranges, "[[", "y.major"), length)
    
    ## convert the plot to a gtable object 
    gt <- ggplot_gtable(g)
    ## find out which items in the layout correspond to the panels
    ## we refer to the "t" (top) index of the layout
    panels <- gt$layout$t[grepl("panel", gt$layout$name)]
    ## replace the default panel heights (1null) with relative sizes 
    ## null units scale relative to each other, so we scale with the number of tiles
    gt$heights[panels] <-lapply(vtiles, unit, "null")
    ## draw on a clean slate
    library(grid)
    grid.newpage()
    grid.draw(gt)
    

    enter image description here

    0 讨论(0)
  • 2020-12-01 19:19

    It took me some time to find easier solution which is actually part of the facet_grid where you can set the space = "free_y". More info at recent question.

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