0

I would like to make a graph similar to those graphs here Indicating the statistically significant difference in bar graph

Considering the following example:

library(ggplot2)

# my data
my_data <- data.frame(x = c("No","Yes"), y=c(5,25), lower = c(1,10), upper = c(15,50))

I make a barchart with some errorbars, which works fine.

my_data %>% ggplot(aes(x=withdrawal,y=estimate)) +
    geom_bar(stat="identity", fill="grey", width=0.5) +
    geom_errorbar(ymin=lower, ymax=upper, width = 0.15) +
    coord_cartesian(ylim = c(0,70))

Okay, now I would like to add some annotations with a p-value, which also works fine.

 my_data %>% ggplot(aes(x=withdrawal,y=estimate)) +
    geom_bar(stat="identity", fill="grey", width=0.5) +
    geom_errorbar(ymin=lower, ymax=upper, width = 0.15) +
    coord_cartesian(ylim = c(0,70)) +
    annotate("text",x=1.5,y=65,label="p<0.001")

enter image description here

Well, but now I would like to add a line indicating the statistically significant difference in bar graph.

 my_data %>% ggplot(aes(x=withdrawal,y=estimate)) +
    geom_bar(stat="identity", fill="grey", width=0.5) +
    geom_errorbar(ymin=lower, ymax=upper, width = 0.15) +
    coord_cartesian(ylim = c(0,70)) +
    annotate("text",x=1.5,y=65,label="p<0.001") +
    geom_path(x=c(1,1,2,2),y=c(55,60,60,55))

Now I did not work. So what is the problem with geom_path? I tried to change the mapping with x.

 my_data <- data.frame(x = c(1,2), y=c(5,25), lower = c(1,10), upper = c(15,50))

 my_data %>% ggplot(aes(x=withdrawal,y=estimate)) +
    geom_bar(stat="identity", fill="grey", width=0.5) +
    geom_errorbar(ymin=lower, ymax=upper, width = 0.15) +
    coord_cartesian(ylim = c(0,70)) +
    annotate("text",x=1.5,y=65,label="p<0.001") +
    geom_path(x=c(1,1,2,2),y=c(55,60,60,55))

Still did not work. What may I do to make geom_path work?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Kev
  • 425
  • 3
  • 8

2 Answers2

1

After trying around, I came up with the following solution.

my_data %>% ggplot(aes(x=withdrawal,y=estimate)) +
  geom_bar(stat="identity", fill="grey", width=0.5) +
  geom_errorbar(ymin=lower, ymax=upper, width = 0.15) +
  coord_cartesian(ylim = c(0,70)) +
  annotate("text",x=1.5,y=65,label="p<0.01")
  geom_path(data = data.frame(x=c(1,1,2,2),y=c(58,60,60,58)), aes(x=x,y=y))

It is a bit messy and there a probably better answers.

Kev
  • 425
  • 3
  • 8
  • For any sort of annotation more than a single label or point, I usually use this method. It's the most flexible in terms of adding exactly what you want where you want it, especially with faceted panels. – Brian Aug 10 '17 at 14:04
0

This previous post asking the same question could be useful: Indicating the statistically significant difference in bar graph USING R

The ggsignif package is one alternative option. For example, see the introductory vignette: https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html

Eddd
  • 69
  • 3
  • I know that link, but the code provided there in the first example did not work in my case. I had to do a workaround creating a new data frame for geom_path. – Kev Aug 10 '17 at 14:57