edit strip size ggplot2

前端 未结 2 1281
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 15:39

I have tried to apply this solution (How can I change the size of the strip on facets in a ggplot?1) to change the size of the strip on facets in a ggplot, I have increase

相关标签:
2条回答
  • 2020-12-05 16:08

    A simple solution is to specify the margins of your strip.text elements appropriately:

    ggplot(mtcars, aes(x=gear)) + 
      geom_bar(aes(y=gear), stat="identity", position="dodge") +
      facet_wrap(~cyl) + 
      theme(strip.text.x = element_text(margin = margin(2,0,2,0, "cm")))
    

    0 讨论(0)
  • 2020-12-05 16:10

    Something like this seems to work:

    g$heights[[6]] <- unit(5,"in")
    g$grobs[[17]]$heights <- unit(2,"in")
    g$grobs[[18]]$heights <- unit(2,"in")
    g$grobs[[19]]$heights <- unit(2,"in")
    
    grid.newpage()
    grid.draw(g)
    

    Note that looking at g$grobs tells us grobs 17, 18 and 19 are the strips.

    You can automate the second step with:

    index <- which(sapply(g$grobs, function(x) x$name == "strip"))
    g$grobs <- lapply(seq_along(g$grobs), function(.x) {
      if(.x %in% index) {
        g$grobs[[.x]]$heights <- unit(2,"in")
      } 
      g$grobs[[.x]]
    } )
    

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