亲爱的,我想在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 = "")
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何获得与链接示例中相同的效果,但在使用因子级别分组时?
干杯,
亚伦
这是我的代码
ggplot(df, aes(x=timepoint, y=mean, fill=group)) +
geom_bar(position=position_dodge(.3), colour="black", stat="identity", width=0.3, , binwidth=0) +
geom_errorbar(position=position_dodge(.3), width=.25, aes(ymin=mean, ymax=mean+sem)) +
scale_fill_manual(values=c("#FFFFFF", "#000000"), guide=FALSE) +
theme_bw() +
ylab(ylab) +
xlab("") +
# xlim("Baseline", "12w") +
scale_x_discrete(expand = c(0,0), limits=c("Baseline","12w")) +
scale_y_continuous(expand = c(0,0) ) +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
Run Code Online (Sandbox Code Playgroud)
这是我的输出,我不想要,它在"基线"和"12w"之间的空间太大:

如何去除栏之间的空间?
谢谢
df是这样的:
df <- structure(list(group = c("a1.d.ffa.mean Dysglyc", "a1.c.ffa.mean Control",
"b1.d.ffa.mean Dysglyc", "b1.c.ffa.mean Control"), timepoint = c("Baseline",
"Baseline", "12w", "12w"), mean = …Run Code Online (Sandbox Code Playgroud) 我知道可以geom_bar使用width参数来更改条形的宽度。确实可以,但是随后会在条之间产生更大的间隙。有没有办法手动将拉杆推得更近些?我应该以某种方式操纵轴吗?
这是一个示例,将右侧的宽度更改为0.3,以获得所需的条形宽度。
library(tidyverse)
library(gridExtra)
p1 <- ggplot(iris, aes(Species, Petal.Length)) + geom_bar(stat="summary")
p2 <- ggplot(iris, aes(Species, Petal.Length)) + geom_bar(stat="summary", width=0.3)
grid.arrange(p1,p2,nrow=1)
Run Code Online (Sandbox Code Playgroud)
注意:我知道这个问题与此类似,但是缩小差距的答案并不明显。