完全删除facet_wrap标签

标签 r graphics ggplot2

我想完全删除构面的标签,以创建一种迷你图效果,对于观众来说,标签无关紧要,我能想到的最好的办法是:

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,为“迷你图”留出更多空间吗?

或者是否有更好的方法来为像这样的大量二进制值时间序列获得这种“迷你图”效果?

最佳答案

对于 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

ggplot2 figure without panel titles

<小时/>

使用 ggplot grob 布局的替代方案

在旧版本的ggplot(v2.1.0之前)中, strip 文本占据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)

关于完全删除facet_wrap标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10547487/

相关文章:

r - 基于 R 中的现有列创建新列

c# - 在不清除原始图片的情况下清除图片框上绘制的图形

3d - 无退化边缘的凹多边形线裁剪

android - 在android中绘制和动画

r - ggplot facet_wrap 中的 Unicode(en_US.UTF-8 语言环境)

r - 在 ggplot 中绘制二项式 GLMER 的随机效应

r - ggplot 图例中的虚线

javascript - 使用 googleVis 和 R 的基于订阅的网站的 Web 开发建议

algorithm - R kmeans 初始化

r - 在不引用完整路径的情况下访问工作目录之外的文件