如何在facet_grid中增加ggplot boxplot的内部边距?

时间:2019-04-04 16:43:12

标签: r ggplot2 margins facet-grid

我正在设置箱形图以将它们显示在ggplot2 facet_grid中,我想增加内部空白。

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

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

1 个答案:

答案 0 :(得分:0)

您要查看的部分是通过比例控制的,而不是刻面或主题边距。

以下任何一种都可以。在这种情况下,它们的结果相似,因为您的x值范围在(N, N-1)附近。更一般而言,请查找(-1, 1)的帮助文件,以获取乘法和加法扩展因子的示例

?expand_scale

illustration