分面网格(分页)条形图中数据点的数量可变:不要拉伸并留下间隙

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

我有以下结构的数据:

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"))

但是更多的国家年(这就是为什么使用 facet_gridfacet_wrap 似乎不现实),我想用这个代码在堆叠条形图中绘制:

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")

这工作正常,除了一些问题(忽略丑陋的 x 轴标签,已经解决):

可以使用不同的年份和不同的年数(例如,AUT 只有一年 - 2013 年,BEL 有两年 - 只有 1999 年和 2003 年,等等)。这导致 AUT 的数据在宽度上被拉长,而 BEL 的数据有间隙。我希望数据显示为

有人可以为此指出正确的方向吗?

如果您将刻面比例和 space 设置为 "free_x",您将获得相对大小。要删除空年份,您应该将其转换为因子变量。因为天平是免费的,所以这会删除面板中未使用的级别。

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)