开发者

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} 开发者_如何学Cfrom 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.

Removing Unused Factors from a Facet in ggplot2


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

Removing Unused Factors from a Facet in ggplot2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜