Ma.
Ma.

Reputation: 235

nested panels in ggplot

first, sorry for my English and mistakes. I have a plot like this:

data <- data.frame(site=rep(letters[1:6],each=3), year=rep(2001:2003, 6), nb=round(runif(18, min=20, max=60)), group=c(rep("A",9),rep("B", 6),rep("C",3)))

ggplot(data=data, aes(x= factor(year), y= nb)) + 
  geom_point() + 
  facet_wrap(~site)

And I would like to add the other panel "group". In fact I would like to make this graph without empty parts:

ggplot(data=data, aes(x= factor(year), y= nb)) + 
  geom_point() + 
  facet_grid(group~site)

Does someone has an idea? Thanks for you help!

#

There is this solution which look like that I want, but I thought there were more simple solution :

plt1 <- ggplot(data=data[data$group=="A",], aes(x= factor(year), y= nb)) + 
          geom_point() + 
          ggtitle("A")+
          facet_grid(~site)+
          xlab("") + ylab("")

plt2 <- ggplot(data=data[data$group=="B",], aes(x= factor(year), y= nb)) + 
          geom_point() + 
          ggtitle("B")+
          facet_grid(~site)+
          xlab("") + ylab("")

plt3 <- ggplot(data=data[data$group=="C",], aes(x= factor(year), y= nb)) + 
         geom_point() + 
         ggtitle("C")+
         facet_grid(~site)+
         xlab("") + ylab("")

library(gridExtra)

grid.arrange(arrangeGrob(plt1,plt2, plt3),
                         left = textGrob("nb",rot=90))

Upvotes: 4

Views: 1225

Answers (1)

Didzis Elferts
Didzis Elferts

Reputation: 98449

You can combine the site and group inside the facet_wrap() - so you will have only "full" facets.

 ggplot(data=data, aes(x= factor(year), y= nb)) + 
     geom_point() + 
     facet_wrap(~site+group)

Upvotes: 6

Related Questions