如何使用 ggbarplot 将单个数据点添加到我的条形图中? (添加抖动不起作用,点图也不起作用)

How can I add individual data points to my bar plot using ggbarplot? (add jitter does not work, neither does dotplot)

我很难使用 ggbarplot 在我的条形图上添加单个数据点。我似乎有一个问题,特别是关于多分组数据(使用填充或颜色)。我尝试了几种不同的方法来添加点,但它们要么导致错误(添加抖动),要么点没有正确对齐到正确的条(添加点图)。任何帮助将不胜感激。

示例数据:

library(ggpubr)


#dataframe
ER <- read.table(header=TRUE, text="
  Sex  Group ER
  M    V     1046
  M    V     1290
  M    Z     1202
  M    Z     1056
  F    V     8000
  F    V     7859
  F    Z     4000
  F    Z     3409
")

如果我使用“add = c(point)”,我可以让它工作,但是当我尝试使用“add = c(jitter)”时,它会导致错误 ("position_jitterdodge() ' 至少需要一种美学来躲避。我在这里看到过类似的问题,但通常是通过将分组因子添加到“填充”而不是“颜色”来解决的。我都试过了,但都没有用。

e <- ggbarplot(ER, x = "Sex", y = "ER", title = "Arcuate Nucleus",
                 ylab= "ER Labeling", xlab = "Group", color = "Group",
                 add = c("mean_se", "jitter"), add.params = list(color = "black"), palette = "jco",
                 position = position_dodge(.8)) 
print(e) 

尝试使用“add = c(”dotplot)”会导致点在我的第二组之间混合并显示在两个并排组之间。

e2 <- ggbarplot(ER, x = "Sex", y = "ER", title = "Arcuate Nucleus",
                 ylab= "ER Labeling", xlab = "Group", color = "Group",
                 add = c("mean_se", "dotplot"), add.params = list(color = "black"), palette = "jco",
                 position = position_dodge(.8)) 
print(e2) 

这似乎可行...但我真的很想使用不同的颜色、形状并添加抖动。示例如下。

e3 <- ggbarplot(ER, x = "Sex", y = "ER", title = "Arcuate Nucleus",
                 ylab= "ER Labeling", xlab = "Group", color = "Group",
                 add = c("mean_se", "point"), add.params = list(color = "black"), palette = "jco",
                 position = position_dodge(.8)) + theme(axis.line = element_line(size = 0.9), axis.ticks = element_line(size = 0.9)) 

print(e3)

问题在于,add.params(color = "black") 会覆盖躲避点所需的分组 (color = "Group")。因此,删除 add.params(color = "black") 将使您的代码正常工作。如果你想要黑点和错误栏,你可以通过例如scale_color_manual 通过将所有组的颜色设置为 "black":

library(ggpubr)
#> Loading required package: ggplot2

ER <- read.table(header=TRUE, text="
  Sex  Group ER
  M    V     1046
  M    V     1290
  M    Z     1202
  M    Z     1056
  F    V     8000
  F    V     7859
  F    Z     4000
  F    Z     3409
")

ggbarplot(ER, x = "Sex", y = "ER", title = "Arcuate Nucleus",
               ylab= "ER Labeling", xlab = "Group", color = "Group", fill = "Group",
               add = c("mean_se", "jitter"), palette = "jco",
               position = position_dodge(.8)) +
  scale_color_manual(values = c(V = "black", Z = "black"))

编辑

问题是像 ggpubr 这样的软件包提供了很多开箱即用的功能。为此付出的“代价”是 out-of-the-box 解决方案有其局限性。如果你想更多地控制你的情节的出现,你必须(至少部分地)使用 ggplot2,例如使用 geom_jitter 为抖动点着色。在这里,我更改了点的形状,以便添加黑色轮廓。不幸的是,无法更改 fill 颜色,而且 jitterdodge 需要填充 aes:

ggbarplot(ER, x = "Sex", y = "ER", title = "Arcuate Nucleus",
          ylab= "ER Labeling", xlab = "Group", color = "Group", fill = "Group",
          add = c("mean_se"), palette = "jco",
          position = position_dodge(.8)) +
  scale_color_manual(values = c(V = "black", Z = "black")) +
  geom_jitter(aes(Sex, ER, fill = Group), shape = 21, color = "black",  position = position_jitterdodge(jitter.height = .1, jitter.width = .2))

另一种选择是使用例如

geom_jitter(aes(Sex, ER, fill = Group), color = "grey",  position = position_jitterdodge(jitter.height = .1, jitter.width = .2))

这让你“充满”,例如“灰色”点但没有轮廓。