ggplot facetted geom_boxplot:减少x轴类别之间的空间 [英] ggplot facetted geom_boxplot: reduce space between x-axis categories

查看:366
本文介绍了ggplot facetted geom_boxplot:减少x轴类别之间的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ggplot创建boxplot。当我减小boxplot的宽度时,x轴类别之间的间距增加。我希望能够缩小x轴类别之间的距离,并使箱型图彼此接近。

  p <-ggplot(data.plot1,aes(time2,Count))
p + geom_boxplot(outlier.shape = NA,width = 0.3)+
ggtitle()+ ylab (cell / mL))+ xlab(Time)+
theme_bw()+ coord_cartesian(ylim = c(0,850))+
geom_hline(data = normal1,aes(yintercept = val ),linetype =dashed)+
facet_grid(。〜CellType1)



因此,基本上可以缩短第0天,第30天,第100天之间的距离,并使箱型图彼此靠得更近。如评论中提到的,缩小图形设备是实现它的一种方式。另一种不改变图形设备大小的方法是在你的酒吧和面板两侧添加空格。 注意:由于您的问题不可重现,因此我使用了内置的 infert 数据集,该数据集可用于演示目的。假设这是您的原始并排盒式图:

  p <-ggplot(infert,aes(as.factor (教育),层))
p + geom_boxplot(outlier.shape = NA,width = 0.3)+
ggtitle()+ ylab(细胞计数(细胞/ mL))+ xlab (Time)+
theme_bw()+ coord_cartesian(ylim = c(0,80))+
#geom_hline(data = normal1,aes(yintercept = val),linetype =dashed) +
facet_grid(。〜induced)



scale_x_discrete(expand = c(0.8 ,0))表示放置在距轴线一定距离的乘法和加法扩展常数。请参阅?scale_x_discrete 。这有效地挤压每个面板中的箱形图,这也减小了每个箱形图的宽度。为了弥补这一点,我在 geom_boxplot 中将宽度增加为 width = 0.6 。请注意,x轴标签现在重叠。你将不得不尝试不同的膨胀系数和宽度尺寸,以得到你想要的。

另请参阅此问题以获取相关问题:删除网格中的小节之间的空间


I am creating a boxplot using ggplot. When I reduce the width of the boxplot the space between the x-axis categories increases. I would like to be able to reduce the space between the x-axis categories and bring the box plots closer to each other.

p<-ggplot(data.plot1, aes(time2, Count))
p+geom_boxplot(outlier.shape = NA, width=0.3)+
  ggtitle("")+ylab("Cell Count (cells/mL) ")+ xlab("Time")  + 
  theme_bw()+  coord_cartesian(ylim = c(0, 850))+
  geom_hline(data=normal1, aes(yintercept = val), linetype="dashed")+
  facet_grid(.~CellType1)

So, basically, reduce the space between Day 0, Day30, Day 100 and bring the boxplots closer to each other.

解决方案

As mentioned in the comments, narrowing the graphics device is one way of doing it. Another way to do it without changing the size of the graphics device is to add spaces between your bars and the sides of the panels. Note: Since your question is not reproducible, I have used the build-in infert dataset, which serves demonstrative purposes. Assuming that this is your original facetted side-by-side boxplots:

p<-ggplot(infert, aes(as.factor(education), stratum))
p+geom_boxplot(outlier.shape = NA, width=0.3)+
  ggtitle("")+ylab("Cell Count (cells/mL) ")+ xlab("Time")  + 
  theme_bw()+  coord_cartesian(ylim = c(0, 80))+
  # geom_hline(data=normal1, aes(yintercept = val), linetype="dashed")+
  facet_grid(.~induced)

This brings the categories together by adding white space on both ends of each panel:

p+geom_boxplot(outlier.shape = NA, width=0.6)+
  ggtitle("")+ylab("Cell Count (cells/mL) ")+ xlab("Time")  + 
  theme_bw()+  coord_cartesian(ylim = c(0, 80))+
  # geom_hline(data=normal1, aes(yintercept = val), linetype="dashed")+
  facet_grid(.~induced) +
  scale_x_discrete(expand=c(0.8,0))

The two numbers in scale_x_discrete(expand=c(0.8,0)) indicate the multiplicative and additive expansion constant that "places some distance away from the axes". See ?scale_x_discrete. This effectively "squishes" the boxplots in each panel together, which also reduces the width of each boxplot. To compensate for that, I increased the width to width=0.6 in geom_boxplot. Notice that the x-axis labels are now overlapping. You will have to experiment with different expansion factors and width sizes to get exactly how you want it.

Also see this question for a related issue: Remove space between bars within a grid

这篇关于ggplot facetted geom_boxplot:减少x轴类别之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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