更改R ggplot中的条之间的间隙

时间:2020-07-02 15:51:50

标签: r ggplot2

我正在尝试在R ggplot2中绘制成组的条形图。有两组A和B(可变刺激)。对于每个组,有两个小节(前和末尾),总共4个小节。我能够修改前后BUT之间的间隙,而不是修改成组的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

1 个答案:

答案 0 :(得分:0)

这非常取决于您的数据,但是如果您使用position_dodge2()而不是position_dodge(),则可以在分组条之间定义padding(默认值为0.1)。

例如:

position = position_dodge2(width = 0.45, padding = 0)

您内部的代码。