kassambara / ggpubr

'ggplot2' Based Publication Ready Plots
https://rpkgs.datanovia.com/ggpubr/
1.12k stars 161 forks source link

ggbarplot missplaces error bars when positive and negative values are stacked #426

Open Agustin-5 opened 2 years ago

Agustin-5 commented 2 years ago

I know issue #147 was about a similar problem, but I'm still having the issue of displaced error bars when trying to stack positive and negative values together. The latest version of ggpubr works great when plotting only positive or only negative values, but it displaces the error bars when I mix positive and negative values. It is probably a very specific issue, but in this case positive values represent aboveground data and negative values represent belowground data so it makes sense to put all together in the same figure. Any suggestions? Thanks!

rec Block Treatment Depth lit.der_SOC try 1 BI IRRI Litter 200.60140696 Added 2 BI DRY Litter 200.15854722 Added 3 BII IRRI Litter 200.36284589 Added 4 BII DRY Litter 201.07118712 Added 5 BIII IRRI Litter 200.83262608 Added 6 BIII DRY Litter 200.95313025 Added 7 BIV IRRI Litter 201.06384517 Added 8 BIV DRY Litter 199.79214004 Added 9 BI IRRI Litter 162.67223066 6 months 10 BI DRY Litter 145.30796715 6 months 11 BII IRRI Litter 165.94734970 6 months 12 BII DRY Litter 165.42415364 6 months 13 BIII IRRI Litter 166.56132628 6 months 14 BIII DRY Litter 175.03680007 6 months 15 BIV IRRI Litter 167.19386417 6 months 16 BIV DRY Litter 171.55939042 6 months 17 BI IRRI Litter 200.48335009 Added 18 BI DRY Litter 202.71174675 Added 19 BII IRRI Litter 201.07608174 Added 20 BII DRY Litter 200.49558665 Added 21 BIII IRRI Litter 198.76389057 Added 22 BIII DRY Litter 201.29995892 Added 23 BIV IRRI Litter 200.71456918 Added 24 BIV DRY Litter 201.75994984 Added 25 BI IRRI Litter 81.67314658 13 months 26 BI DRY Litter 69.87997411 13 months 27 BII IRRI Litter 57.66392059 13 months 28 BII DRY Litter 80.78068368 13 months 29 BIII IRRI Litter 79.46654447 13 months 30 BIII DRY Litter 119.43297578 13 months 31 BIV IRRI Litter 73.58404924 13 months 32 BIV DRY Litter 92.26087859 13 months 33 BI IRRI 0-5 cm -16.61684020 13 months 34 BI IRRI 5-10 cm -0.85861080 13 months 35 BI IRRI 10-20 cm -0.26653591 13 months 36 BI IRRI 20-40 cm 0.00000000 13 months 37 BI DRY 0-5 cm -18.19397017 13 months 38 BI DRY 5-10 cm -1.60913363 13 months 39 BI DRY 10-20 cm -0.58920156 13 months 40 BI DRY 20-40 cm 0.00000000 13 months 41 BII IRRI 0-5 cm -21.84582760 13 months 42 BII IRRI 5-10 cm -1.53017507 13 months 43 BII IRRI 10-20 cm -0.43183087 13 months 44 BII IRRI 20-40 cm -0.57452145 13 months 45 BII DRY 0-5 cm -14.69022133 13 months 46 BII DRY 5-10 cm -2.62928253 13 months 47 BII DRY 10-20 cm -0.97054075 13 months 48 BII DRY 20-40 cm -1.27804330 13 months 49 BIII IRRI 0-5 cm -11.67742553 13 months 50 BIII IRRI 5-10 cm -1.98756823 13 months 51 BIII IRRI 10-20 cm -0.66220384 13 months 52 BIII IRRI 20-40 cm 0.00000000 13 months 53 BIII DRY 0-5 cm -12.02927203 13 months 54 BIII DRY 5-10 cm -1.65783250 13 months 55 BIII DRY 10-20 cm -0.94420555 13 months 56 BIII DRY 20-40 cm -0.98772403 13 months 57 BIV IRRI 0-5 cm -11.29702920 13 months 58 BIV IRRI 5-10 cm -1.24230185 13 months 59 BIV IRRI 10-20 cm -0.67237858 13 months 60 BIV IRRI 20-40 cm -0.09959631 13 months 61 BIV DRY 0-5 cm -12.88809810 13 months 62 BIV DRY 5-10 cm -1.66912975 13 months 63 BIV DRY 10-20 cm -0.75638382 13 months 64 BIV DRY 20-40 cm -0.85880038 13 months 65 BI IRRI 0-5 cm -8.76740676 6 months 66 BI IRRI 5-10 cm -0.63486176 6 months 67 BI IRRI 10-20 cm -0.17377915 6 months 68 BI IRRI 20-40 cm -0.11005066 6 months 69 BI DRY 0-5 cm -7.96025584 6 months 70 BI DRY 5-10 cm -0.90234972 6 months 71 BI DRY 10-20 cm -0.20623258 6 months 72 BI DRY 20-40 cm 0.00000000 6 months 73 BII IRRI 0-5 cm -5.83373867 6 months 74 BII IRRI 5-10 cm -0.17048297 6 months 75 BII IRRI 10-20 cm 0.00000000 6 months 76 BII IRRI 20-40 cm 0.00000000 6 months 77 BII DRY 0-5 cm -8.53555872 6 months 78 BII DRY 5-10 cm -0.22213583 6 months 79 BII DRY 10-20 cm -0.67072101 6 months 80 BII DRY 20-40 cm 0.00000000 6 months 81 BIII IRRI 0-5 cm -8.56494530 6 months 82 BIII IRRI 5-10 cm -0.16359151 6 months 83 BIII IRRI 10-20 cm 0.00000000 6 months 84 BIII IRRI 20-40 cm 0.00000000 6 months 85 BIII DRY 0-5 cm -9.29394800 6 months 86 BIII DRY 5-10 cm -0.34334168 6 months 87 BIII DRY 10-20 cm 0.00000000 6 months 88 BIII DRY 20-40 cm -0.42264637 6 months 89 BIV IRRI 0-5 cm -7.67759931 6 months 90 BIV IRRI 5-10 cm -0.51149227 6 months 91 BIV IRRI 10-20 cm 0.00000000 6 months 92 BIV IRRI 20-40 cm 0.00000000 6 months 93 BIV DRY 0-5 cm -9.17671082 6 months 94 BIV DRY 5-10 cm -0.58256320 6 months 95 BIV DRY 10-20 cm 0.00000000 6 months 96 BIV DRY 20-40 cm 0.00000000 6 months

rec %>% ggbarplot(x = "Treatment", y ="lit.der_SOC", add="mean_se", fill= "Depth", ylab=expression("Litter-derived C (g C " m^-{2}")"), xlab="", legend.title= "", facet.by = "try", title="(a)", palette = "aaas", alpha=.7) + geom_hline(yintercept = 0)+ theme_pubr(legend="right")

Rplot image

fpantin commented 1 year ago

Hello, I am experiencing the same problem, which appears only when the data contain both positive and negative values:

library(tidyverse)
library(ggpubr)
library(rstatix)
library(datarium)

data("anxiety", package = "datarium")
anxiety <- anxiety %>%
  gather(key = "time", value = "score", t1, t2, t3) %>%
  convert_as_factor(id, time)
ggbarplot(anxiety, x = "time", y = "score", add = "mean_se", color = "group")

image

anxiety$score_neg <- -anxiety$score
ggbarplot(anxiety, x = "time", y = "score_neg", add = "mean_se", color = "group")

image

anxiety$score_posneg <- anxiety$score
anxiety$score_posneg[anxiety$group == "grp1"] <- -anxiety$score_posneg[anxiety$group == "grp1"]
ggbarplot(anxiety, x = "time", y = "score_posneg", add = "mean_se", color = "group")

image

Note that the level for which the error bar position is correct depends on the order of the factor levels:

anxiety$group <- factor(anxiety$group, levels = c("grp3", "grp2", "grp1"), ordered = T)
ggbarplot(anxiety, x = "time", y = "score_posneg", add = "mean_se", color = "group")

image