0

I have a dataframe df

df<- structure(list(Categorie = structure(c(1L, 1L, 2L, 2L, 4L, 4L, 
3L, 1L, 1L, 2L, 2L, 4L, 4L, 3L, 1L, 1L, 2L, 2L, 4L, 4L, 3L, 1L, 
1L, 2L, 2L, 4L, 4L, 3L, 1L, 1L, 2L, 2L, 4L, 4L, 3L, 1L, 1L, 2L, 
2L, 4L, 4L, 3L, 1L, 1L, 2L, 2L, 4L, 4L, 3L, 1L, 1L, 2L, 2L, 4L, 
4L, 3L), .Label = c("Age classes", "Climate", "Nutrient availability", 
"PFT"), class = "factor"), Sub_categories = structure(c(7L, 4L, 
6L, 1L, 3L, 2L, 5L, 7L, 4L, 6L, 1L, 3L, 2L, 5L, 7L, 4L, 6L, 1L, 
3L, 2L, 5L, 7L, 4L, 6L, 1L, 3L, 2L, 5L, 7L, 4L, 6L, 1L, 3L, 2L, 
5L, 7L, 4L, 6L, 1L, 3L, 2L, 5L, 7L, 4L, 6L, 1L, 3L, 2L, 5L, 7L, 
4L, 6L, 1L, 3L, 2L, 5L), .Label = c("Continental", "DBF", "ENF", 
"Intermediate-Old", "Low-High", "Temperate", "Young"), class = "factor"), 
    Variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L), .Label = c("Age", "Clay content", "GPP", "GPP*Age", 
    "GPP*P", "GPP*P trend", "N availability", "SPI"), class = "factor"), 
    Importance = c(19.2, 23.7, 45.2, 45.7, 39, 66.8, 34.8, 10.9, 
    16.2, 9.2, 6.3, 15.7, 2.1, 10, 13.2, 7.1, 6.1, 3.8, 2.4, 
    7.3, 5.2, 6.4, 10, 6.6, 3.7, 2.9, 5.8, 8.4, 17.7, 0, 6.1, 
    5.2, 8.4, 2.8, 6.7, 11.8, 21.1, 9.8, 21.9, 20, 6.3, 13.5, 
    2.3, 7.6, 3.9, 1.3, 3.9, 0.4, 3.8, 10.9, 7.5, 4.5, 5.8, 0.3, 
    2.5, 9.4)), .Names = c("Categorie", "Sub_categories", "Variable", 
"Importance"), class = "data.frame", row.names = c(NA, -56L))

I want to plot my data by doing a facet_grid. with the Categorie and Sub_categories variables. I run the below command line:

library(ggplot2)

ggplot(data = var_Imp) + 
  geom_bar(mapping = aes(x = Variable, y = Importance, fill=Variable), width = 1, stat= "identity", position = "stack") + 
  coord_flip() +
  facet_grid(Categorie~Sub_categories,  scales="free", space="free", shrink=TRUE, drop=TRUE)+ 
  theme_bw(base_size = 14, base_family = "Helvetica")+
  theme(axis.ticks.length=unit(-0.25, "cm"), 
        legend.position="none", 
        legend.box="horizontal",
        legend.key = element_blank(),
        legend.text=element_text(size=14), 
        axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")), 
        axis.text.y = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")),
        axis.ticks.y=element_blank())+
  xlab("") +
  ylab("Relative contribution [%]")+ 
  scale_fill_brewer(type = "div")

However, the levels with no information in some of the facets are still plotted although they should not because there is no information in it. I thought the scales="free" and space="free" parameters will do the job but apparently not. Anyone knows how I can plot my data without the unused levels? Thanks

Simon Besnard
  • 377
  • 1
  • 6
  • 18
  • 2
    See [`?droplevels`](http://stackoverflow.com/questions/1195826/drop-factor-levels-in-a-subsetted-data-frame). – Roman Luštrik Nov 14 '16 at 12:24
  • The issue is more coming from the interaction between `categorie` and `Sub_categories` variable. In theory, all the levels are being used just some of the interactions. – Simon Besnard Nov 14 '16 at 14:45

1 Answers1

0

I don't know about a specific command in ggplot2. Two possible options:

1) Any of the options proposed in this post ggplot2: How to force the number of facets with too few plots?

2) Export the plot in .svg (or other vectorial format), open it with inkscape or any other program and delete the empty facets

Community
  • 1
  • 1