对于ggplot v2.1.0或更高版本,使用element_blank()
删除不需要的元素:
library(MASS) # To get the data
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
.
Alternative using ggplot grob layout
在旧版本的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)
# Get the ggplot grob
gt <- ggplotGrob(p)
# Locate the tops of the plot panels
panels <- grep("panel", gt$layout$name)
top <- unique(gt$layout$t[panels])
# Remove the rows immediately above the plot panel
gt = gt[-(top-1), ]
# Draw it
grid.newpage()
grid.draw(gt)