How to increase the inner margin of a ggplot boxplot in facet_grid?

我的梦境 提交于 2020-01-05 16:12:21

问题


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

回答1:


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

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



来源:https://stackoverflow.com/questions/55521130/how-to-increase-the-inner-margin-of-a-ggplot-boxplot-in-facet-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!