变更单保证金显示在facet_grid ggplot2中?

问题描述

我已经看到了几个有关如何通过重新排序因子级别来改变构面顺序的答案。但是我想更改保证金的显示顺序。因此,基本上(all)468之前显示在左侧面板上。预先感谢!

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

enter image description here

解决方法

怎么样

mtcars2 <- rbind(mtcars,within(mtcars,cyl <- "(All)"))

qplot(mpg,wt,data = mtcars2) + facet_grid(. ~ cyl)

enter image description here

,

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

library(ggplot2)

gg = qplot(mpg,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)

enter image description here