如何增加facet_grid中ggplot箱线图的内部边距? [英] How to increase the inner margin of a ggplot boxplot in facet_grid?

查看:255
本文介绍了如何增加facet_grid中ggplot箱线图的内部边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am setting up boxplots to present these in a ggplot2 facet_grid and I would like to increase the inner margin.

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

Unfortunately, I am not able to increase the distance to the frame of the facet.

如何如蓝色箭头所示增加内部页边距(左右)?

How could I increase the inner margin (left and right) as indicated by the blue arrows?

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

此外,它在离散范围内如何工作?

Also, how would it work for a discrete scale?

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

推荐答案

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

The part you are looking at is controlled from scales, not facets or theme margins.

以下任何一种都可以.在这种情况下,它们的结果类似,因为您的x值范围在(-1, 1)附近.通常,请查找?expand_scale的帮助文件,以获取乘性与加性扩展因子的示例

Either of the following would work. Their results are similar in this case, since your x-values' range is in the neighbourhood of (-1, 1). More generally, look up the help file for ?expand_scale for examples of multiplicative vs. additive expansion factors

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

这篇关于如何增加facet_grid中ggplot箱线图的内部边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆