简体   繁体   English

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

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

I am having a hard time adding individual data points on my barplots using ggbarplot.我很难使用 ggbarplot 在我的条形图上添加单个数据点。 I seem to have an issue particularly on multi-grouped data (using fill or color).我似乎在多组数据(使用填充或颜色)上特别有问题。 I've tried a couple different ways to add dots, but they've either resulted in an error (adding jitter) or the dots are not properly aligned to the correct bar (adding dotplot).我尝试了几种不同的方法来添加点,但它们要么导致错误(添加抖动),要么点没有正确对齐到正确的条形(添加点图)。 Any help would be greatly appreciated.任何帮助将不胜感激。

Example data:示例数据:

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
")

I can get it to work if I use "add = c(point)" but when I try to use "add = c(jitter)" it results in an error ("position_jitterdodge()' requires at least one aesthetic to dodge by. I've seen similar questions asked here, but were usually resolved by adding the grouping factor to "Fill" instead of "color." I tried both and neither worked.如果我使用“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) 

Trying to use "add = c("dotplot)" results in the dots being mixed between my second group and shown in between the two side-by-side groups.尝试使用 "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) 

This seems to work... but I would really like to use different colors, shapes, and add jitter.这似乎有效……但我真的很想使用不同的颜色、形状并添加抖动。 Example below.下面举例。

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)

在此处输入图片说明

The issue is that with add.params(color = "black") you are overwriting the grouping ( color = "Group" ) needed for dodging the points.问题是,使用add.params(color = "black")覆盖躲避点所需的分组( color = "Group" )。 Hence, dropping add.params(color = "black") will make your code work.因此,删除add.params(color = "black")将使您的代码工作。 If you want black points and error bars you can do that via eg scale_color_manual by setting the colors for all groups to "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"))

EDIT编辑

The issue is that packages like ggpubr offer a lot out of the box.问题是像ggpubr这样的软件包提供了很多开箱即用的功能。 The "price" one pays for that is that out-of-the-box solutions have their limits.一个人为此付出的“代价”是开箱即用的解决方案有其局限性。 If you want have more control about the appearence of your plot you have to do it (at least partially) using ggplot2, eg to color the points you can the jittered points using geom_jitter .如果你想对自己的情节appearence更多的控制,你必须使用GGPLOT2做到这一点(至少部分地),例如以颜色,你可以使用抖动点的点geom_jitter Here I change the shape of the points so that I can add a black outline.在这里,我更改了点的形状,以便可以添加黑色轮廓。 Unfortunately it is not possibleto change the fill color as well as jitterdodge requires the fill aes:不幸的是,无法更改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))

Another option would be to use eg另一种选择是使用例如

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

which gives you "filled" eg "grey" points but with no outline.它为您提供“填充”,例如“灰色”点,但没有轮廓。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM