如何在facet_grid中增加ggplot箱线图的内边距?

3

我正在设置箱线图以在ggplot2的facet_grid中呈现,并且我想增加内部边距。

不幸的是,我无法增加到facet框架的距离。

enter image description here

我应该如何增加内部边距(左侧和右侧),如蓝色箭头所示?

enter image description here

require(ggplot2)
dat <- rbind(data.frame(approach=1,product=1,value=seq(1,20,0.5)), 
             data.frame(approach=1,product=2,value=seq(5,15,0.3)), 
             data.frame(approach=1,product=3,value=seq(5,17,0.2)), 
             data.frame(approach=2,product=1,value=seq(1,13,0.3)), 
             data.frame(approach=2,product=2,value=seq(3,18,0.5)), 
             data.frame(approach=2,product=3,value=seq(4,25,0.7)), 
             data.frame(approach=3,product=1,value=seq(1,15,0.6)), 
             data.frame(approach=3,product=2,value=seq(3,16,0.5)), 
             data.frame(approach=3,product=3,value=seq(1,10,0.1)))

gg1 <- ggplot(dat, aes(group =product, y = value)) + 
  geom_boxplot() + 
  ylab("size (cm)")+ 
  theme(panel.spacing = unit(0.1, 'lines')) + 
  theme(plot.background = element_rect(fill ="lightgrey" )) +
  scale_fill_grey(start = 0.0, end = 1) +
  theme_bw()+ 
  xlab("") +   
  facet_grid(cols=vars(approach)) +
  theme(axis.text.x = element_text(colour="black")) + 
  theme(axis.text.y=element_text(colour="black"))+ 
  theme(panel.spacing=unit(0,"lines")) + 
  guides(fill=guide_legend(title="Products")) + 
  theme(plot.background = element_rect(fill ="lightgrey" ))

gg1

此外,对于离散刻度,它会如何工作?
require(ggplot2)
dat <- rbind(data.frame(approach=1,product=1,value=seq(1,20,0.5)), 
             data.frame(approach=1,product=2,value=seq(5,15,0.3)), 
             data.frame(approach=1,product=3,value=seq(5,17,0.2)), 
             data.frame(approach=2,product=1,value=seq(1,13,0.3)), 
             data.frame(approach=2,product=2,value=seq(3,18,0.5)), 
             data.frame(approach=2,product=3,value=seq(4,25,0.7)), 
             data.frame(approach=3,product=1,value=seq(1,15,0.6)), 
             data.frame(approach=3,product=2,value=seq(3,16,0.5)), 
             data.frame(approach=3,product=3,value=seq(1,10,0.1)))

dat$product<-as.factor(dat$product)

gg1<-ggplot(dat, aes(x =product, y = value)) +
  geom_boxplot() + 
  ylab("size (cm)")+ 
  theme(panel.spacing = unit(0.1, 'lines')) +
  theme(plot.background = element_rect(fill ="lightgrey" )) +
  scale_fill_grey(start = 0.0, end = 1) +  
  theme_bw()+ xlab("") + 
  facet_grid(cols=vars(approach)) +
  theme(axis.text.x = element_text(colour="black")) +
  theme(axis.text.y=element_text(colour="black"))+ 
  theme(panel.spacing=unit(0,"lines")) + 
  guides(fill=guide_legend(title="Products")) + 
  theme(plot.background = element_rect(fill ="lightgrey" ))

gg1

在你的ggplot命令中添加xlim(c(-1,1)) - J_F
1个回答

6
你正在查看的部分是由刻度控制的,而不是由维度或主题边距控制。
以下两种方法都可以使用。在这种情况下,它们的结果非常相似,因为你的x值范围在(-1,1)附近。更一般地说,请查看?expand_scale帮助文件,以了解乘法扩展因子和加法扩展因子的示例。
gg1 + scale_x_continuous(expand = c(0.2, 0)) # expand scales by a multiple of 20%

gg1 + scale_x_continuous(expand = c(0, 0.2)) # expand scales by an addition of 0.2

illustration


非常感谢您的建议。通过扩大比例尺来解决问题是个好方法!但是,当产品数据(x轴)为因子时,我们该如何使用离散比例尺进行设置呢? - Niels
1
expand参数可用于数值和离散轴。您可以使用scale_x_discrete(expand = <something>)代替。 - Z.Lin
谢谢您的好建议!我也尝试过使用position_dodge定位盒形图,当x轴为连续变量时可行,但是当x轴为离散变量时,position_dodge就不起作用了。 如果您有关于这个问题的建议,那就太好了。 - Niels

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接