减少facet_wrap中轴标签和轴之间的区域

问题描述

我使用 facet_wrap 显示两个图,我想减少水平轴和轴上年份之间的距离,使年份更接近轴。有没有办法做到这一点?

   ggplot( df,aes(x=year,y = value)) +
  geom_area(aes(fill = variable)) +
  facet_wrap(~ scenario,ncol=3) +
  ylab("") +
  xlab("") +
  theme_bw() + 
  theme(
    panel.border = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank()) +
  theme(axis.text.x = element_text(angle = 90)) +
  theme(axis.title.y = element_text(angle = 0)) +
  theme(axis.line = element_line(),strip.background = element_blank(),panel.margin = unit(2,"lines"))

enter image description here

这是数据集:

 df <- structure(list(country = c("CAN","CAN","CAN"),year = c(2030,2030,2040,2040),scenario = structure(c(2L,2L,1L,1L),.Label = c("BAU","2Ci","2Cd"),class = "factor"),variable = structure(c(5L,5L,2L),.Label = c("ELEC","GAS","ROIL","OIL","COAL"),value = c(0.128654289458377,2.37568827939126,0.146615100987535,2.86487702956444,0.0980137869955521,1.62493493094997,0.150757647986727,2.74361420537482),unit = c("Exojoules","Exojoules","Exojoules"),sector = c("ECON","ECON","ECON")),row.names = c(NA,-8L),groups = structure(list(country = c("CAN",scenario = c("2Ci","BAU","BAU"),.rows = list(1:2,3:4,5:6,7:8)),-4L),class = c("tbl_df","tbl","data.frame")),class = c("grouped_df","tbl_df","data.frame"))

解决方法

scale_y_continuous(expand = expand_scale(mult = c(0,0.05))) +

应该使基线和数据之间没有空格,顶部的填充与默认情况相同。

enter image description here