r - 在 ggplot2 (facet_grid) 中的特定方面之间添加空间

标签 r ggplot2

我已经能够在所有方面( Alter just horizontal spacing between facets (ggplot2) )之间添加垂直空间,但无法添加 在指定的刻面之间只添加一个空格?

这是一个基于我的真实数据的示例(在真实图中我有堆叠条):

mydf<-data.frame(year = rep(c(2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017),times = 2),
             Area = rep(c('here','there'),times = 12),
             yearArea = rep(c('here.2016','here.2017', 'there.2016','there.2017'), times = 12),
             treatment = rep(c('control','control','control','treat', 'treat','treat'), times = 4),
             response = rep(c('a','b','c','d'), times = 6),
             count = rep(c(23,15,30,20), times = 6))
mycolour<-c("#999999", "#0072B2", "#009E73","#000000")

返回图:
#default facet spacing 
example<-ggplot(data=mydf, aes(x=treatment, y=count, fill=response)) + 
  geom_bar(stat="identity", width = 0.5) +  
  scale_fill_manual(values = mycolour, name = "Response") + 
  labs (y = "Count") +
  facet_grid(~yearArea) + 
  theme_bw()
example

#spacing between each facet
spacedex<-example + theme(panel.spacing.x=unit(2, "lines"))

spacedex

如何将空间的增加限制为仅在第二个和第三个方面之间? (在这里.2017 和那里.2016 之间)

最佳答案

library(grid)
gt = ggplot_gtable(ggplot_build(example))
gt$widths[7] = 4*gt$widths[7]
grid.draw(gt)

enter image description here

关于r - 在 ggplot2 (facet_grid) 中的特定方面之间添加空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49123019/

相关文章:

r - 在 R ggplot 图例中混合颜色、填充和线型

r - 为最小-最大范围创建 geom_ribbon

r - 使用 RSelenium 在 R 中抓取 Javascript

r - 将文本标签添加到 tmap 图

r - 如何在给定最大间隙参数的情况下用零替换连续的 NA(在 R 中)

r - 使用ggplot2中的scale_fill_brewer()函数设置数据断点

r - 在ggplot2图例中组合线型和颜色

r - 按角度投影 - 结果具有随机顺序

r - 为 R 中的同一变量绘制两个不同的 x Axis

r - 使用 ggplot2 连接嵌套数据组中图形上的点