在 ggplot2 中的条之间添加空间 [英] Adding space between bars in ggplot2

查看:33
本文介绍了在 ggplot2 中的条之间添加空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 ggplot2 中的条形之间添加空格.此页面提供了一种解决方案:http://www.streamreader.org/stats/questions/6204/how-to-increase-the-space-between-the-bars-in-a-bar-plot-in-ggplot2.然而,该解决方案没有对 x 轴分组使用因子水平,而是创建一个数字序列 x.seq,以手动放置条形,然后使用 width() 参数缩放它们.width() 不起作用,但是,当我使用 x 轴的因子级别分组时,如下例所示.

I'd like to add spaces between bars in ggplot2. This page offers one solution: http://www.streamreader.org/stats/questions/6204/how-to-increase-the-space-between-the-bars-in-a-bar-plot-in-ggplot2. Instead of using factor levels for the x-axis groupings, however, this solution creates a numeric sequence, x.seq, to manually place the bars and then scales them using the width() argument. width() doesn't work, however, when I use factor level groupings for the x-axis as in the example, below.

library(ggplot2)

Treatment <- rep(c('T','C'),each=2)
Gender <- rep(c('M','F'),2)
Response <- sample(1:100,4)
df <- data.frame(Treatment, Gender, Response)

hist <- ggplot(df, aes(x=Gender, y=Response, fill=Treatment, stat="identity"))
hist + geom_bar(position = "dodge") + scale_y_continuous(limits = c(0, 
    100), name = "") 

有谁知道如何获得与链接示例中相同的效果,但同时使用因子级别分组?

Does anyone know how to get the same effect as in the linked example, but while using factor level groupings?

推荐答案

这是您想要的吗?

hist + geom_bar(width=0.4, position = position_dodge(width=0.5))

  • widthgeom_bar 中决定了条的宽度.
  • widthposition_dodge 中决定了每个条的位置.
    • width in geom_bar determines the width of the bar.
    • width in position_dodge determines the position of each bar.
    • 也许你在和他们玩了一段时间后很容易理解他们的行为.

      Probably you can easily understand their behavior after you play with them for a while.

      这篇关于在 ggplot2 中的条之间添加空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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