簡體   English   中英

在ggplot2(facet_grid)中的特定構面之間添加空格

[英]Add space between specific facets in ggplot2 (facet_grid)

我已經能夠在所有方面之間添加垂直空間( 改變方面之間的水平間距(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)

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM