Visualizing 2x2 or 2x3 contingency tables using corrplot

353 Views Asked by At

I have a dataset from which I am trying to visualize a contingency table of ETH (ethnicity coded as 0 or 1) and SEX (sex coded as 0 or 1).

This is what my dataset looks like:

enter image description here

This is my coding in R:

library(readxl)

library(corrplot)

#Dataset

Datavisit1<- read_excel("~/Downloads/Datavisit1.xlsx")

View(Datavisit1)

#Contingency Table

ethsextable<- table(Datavisit1$ETH, Datavisit1$SEX, dnn = c("ETH", "SEX"))

ethsextable

ethsextablechi<- chisq.test(ethsextable)

ethsextablechi

corrplot(ethsextablechi$residuals, is.corr = FALSE)

This is the plot I see:

enter image description here

  1. How do I add the labels of ETH and SEX to this corrplot of the residuals? Right now I am confused which one is ETH and which one is SEX.

  2. How do I put the legend that has the Pearson residuals numbers slightly to the right? At the moment the numbers are above the colour bar and hard to see.

  3. Is the visualization of the chi-squared residuals of 2x2 or 2x3 contingency tables better through corrplots or through mosaic plots? Which is the more standard way of visual representation?

Thank you!

1

There are 1 best solutions below

0
On

I have a function in a package that I wrote for a class that's on CRAN called uwo4419 that has a function in it to plot standardized residuals from a chi-squared test.

set.seed(1240)
dat <- data.frame(
  x = sample(1:2, 100, prob=c(.75, .25), replace=TRUE), 
  y = sample(1:3, 100, prob=c(.2,.4,.2), replace=TRUE)
)

tab <- table(dat)
uwo4419::plotStdRes(tab)

enter image description here

This would seem to answer all of your questions. I feel like a heatmap plot is a good way to visualize standardized residuals.