Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot only a select few facets in facet_grid

Tags:

r

ggplot2

I was looking for a way to plot using facet_grid in ggplot2 that only displays just a few select facets. say I have the following plot:

enter image description here

Been looking for a quick way to, for instance, just plot facets 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 just chop up my data first but this must of course be possible to allocate in ggplot2 Even just a nudge would be most welcome.

like image 235
user1317221_G Avatar asked May 15 '12 20:05

user1317221_G


2 Answers

Use subset in your ggplot call.

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

like image 100
bdemarest Avatar answered Nov 10 '22 10:11

bdemarest


Would this be okay,

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
like image 3
Eric Fail Avatar answered Nov 10 '22 11:11

Eric Fail