mrpargeter
mrpargeter

Reputation: 329

Add significance bars to proportions plot using ggplot2

I have a barplot of proportions and I would like to add significance bars showing the statistical differences between groups. I would like to use the ggsignif package to create a similar like this:

enter image description here

I have tried using the ggsignif package and ggplot2, but the results do not appear to work for tests of proportions (e.g. chi.square)

My data look like this:

Input =("
        Group  Yes  No
        1       10       90
        2       30       70
        3       20       80
        ")

test_table = as.data.frame(read.table(textConnection(Input),
                              header=TRUE))

And my initial plot looks like this:

ggplot(test_table, 
       aes(x=Group, y=Yes)) 
    + geom_col()

Upvotes: 1

Views: 5497

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50668

Here is a possibility.

We first calculate pairwise comparisons between pairs of proportions (correcting for multiple hypothesis testing) using base R's pairwise.prop.test (see ?pairwise.prop.test and ?prop.test for details):

library(broom)
library(tidyverse)
res <- pairwise.prop.test(as.matrix(test_table[, -1])) %>%
    tidy() %>%
    mutate_at(vars(contains("group")), ~factor(.x, test_table$Group))
res
## A tibble: 3 x 3
#  group1 group2 p.value
#  <fct>  <fct>    <dbl>
#1 2      1      0.00235
#2 3      1      0.149
#3 3      2      0.149

I use broom::tidy to tidy the output of pairwise.prop.test; this is not a critical dependence but saves us some time.

Then we can plot the Yes/(Yes + No) proportions and overlay p-values from the pairwise tests proportion tests

library(ggsignif)
df <- test_table %>%
    mutate(Prop = Yes / (Yes + No))

ggplot(df, aes(Group, Prop, group = Group)) +
    geom_col() +
    geom_signif(
        xmin = as.integer(res$group1),
        xmax = as.integer(res$group2),
        y_position = 0.3 + df$Prop,
        annotation = format(res$p.value, digits = 3),
        tip_length = 0.1)

enter image description here

Upvotes: 8

Related Questions