Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

ggplot2 figure without panel titles


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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...