【问题标题】:How to increase the inner margin of a ggplot boxplot in facet_grid?如何在 facet_grid 中增加 ggplot boxplot 的内边距?
【发布时间】:2019-04-04 16:43:11
【问题描述】:

我正在设置箱线图以将这些显示在 ggplot2 facet_grid 中,并且我想增加内边距。

很遗憾,我无法增加与刻面框架的距离。

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

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

【问题讨论】:

  • xlim(c(-1,1)) 添加到您的 ggplot 命令中。

标签: r ggplot2 margins facet-grid


【解决方案1】:

您正在查看的部分是由比例控制的,而不是分面或主题边距。

以下任何一种都可以。在这种情况下,它们的结果相似,因为您的 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

【讨论】:

  • 感谢您的精彩建议。通过扩大规模的解决方法很棒!但是,当在此代码中产品(x 轴)的数据是一个因素时,我们如何使用离散比例进行设置?
  • expand 参数可用于数值轴和离散轴。您可以改用scale_x_discrete(expand = &lt;something&gt;)
  • 感谢您的建议!我还尝试使用 position_dodge 定位 box_plot,当 x 轴是连续变量时它正在工作,但是,当 x 轴是离散变量时, position_dodge 不起作用。如果您对此问题有建议,那就太好了。
猜你喜欢
  • 1970-01-01
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-10
  • 2021-11-26
  • 1970-01-01
  • 2022-01-25
相关资源
最近更新 更多