在 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))
-
width
在geom_bar
中决定了条的宽度. -
width
在position_dodge
中决定了每个条的位置. -
width
ingeom_bar
determines the width of the bar. -
width
inposition_dodge
determines the position of each bar.
也许你在和他们玩了一段时间后很容易理解他们的行为.
Probably you can easily understand their behavior after you play with them for a while.