我想在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 = "")
没有人知道如何获得与链接示例相同的效果,但是在使用因子级别分组时?
这是您想要的吗?
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 =“在此处输入图像描述”>