r - 更改R ggplot中条形之间的间隙
问题描述
我正在尝试在 R,ggplot2 中制作分组条形图。有两组 A 和 B(可变刺激)。每组有两个小节(前和结束),总共 4 个小节。我能够修改 pre 和 end 之间的间隙,但不能修改分组的条 A 和 B 之间的间隙(太远)。你可以帮帮我吗?提前谢谢这是我的代码。
cols <- c( "#660000", "#000333")
TPS <- ggplot(Tonic_pupilsize, aes(x=Stimulation, y= (Tonic), fill = factor(Time))) +
stat_summary(geom = "bar", fun.y = mean, show.legend = T, width=0.4, position = position_dodge(width=0.45)) +
scale_fill_manual(values = c( "#D55E00", "#0072B2"), name = "", labels = c("Pre", "End"))
TPS
TPS1 <- TPS + stat_summary(fun.data =mean_se, geom = "errorbar", width=0.4, size=0.6, position = position_dodge(width=0.45))
TPS2 <- TPS1 + geom_point(aes(colour = factor(Time)),
size = 4, alpha = 0.3,
position = position_dodge(width = 0.45))+
guides(color = FALSE) +
scale_colour_manual(values = cols)
TPS3 <- TPS2 + labs(x = "", y = " (px)") + # change labels
scale_x_discrete(labels = c("A", "B"), expand = c(0, 0.2)) + theme_bw() + #change background
theme(axis.title.y = element_text(size=26, face="bold"),
axis.text.y = element_text(size= 22),
axis.text.x = element_text(size=22, face="bold"),
legend.text = element_text(size = 22, face="bold"), # change labels
panel.border = element_blank(), panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
axis.line = element_line(colour = "black")) + #change backgroujd
scale_y_continuous(limits = c(0, 2600), expand = c(0, 0))
TPS3
解决方案
这很大程度上取决于您的数据,但例如,如果您使用position_dodge2()
而不是position_dodge()
,您可以定义padding
分组条之间的值(默认为 0.1)。
所以例如:
position = position_dodge2(width = 0.45, padding = 0)
在你的代码里面。