完全删除facet_wrap标签 [英] Remove facet_wrap labels completely

查看:362
本文介绍了完全删除facet_wrap标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望完全删除facet的标签以创建一种 sparkline 效果,因为对于观众来说标签是不相关的,我能想到的最好的是: (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 )

所以我可以完全去掉(现在空白的)strip.background为sparklines留出更多空间吗?



或者还有一种更好的方法可以让大量的 sparkline 效果二进制值时间序列像这样?

解决方案

对于ggplot v2.1.0或更高版本,使用 element_blank )删除不需要的元素:

  library(MASS)#获取数据
库(ggplot2)

qplot(
周,
y,
data = bacteria,
group = ID,
geom = c('point','line'),
xlab ='',
ylab =''
)+
facet_wrap(〜ID)+
主题(
strip.background = element_blank(),
strip.text.x = element_blank()

在这种情况下,您尝试删除的元素称为 strip








另一种使用ggplot grob layout



在旧版本的 ggplot



element_blank 删除文本和背景,但它不会删除该行占用的空间。



此代码从布局中删除这些行:

  library(ggplot2)
图书馆(网格)

p < - qplot(
周,
y,
数据=细菌,
组= ID,
几何= c('point','line'),
xlab ='',
ylab =''
)+
facet_wrap(〜ID)

#获取ggplot grob
gt< - ggplotGrob(p)

#找到绘图面板的顶部
面板< - grep(panel,gt $布局$名称)$ b $顶部< - 独特(GT $布局$ t [面板])

#删除图表面板上方的行
gt = gt [ - (top-1),]

#绘制
grid.newpage()
grid.draw(gt)


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))

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?

解决方案

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()
)

In this case, the element you're trying to remove is called strip.


Alternative using ggplot grob layout

In older versions of ggplot (before v2.1.0), the strip text occupies rows in the gtable layout.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆