Big Old Dave
Big Old Dave

Reputation: 349

How to display Fisher's Exact rather than Chi-sq p-value on vcd::mosaic plot legend

The following mosaic plot code, using very toy data, indicates a significance value as part of the legend. This appears to be calculated using the same chi-sq approximation used in base R, as confirmed by the relevant line of code (i.e. chisq.test()).

Is there a relatively simple way of over-writing the chi-sq result in the legend with, say, a p-value derived from Fisher's Exact test (i.e. using fisher.test())? Or is there a possible option in the vcd package?

tbl <- ( cbind(c(15,10),c(1,10)) )

dimnames(tbl) <- list(outcome = c("So fun!","Meh!"), adjustment = c("A","B")) 

stats::chisq.test( tbl, correct = FALSE ) ## p-value = 0.004631 

vcd::mosaic(tbl, 
            gp = shading_hcl, 
            split_vertical = TRUE,
            main = "Toy Example")

stats::fisher.test( tbl ) ## p-value = 0.009091

Upvotes: 1

Views: 57

Answers (1)

TarJae
TarJae

Reputation: 78907

One way is to manually add the Fisher exact test using the grid package:

# Your code

tbl <- ( cbind(c(15,10),c(1,10)) )

dimnames(tbl) <- list(outcome = c("So fun!","Meh!"), adjustment = c("A","B")) 

stats::chisq.test( tbl, correct = FALSE ) ## p-value = 0.004631 

vcd::mosaic(tbl, 
            shade = FALSE,
            split_vertical = TRUE,
            main = "Toy Example")

stats::fisher.test( tbl ) ## p-value = 0.009091

# New code

fisher_p <- fisher.test(tbl)$p.value

grid::grid.text(
  label = paste0("Fisher's Exact p = ", format.pval(fisher_p, digits = 3)),
  x     = grid::unit(0.5, "npc"), 
  y     = grid::unit(0.1, "npc")
)

enter image description here

Upvotes: 2

Related Questions