add p-values of Pearson's chi-squared test to facet ggplots

5.8k views Asked by At

I compare categorical data from three different groups.

I wonder if it is possible to easily add p-values of chi-squared tests to facet ggplots (since I am analyzing a big data set). I just read that there is a marvelous way to do so when comparing means https://www.r-bloggers.com/add-p-values-and-significance-levels-to-ggplots/. However, I could not find a solution for other tests (like the chisq.test in my case).

d.test <- data.frame(
  results = sample(c("A","B","C"), 30, replace =TRUE),
  test = sample(c("test1", "test2","test3"), 30, replace = TRUE)
)

chisq.test(d.test$results,d.test$test)

ggplot(d.test, aes(results) ) +
  geom_bar() + facet_grid(test ~ .)

Many thanks for your help! ;D

2

There are 2 answers

0
Brian On BEST ANSWER

broom has methods to create tidy dataframes of most statistical test outputs. Then you can use that output as a data = argument within geom_text.

Generate data

library(broom)
library(dplyr)
library(ggplot2)

fakedata <- 
  data.frame(groups = sample(c("pop1", "pop2", "pop3", "pop4"), 120, replace = T),
             results = sample(c("A","B","C"), 120, replace = TRUE),
             test = sample(c("test1", "test2","test3"), 120, replace = TRUE))

Conduct and tidy tests

fakedata.test <-
  fakedata %>% 
    group_by(groups) %>% 
    do(fit = chisq.test(.$results, .$test)) %>% 
    tidy(fit)
# A tibble: 4 x 5
# Groups:   groups [4]
  groups statistic    p.value parameter                     method
  <fctr>     <dbl>      <dbl>     <int>                     <fctr>
1   pop1  3.714286 0.44605156         4 Pearson's Chi-squared test
2   pop2  2.321429 0.67687042         4 Pearson's Chi-squared test
3   pop3  2.294897 0.68169829         4 Pearson's Chi-squared test
4   pop4 10.949116 0.02714188         4 Pearson's Chi-squared test

Visualize

fakedata %>% 
  ggplot(aes(results, test)) + 
  geom_jitter(width = 0.2, height = 0.2, shape = 1, size = 2) +
  geom_text(data = fakedata.test,
            aes(3, 3.5, 
                label = paste0("χ²(", parameter, ")=", round(statistic, 2), "; p=", round(p.value, 2))),
            hjust = 1) +
  facet_wrap(~groups)

enter image description here

2
CPak On

Store your p-value in a variable

pval <- chisq.test(d.test$results,d.test$test)$p.value

Use annotate to plot text manually

ggplot(d.test, aes(results) ) +
  geom_bar() + facet_grid(test ~ .) +
  annotate("text", x=1, y=5, label=pval)

Change its positioning with x and y

ggplot(d.test, aes(results) ) +
  geom_bar() + facet_grid(test ~ .) +
  annotate("text", x=2, y=3, label=pval)    

Change significant digits displayed with signif

ggplot(d.test, aes(results) ) +
  geom_bar() + facet_grid(test ~ .) +
  annotate("text", x=1, y=5, label=signif(pval,4))

Add a 'label' p-value: with

ggplot(d.test, aes(results) ) +
  geom_bar() + facet_grid(test ~ .) +
  annotate("text", x=1, y=5, label=paste0("p-value: ", signif(pval,4)))