在ggplot2中的条形图之间添加空格 [英] Adding space between bars in ggplot2
问题描述
library(ggplot2)
处理< - rep(c('T','C '),each = 2)
性别< - rep(c('M','F'),2)
响应< - 样本(1:100,4)
df< - data.frame(治疗,性别,反应)
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(宽度= 0.5))
-
width
geom_bar
决定栏的宽度。 -
width
在position_dodge
中确定每个柱的位置。
可能你可以轻松了解他们的行为后,你玩了一段时间。
Dear all, 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?
Cheers,
Aaron
Is this what you want?
hist + geom_bar(width=0.4, position = position_dodge(width=0.5))
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.
这篇关于在ggplot2中的条形图之间添加空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!