【问题标题】:Change order margin is presented in facet_grid ggplot2?变更单边距显示在 facet_grid ggplot2 中?
【发布时间】:2020-10-12 20:28:45
【问题描述】:

我已经看到了几个关于如何通过重新排序因子级别来更改构面顺序的答案。但我想更改显示边距的顺序。所以基本上在468之前的左侧面板上显示了(all)。提前致谢!

library(ggplot2)
qplot(mpg, wt, data=mtcars) + facet_grid(. ~ cyl, margins=TRUE)

【问题讨论】:

    标签: r ggplot2 facet-grid


    【解决方案1】:

    怎么样

    mtcars2 <- rbind(mtcars, within(mtcars, cyl <- "(All)"))
    
    qplot(mpg, wt, data = mtcars2) + facet_grid(. ~ cyl)
    

    【讨论】:

      【解决方案2】:

      我有一个非常复杂的解决方案,你总是可以编辑你的 grobs 结构:

      library(ggplot2)
      
      gg = qplot(mpg, wt, data=mtcars) + facet_grid(. ~ cyl, margins=TRUE)
      
      ggg = ggplotGrob(gg)
      
      ggg$grobs = ggg$grobs[c(1,5,2:4,9,6:8,10:15,19,16:18,20:27)]
      
      grid::grid.newpage()
      
      grid::grid.draw(ggg)
      

      【讨论】:

      • 是的,可以在 ggplot 中重新排列 grobs,但这应该被视为最后的手段,因为很难概括解决方案以适应数据的变化,并且涉及相当多的移动数据大约。但是 +1 用于展示替代方案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      相关资源
      最近更新 更多