Home > Enterprise >  t-test for count data using stat_compare_means
t-test for count data using stat_compare_means

Time:09-21

I'm trying to run a simple t.test on a dataset counting the decision a beetle made between two options. I.E. a beetle walked toward or away from it's host tree. Is there a way to use stat_compare_means on count data in ggplot?

ggplot(dat %>% na.omit(), aes(x=choice))   
geom_bar()   
stat_compare_means()

CodePudding user response:

A t-test is the wrong test to check whether a proportions are significantly different. We can use prop.test instead, and add it with stat_pval_manual

library(tidyverse)
library(ggpubr)

ggplot(dat %>% na.omit() %>% group_by(choice) %>% count(), 
       aes(x = choice, y = n))   
  stat_pvalue_manual(data = data.frame(
                     group1 = "go", group2 = "stay",
                      p = prop.test(table(na.omit(dat$choice)))$p.value,
                      y.position = max(table(na.omit(dat$choice))) * 1.2),
                   label = "prop.test, p = {round(p, 3)}", bracket.size = 1,
                   size = 6, tip.length = 0.1)  
  geom_col(width = 0.5, aes(fill = choice))   
  scale_y_continuous(limits = c(0, 80))  
  theme_light(base_size = 20)  
  scale_fill_manual(values = c("deepskyblue4", "orange"), guide = "none")

enter image description here


Data used

set.seed(1)
dat <- data.frame(choice = sample(c("stay", "go"), 100, TRUE, c(0.35, 0.65)))
  • Related