Draw only selected few faces in facet_grid

I was looking for a build method using facet_gridin ggplot2that displays only a few selected faces. let's say I have the following plot:

enter image description here

Looking for a quick way, for example, just draw faces 1 and 3.

#data
y<-1:12
x<-c(1,2,3,1,2,3,1,2,3,1,2,3)
z<-c("a","a","a","b","b","b","a","a","a","b","b","b")
df<-as.data.frame(cbind(x,y,z))

#plot

a <- ggplot(df, aes(x = z, y = y,
  fill = z))
b <- a + geom_bar(stat = "identity", position = "dodge")
c <- b + facet_grid(. ~ x, scale = "free_y")
c

Obviously, I figured out how to chop off my data first, but this, of course, can be highlighted in ggplot2. Even a greeting would be very helpful.

+5
source share
2 answers

Use subsetin your call ggplot.

plot_1 = ggplot(subset(df, x %in% c(1, 2)), aes(x=z, y=y, fill=z)) +
         geom_bar(stat = "identity", position = "dodge") +
         facet_grid(. ~ x, scale = "free_y")

enter image description here

+8
source

Would it be alright

a <- ggplot(subset(df, x != 2), aes(x = z, y = y, fill = z))
b <- a + geom_bar(stat = "identity", position = "dodge")
c <- b + facet_grid(. ~ x, scale = "free_y")
c
+2
source

All Articles