将标签放在 facet_grid 中的条带外
Placing labels outside of the strips in facet_grid
我想知道如何在 facet_grid
条带的 left
或 right
侧放置标签。
作为一个可重现的例子,我想从这个例子开始
library(ggplot2)
ggplot(mtcars, aes("", hp)) +
geom_boxplot(width=0.7, position=position_dodge(0.7)) +
theme_bw() +
facet_grid(. ~ vs + am + carb,switch = 'both',labeller = label_both) +
theme(panel.spacing=unit(0.2,"lines"),
strip.background=element_rect(color="grey30", fill="grey90"),
panel.border=element_rect(color="grey90"),
axis.ticks.x=element_blank())+
#strip.placement="outside") +
labs(x="")
我要找的剧情;
您始终可以手动将标题添加到 gtable,
g <- ggplotGrob(p)
library(gtable)
library(grid)
library(gridExtra)
pos <- subset(g$layout, grepl("strip",name))
titles <- tableGrob(c("vs","am","carb"), theme = ttheme_minimal(9))
titles$heights <- unit(rep(1,3), "null")
g$widths[ncol(g)] <- sum(titles$widths)
g <- gtable_add_grob(g, titles, t=unique(pos$t), l = ncol(g))
grid.newpage()
grid.draw(g)
我想知道如何在 facet_grid
条带的 left
或 right
侧放置标签。
作为一个可重现的例子,我想从这个例子开始
library(ggplot2)
ggplot(mtcars, aes("", hp)) +
geom_boxplot(width=0.7, position=position_dodge(0.7)) +
theme_bw() +
facet_grid(. ~ vs + am + carb,switch = 'both',labeller = label_both) +
theme(panel.spacing=unit(0.2,"lines"),
strip.background=element_rect(color="grey30", fill="grey90"),
panel.border=element_rect(color="grey90"),
axis.ticks.x=element_blank())+
#strip.placement="outside") +
labs(x="")
我要找的剧情;
您始终可以手动将标题添加到 gtable,
g <- ggplotGrob(p)
library(gtable)
library(grid)
library(gridExtra)
pos <- subset(g$layout, grepl("strip",name))
titles <- tableGrob(c("vs","am","carb"), theme = ttheme_minimal(9))
titles$heights <- unit(rep(1,3), "null")
g$widths[ncol(g)] <- sum(titles$widths)
g <- gtable_add_grob(g, titles, t=unique(pos$t), l = ncol(g))
grid.newpage()
grid.draw(g)