Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R) How to change the color of mean_se in the ggbarplot?

when the standard error bar is overlapped with the jitter, I want to show the error bar by changing the color of it.
However, when I added add.params = list(color = "black"), all additional components were changed to black.
How can I only change the color of the error bar?

# original code: https://rpkgs.datanovia.com/ggpubr/reference/ggbarplot.html
library(ggpubr)

ggbarplot(ToothGrowth, x = "dose", y = "len", color = "supp", 
          add = c("mean_se", 'jitter'), palette = c("#00AFBB", "#E7B800"),
          position = position_dodge(0.8))

Original plot
enter image description here

Expected plot
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One option to fix that would be to manipulate the ggplot2 object, i.e. for the example plot the error bars are added as the third layer:

library(ggpubr)
#> Loading required package: ggplot2

p <- ggbarplot(ToothGrowth,
  x = "dose", y = "len", color = "supp",
  add = c("mean_se", "jitter"), palette = c("#00AFBB", "#E7B800"),
  position = position_dodge(0.8)
)

p$layers
#> [[1]]
#> mapping: colour = ~supp 
#> geom_bar: just = 0.5, width = 0.7, na.rm = FALSE, orientation = NA
#> stat_identity: na.rm = FALSE
#> position_dodge 
#> 
#> [[2]]
#> mapping: colour = ~supp 
#> geom_point: na.rm = FALSE
#> stat_identity: na.rm = FALSE
#> position_jitterdodge 
#> 
#> [[3]]
#> mapping: colour = ~supp, group = ~supp 
#> geom_errorbar: na.rm = FALSE, orientation = NA, width = 0.1
#> stat_summary: fun.data = mean_se_, fun = NULL, fun.max = NULL, fun.min = NULL, fun.args = list(error.limit = "both"), na.rm = FALSE, orientation = NA
#> position_dodge

Based on this information we can set the colour parameter to black for the error bars only like so:

p$layers[[3]]$aes_params$colour <- "black"

p

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading