完全删除 facet_wrap 标签 [英] Remove facet_wrap labels completely
问题描述
我想完全删除刻面的标签以创建一种迷你图效果,至于对观众而言标签无关紧要,我能想到的最好的方法是:>
I'd like to remove the labels for the facets completely to create a sort of sparkline effect, as for the audience the labels are irrelevant, the best I can come up with is:
library(MASS)
library(ggplot2)
qplot(week,y,data=bacteria,group=ID, geom=c('point','line'), xlab='', ylab='') +
facet_wrap(~ID) +
theme(strip.text.x = element_text(size=0))
那么我可以完全去掉(现在是空白的)strip.background 以便为迷你图"留出更多空间吗?
So can I get rid of the (now blank) strip.background completely to allow more space for the "sparklines"?
或者,对于像这样的大量二进制值时间序列,是否有更好的方法来获得这种迷你图"效果?
Or alternatively is there a better way to get this "sparkline" effect for a large number of binary valued time-series like this?
推荐答案
对于 ggplot v2.1.0 或更高版本,使用 element_blank()
删除不需要的元素:
For ggplot v2.1.0 or higher, use element_blank()
to remove unwanted elements:
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
.
In this case, the element you're trying to remove is called strip
.
在旧版本的 ggplot
(v2.1.0 之前)中,条形文本占据 gtable 布局中的行.
In older versions of ggplot
(before v2.1.0), the strip text occupies rows in the gtable layout.
element_blank
删除文本和背景,但不会删除该行占用的空间.
element_blank
removes the text and the background, but it does not remove the space that the row occupied.
此代码从布局中删除这些行:
This code removes those rows from the layout:
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)
这篇关于完全删除 facet_wrap 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!