在ggplot2中的小节之间添加空格

问题描述 投票:31回答:1

我想在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()参数缩放它们。但是,当我在x轴上使用因子水平分组时,width()不起作用,如下例所示。

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 = "") 

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

r ggplot2
1个回答
77
投票

这是您想要的吗?

hist + geom_bar(width=0.4, position = position_dodge(width=0.5))
  • width中的[geom_bar确定条形的宽度。
  • width中的[position_dodge确定每个小节的位置。

和他们玩了一段时间后,您可能很容易理解他们的行为。

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9WSXJRSC5wbmcifQ==” alt =“在此处输入图像描述”>

© www.soinside.com 2019 - 2024. All rights reserved.