R 如何在ggplot中控制镶嵌面_包裹中的垂直空间

R 如何在ggplot中控制镶嵌面_包裹中的垂直空间,r,ggplot2,stacked-chart,facet-wrap,R,Ggplot2,Stacked Chart,Facet Wrap,我已经创建了一个横跨两个维度评级和地理(Geo_类)的刻面图。如何在不同的地理类(panel.spating.x)之间引入空格,同时避免在评级类之间引入空格。这里是示例数据 在下图中,1至3、4、5、6、7代表评级,Geo_等级为(沙特阿拉伯、非GCC、其他GCC和所有)。方法是新的或旧的。 Im使用以下代码生成绘图 p<-ggplot(Final_impact_melt_All5, aes(x=Method, y=Capital_Charge, fill= Capital_Charge_

我已经创建了一个横跨两个维度评级和地理(Geo_类)的刻面图。如何在不同的地理类(panel.spating.x)之间引入空格,同时避免在评级类之间引入空格。这里是示例数据

在下图中,1至3、4、5、6、7代表评级,Geo_等级为(沙特阿拉伯、非GCC、其他GCC和所有)。方法是新的或旧的。 Im使用以下代码生成绘图

p<-ggplot(Final_impact_melt_All5, aes(x=Method, y=Capital_Charge, fill= Capital_Charge_type))+ geom_bar(stat='Identity', width= 1)
p + facet_wrap (Geo_class ~ Ratings, nrow = 2) + scale_fill_brewer(palette ="Oranges") + theme(axis.text=element_text(size=6),panel.spacing.x=unit(0, "lines"),panel.spacing.y=unit(1, "lines"))

p这就是你想要的吗?据我所知,你所要求的不能只用
ggplot
来完成。下面的代码并不漂亮。它取决于
gtable
grid
功能。它分解ggplot中的条带,然后以适当的间距构造新条带。该代码适用于示例中内部和外部条带标签的特定配置。如果这种情况发生变化,代码就会中断。而且它可能无法在下一版本的ggplot中存活

library(ggplot2)
library(grid)
library(gtable)

# Set spacing between Geo_class and between Ratings
OuterSpacing = unit(5, "pt") # spacing between Geo_class
InnerSpacing = unit(0, "pt") # spacing between Ratings

# Your ggplot
p <- ggplot(Final_impact_melt_All5, 
           aes(x=Method, y=Capital_Charge, fill= Capital_Charge_type)) + 
    geom_bar(stat='Identity', width= 1)
plot = p + 
   facet_wrap (Geo_class ~ Ratings, nrow = 2) + 
    scale_fill_brewer(palette ="Oranges") + 
    theme(axis.text=element_text(size=6), 
          panel.spacing.x = OuterSpacing,
          panel.spacing.y = unit(1, "lines"))


# Get the ggplot grob
g = ggplotGrob(plot)

# Set spacing between 'Ratings' to 'InnerSpacing'
g$widths[c(seq(6, by=4, length.out=4), seq(26, by=4, length.out=4)) ] = InnerSpacing

# Get a list of strips
strip = lapply(grep("strip-t", g$layout$name), function(x) {g$grobs[[x]]})

# Number of strips
URow = 4;  LRow = 5   # Top row and Bottom row

# Construct gtable to contain the new strip
Inner = (rep(unit.c(unit(1, "null"), InnerSpacing), LRow))[-10]

newStrip  = gtable(widths = (rep(unit.c(Inner, OuterSpacing), URow))[-40], 
                   heights = strip[[1]]$heights)

## Populate the gtable 
# Top Row
cols1 = seq(1, by = 5, length.out = 4)
cols2 = (seq(1, by = 10, length.out = 4))
newStrip = gtable_add_grob(newStrip,  lapply(strip[cols1], `[`, 1), t = 1, l = cols2, r = cols2 + 8)

# Bottom row
cols = seq(1, by = 2, length.out = 20)
newStrip = gtable_add_grob(newStrip, lapply(strip, `[`, 2), t = 2, l = cols)

## Add the strips to the plot, 
# making sure the second half go in the upper section (t=6)
# and the first half go in the lower section (t=11)
pgNew = gtable_add_grob(g, newStrip[1:2, 21:39], t = 6, l = 4, r = 40)
pgNew = gtable_add_grob(pgNew, newStrip[1:2, 1:19], t = 11, l = 4, r = 40)

# Remove the original strip
for(i in 102:121) pgNew$grobs[[i]] = nullGrob()


# Draw the plot
grid.newpage()
grid.draw(pgNew)
库(ggplot2)
图书馆(网格)
图书馆(gtable)
#设置Geo_类和额定值之间的间距
外部空间=单位(5,“pt”)#地理等级之间的间距
内部间距=单位(0,“pt”)#额定值之间的间距
#你的阴谋

p这看起来怎么样:
ggplot(最终影响熔化全部5,aes(x=评级,y=资本费用,填充=资本费用类型,颜色=方法))+geom_条(stat='Identity',width=1,position='dodge')+刻面包裹(~Geo_class,nrow=2)+刻度填充酿酒器(调色板=“橙子”)+刻度颜色手册(值=c(“黑色”、“白色”)+主题(axis.text=元素文本)(尺寸=6),面板间距.x=单位(1,“线”),面板间距.y=单位(1,“线”))
感谢Brian拍摄这张照片。但是我需要堆叠这些条。我已经发布了一张图片,上面显示了代码的外观,评论部分似乎没有图片上传选项。哦,嗯,我刚刚意识到,避开这些条线会将其取消堆叠。很抱歉。尝试这样做:。原理是一样的,你想减少e您的并排分组是沿着X轴的,而with-a-space分组是您的方面。我看了一看基于方面的解决方案。然而,虽然示例中仅沿一个维度(名称)刻面,但我的是沿两个维度刻面(评级和地理等级)。虽然我需要沿评级的间距为零,但我需要沿Geo_类的间距。我可以有4个刻面行来解决此问题,但我确实需要2*2网格中的刻面行。请在此处查找示例数据哇..这正是我所需要的,但知道它不是ggplot功能令人沮丧。感谢您在这方面的出色努力。