admin管理员组

文章数量:1186278

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

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
Share Improve this question edited Jan 26 at 22:12 Big Old Dave asked Jan 26 at 22:06 Big Old DaveBig Old Dave 3492 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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")
)

本文标签: rHow to display Fisher39s Exact rather than Chisq pvalue on vcdmosaic plot legendStack Overflow