对于ggplot v2.1.0或更高版本,请使用
element_blank()
来删除不需要的元素:
library(MASS)
library(ggplot2)
qplot(
week,
y,
data = bacteria,
group = ID,
geom = c('point', 'line'),
xlab = '',
ylab = ''
) +
facet_wrap(~ ID) +
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)
在这种情况下,您要删除的元素称为
strip
。
使用ggplot grob布局的替代方案
在旧版本的
ggplot
(v2.1.0之前)中,条带文本占据gtable布局中的行。
element_blank
会删除文本和背景,但不会删除该行占用的空间。
此代码从布局中删除了这些行:
library(ggplot2)
library(grid)
p <- qplot(
week,
y,
data = bacteria,
group = ID,
geom = c('point', 'line'),
xlab = '',
ylab = ''
) +
facet_wrap(~ ID)
gt <- ggplotGrob(p)
panels <- grep("panel", gt$layout$name)
top <- unique(gt$layout$t[panels])
gt = gt[-(top-1), ]
grid.newpage()
grid.draw(gt)
strip.text.x
替换为strip.text
。 - Freddie J. Heather