variable number of data points in facet grid (paginated) bar plot: don't stretch and leave gaps

137 Views Asked by At

I have data with the following structure:

data <- structure(list(country_code_reordered = c("AUS", "AUS", "AUS", 
    "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
    "AUS", "AUS", "AUS", "AUT", "AUT", "AUT", "AUT", "AUT", "BEL", 
    "BEL", "BEL", "BEL", "BEL", "BEL", "BEL", "BEL", "BEL", "BEL", 
    "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", 
    "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", 
    "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "NZL", "NZL", 
    "NZL", "NZL", "NZL", "NZL", "NZL", "NZL", "NZL", "NZL", "NZL", 
    "NZL", "NZL", "NZL", "NZL", "NZL", "NZL", "NZL", "NZL", "NZL", 
    "NZL", "NZL", "NZL", "NZL", "NZL"), year = c(2004, 2004, 2004, 
    2004, 2004, 2007, 2007, 2007, 2007, 2007, 2013, 2013, 2013, 2013, 
    2013, 2013, 2013, 2013, 2013, 2013, 1999, 1999, 1999, 1999, 1999, 
    2003, 2003, 2003, 2003, 2003, 1997, 1997, 1997, 1997, 1997, 2004, 
    2004, 2004, 2004, 2004, 2008, 2008, 2008, 2008, 2008, 2011, 2011, 
    2011, 2011, 2011, 2015, 2015, 2015, 2015, 2015, 1996, 1996, 1996, 
    1996, 1996, 2002, 2002, 2002, 2002, 2002, 2008, 2008, 2008, 2008, 
    2008, 2011, 2011, 2011, 2011, 2011, 2014, 2014, 2014, 2014, 2014
    ), variable = c("apples", "bananas", "pears", "kiwi", "Other", 
    "apples", "bananas", "pears", "kiwi", "Other", "apples", "bananas", 
    "pears", "kiwi", "Other", "apples", "bananas", "pears", "kiwi", 
    "Other", "apples", "bananas", "pears", "kiwi", "Other", "apples", 
    "bananas", "pears", "kiwi", "Other", "apples", "bananas", "pears", 
    "kiwi", "Other", "apples", "bananas", "pears", "kiwi", "Other", 
    "apples", "bananas", "pears", "kiwi", "Other", "apples", "bananas", 
    "pears", "kiwi", "Other", "apples", "bananas", "pears", "kiwi", 
    "Other", "apples", "bananas", "pears", "kiwi", "Other", "apples", 
    "bananas", "pears", "kiwi", "Other", "apples", "bananas", "pears", 
    "kiwi", "Other", "apples", "bananas", "pears", "kiwi", "Other", 
    "apples", "bananas", "pears", "kiwi", "Other"), value = c(0.210602311624262, 
    0, 0, 0.193163140707949, 0.596234547667789, 0.17398206734332, 
    0, 0, 0.217457043156019, 0.608560889500661, 0.23261274015293, 
    0.0992685513509725, 0, 0.151373051792519, 0.516745656703578, 
    0.0712474108989224, 0.189258040518201, 0, 0.0383721309596656, 
    0.701122417623211, 0.109226970952933, 0, 0, 0.105200617726645, 
    0.785572411320422, 0.0532023348024211, 0.218013168445272, 0, 
    0.0584327223054532, 0.670351774446853, 0.178736197223, 0, 0, 
    0.00601818929193635, 0.815245613485064, 0.0362174844236991, 0.249671873501215, 
    0, 0.104923472200597, 0.609187169874489, 0.13514989266512, 0, 
    0, 0.191034506260092, 0.673815601074788, 0.0478154265820596, 
    0, 0, 0.191713466865929, 0.760471106552011, 0.0521122020472708, 
    0, 0, 0.25138957510272, 0.696498222850009, 0.188520116312847, 
    0.0904901629697208, 0, 0.189079585384056, 0.531910135333376, 
    0.0651479433250636, 0.0840392490970888, 0.110277852460391, 0.106226799045419, 
    0.634308156072038, 0.183960204165918, 0, 0, 0.142414543504934, 
    0.673625252329148, 0.0890430949235527, 0, 0, 0.239622573605545, 
    0.671334331470902, 0.101627964301596, 0, 0, 0.200552566638327, 
    0.697819469060078)), row.names = c(NA, 
    -80L), class = c("tbl_df", "tbl", "data.frame"))

but many more country-years (which is why using facet_grid and facet_wrap seem not realistic), which I would like to plot in stacked bar plots with this code:

ggplot(d, aes(fill=variable, y=value, x=year)) + 
    scale_fill_brewer(name = "Fruits", labels = c("pears", "Other", "kiwi", "bananas", "apples"), guide = guide_legend(reverse = T))+
    geom_bar(position = "fill", stat="identity") +
    labs(x = "Country code", y = "Contribution", title = NULL) + 
    ggforce::facet_wrap_paginate(~country_code_reordered, nrow = 1, ncol = 4, page = 1, scales = "free_x")

This works fine, except some issues (ignore the ugly x-axis labelling, that's taken care of):

sample stacked bar plot

Different years and different amounts of years are available (e.g. AUT only has one year - 2013, BEL has two - 1999 and 2003 only, etc.). This leads to the data for AUT being stretched in width, while BEL has a gap. I would like the data to be displayed such that

  • bar width remains constant (i.e. one year has the same width in e.g. AUS and AUT)
  • facet widths vary with the number of years
  • no gaps for 'empty years' remain (i.e. BEL has no empty x-axis entries for the years 2000, 2001, 2002).

Can someone point me to the right direction for this?

1

There are 1 best solutions below

3
On BEST ANSWER

If you set the facet scales and space to "free_x", you'll get the relative sizes. To drop empty years, you should convert it to a factor variable. Because the scales are free, this drops unused levels in a panel.

library(ggplot2)
library(ggforce)

ggplot(data, aes(x = as.factor(year), y = value, fill = variable)) +
  geom_col(position = "fill") +
  facet_grid_paginate(~ country_code_reordered, 
                      scales = "free_x", space = "free_x",
                      nrow = 1, ncol = 4, page = 1)

enter image description here