简体   繁体   中英

Visualizing 2x2 or 2x3 contingency tables using corrplot

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:

在此处输入图像描述

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:

在此处输入图像描述

  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!

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)

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM