王玉璞
王玉璞

Reputation: 11

How to add number of observation in bar plot? (need to display error bar and significance together)

I'm constructing my own graph by following the codes in a tutorial:

library(ggpubr)
data("ToothGrowth")
ggbarplot(ToothGrowth, x = "dose", y = "len", 
          add = c("mean_se", "jitter"),
          color = "supp", palette = "jco",
          position = position_dodge(0.8))

I want to show the number of observation on each column (on the bottom or on the top), together with the error bar and significance. Could anyone use ToothGrowth as an example to plot like this?

displaying n number at the bottom

Upvotes: 1

Views: 1884

Answers (1)

moodymudskipper
moodymudskipper

Reputation: 47350

You can try this :

ggbarplot(ToothGrowth, x = "dose", y = "len", 
          add = c("mean_se", "jitter"),
          color = "supp", palette = "jco",
          position = position_dodge(0.8)) +
  geom_text(aes(x = factor(dose),
                y = 0,
                label = paste("n =",len,"\n"),
                group = supp),
            aggregate(. ~ dose + supp,ToothGrowth,length),
            position = position_dodge(.8))

enter image description here

I didn't find how to plot one error bar for each group with this package, and i'm not sure if it's possible, by simplifying the interface with such package you also lose flexibility.

Upvotes: 1

Related Questions