本文介绍了在ggplot2中的特定构面之间添加空间(facet_grid)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够在所有构面之间添加垂直空间(只是改变小平面之间的水平间距(ggplot2)),但是无法在指定的小平面之间仅添加一个间距吗?

I've been able to add vertical space between all facets (Alter just horizontal spacing between facets (ggplot2)) but haven't been able to add just one space between specified facets?

这是一个基于我的真实数据的示例(在真实情节中,我堆积了条形图):

Here's an example based on my real data (in the real plot I have stacked bars):

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

如何将空间的增加限制为仅在第二个和第三个构面之间? (在here.2017和there.2016之间)

How can I limit the addition of space to only between the second and third facet? (between here.2017 and there.2016)

推荐答案

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

这篇关于在ggplot2中的特定构面之间添加空间(facet_grid)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:52