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
415 views
in Technique[技术] by (71.8m points)

r - Removing Unused Factors from a Facet in ggplot2

I am trying to figure out a neat way to remove unused factors from a facet in ggplot2. Here is a minimal example

# DUMMY DATA
mydf = data.frame(
  x = rpois(6, 25),
  y = LETTERS[1:6],
  cat = c(rep('AA', 3), rep('BB', 3)))

# PLOT IT!
p0 = ggplot(mydf, aes(x = x, y = y)) +
  geom_point() +
  facet_wrap(~ cat, ncol = 1)

From the plot below, you can see that factors D, E and F are plotted in facet AA despite the fact that there is no corresponding data. What I want is for a way to eliminate {D, E, F} from facet AA and similarly {A, B, C} from facet BB.

Is there a neat way to do this, or even a hack would be acceptable.

enter image description here

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I think all you need is scales = "free_y":

p0 = ggplot(mydf, aes(x = x, y = y)) +
  geom_point() +
  facet_wrap(~ cat, ncol = 1,scales = "free_y")

p0

enter image description here


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

...