CorLevelPlot provides a quick and colourful way to visualise statistically significant correlations between any combination of categorical and continuous variables. Moreover, the statistical significancies of these correlations are also provided.
library(knitr) opts_chunk$set(tidy = FALSE, message = FALSE, warning = FALSE)
Install and load CorLevelPlot:
devtools::install_github("kevinblighe/CorLevelPlot") library(CorLevelPlot)
The following code taken from Tutorial for the WGCNA package for R - 1. Simulation of expression and trait data
# simulate trait-to-eigengene data no.obs <- 50 ESturquoise <- 0; ESbrown <- -0.6; ESgreen <- 0.6; ESyellow <- 0 ESvector <- c(ESturquoise, ESbrown, ESgreen, ESyellow) nGenes1 <- 3000 simulateProportions1 <- c(0.2, 0.15, 0.08, 0.06, 0.04) set.seed(1) MEgreen <- rnorm(no.obs) scaledy <- MEgreen * ESgreen + sqrt(1 - ESgreen ^ 2) * rnorm(no.obs) y <- ifelse( scaledy > median(scaledy), 2, 1) MEturquoise <- ESturquoise * scaledy + sqrt(1 - ESturquoise ^ 2) * rnorm(no.obs) MEblue <- 0.6 * MEturquoise + sqrt(1 - 0.6 ^ 2) * rnorm(no.obs) MEbrown <- ESbrown * scaledy + sqrt(1 - ESbrown ^ 2) * rnorm(no.obs) MEyellow <- ESyellow * scaledy + sqrt(1 - ESyellow ^ 2) * rnorm(no.obs) ModuleEigengeneNetwork1 <- data.frame(y, MEturquoise, MEblue, MEbrown, MEgreen, MEyellow) CorLevelPlot(data = ModuleEigengeneNetwork1, x = c("y", "MEturquoise", "MEblue", "MEbrown", "MEgreen", "MEyellow"), y = c("y", "MEturquoise", "MEblue", "MEbrown", "MEgreen", "MEyellow"), titleX = "X correlates", cexTitleX = 3.0, rotTitleX = 0, colTitleX = "forestgreen", fontTitleX = 2, titleY = "Y\ncorrelates", cexTitleY = 4.0, rotTitleY = 100, colTitleY = "gold", fontTitleY = 4, cexLabX = 1.0, rotLabX = 45, colLabX = "grey20", fontLabX = 1, cexLabY = 1.0, rotLabY = 30, colLabY = "royalblue", fontLabY = 1, posLab = "bottomleft", col = c("blue4", "blue3", "blue2", "blue1", "white", "red1", "red2", "red3", "red4"), posColKey = "right", cexLabColKey = 1.2, cexCorval = 1.0, fontCorval = 4, main = "WGCNA example", scale = FALSE, cexMain = 2, rotMain = 360, colMain = "red4", fontMain = 4, corFUN = "pearson", corUSE = "pairwise.complete.obs", signifSymbols = c("***", "**", "*", ""), signifCutpoints = c(0, 0.001, 0.01, 0.05, 1), colFrame = "white", plotRsquared = FALSE)
library(datasets) data(iris) # order the categories in the 'Species' column # CorLevelPlot will conver these to 1, 2, 3, ... iris$Species <- as.numeric(factor(iris$Species, levels=c("setosa", "versicolor", "virginica"))) i <- CorLevelPlot(data = iris, x = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species"), y = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species"), col = c("white", "cornsilk1", "yellow", "gold", "forestgreen", "darkgreen"), cexCorval = 1.2, fontCorval = 2, posLab = "all", rotLabX = 45, scale = TRUE, main = bquote(Iris~r^2~correlates), plotRsquared = TRUE) pca <- stats::prcomp(iris[,c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")]) df <- data.frame(pca$x, iris) ii <- CorLevelPlot(data = df, x = c("PC1", "PC2", "PC3", "PC4"), y = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species"), cexTitleX = 2.0, rotTitleX = 0, fontTitleX = 2, titleY = "Iris components", cexTitleY = 2.0, rotTitleY = 90, fontTitleY = 2, posLab = "topright", col = c("blue1", "skyblue", "white", "pink", "red1"), posColKey = "bottom", cexLabColKey = 1.5, cexCorval = 1.5, fontCorval = 2, rotLabX = 45, scale = TRUE, main = "Iris PC correlates", colFrame = "white", plotRsquared = FALSE) require(rasterVis) require(gridExtra) require(grid) grid.arrange( arrangeGrob(i, top = textGrob("A", x = unit(0.05,"npc"), y = unit(0.9,"npc"), just = c("left","top"), gp = gpar(fontsize=32))), arrangeGrob(ii, top = textGrob("B", x = unit(0.05,"npc"), y = unit(0.9,"npc"), just = c("left","top"), gp = gpar(fontsize=32))), ncol = 2)
library(DAAG) data(monica) # order the categorical variables monica$outcome <- as.numeric(factor(monica$outcome, levels=c("dead", "live"))) monica$diabetes[monica$diabetes=="nk"] <- NA monica$diabetes <- as.numeric(factor(monica$diabetes, levels=c("n", "y"))) monica$hichol[monica$hichol=="nk"] <- NA monica$hichol <- as.numeric(factor(monica$hichol, levels=c("n", "y"))) monica$stroke[monica$stroke=="nk"] <- NA monica$stroke <- as.numeric(factor(monica$stroke, levels=c("n", "y"))) monica$sex <- as.numeric(factor(monica$sex, levels=c("m", "f"))) monica$yronset <- as.numeric(factor(monica$yronset, levels=c("85","86","87","88","89","90","91","92","93"))) monica$highbp[monica$highbp=="nk"] <- NA monica$highbp <- as.numeric(factor(monica$highbp, levels=c("n", "y"))) monica$angina[monica$angina=="nk"] <- NA monica$angina <- as.numeric(factor(monica$angina, levels=c("n", "y"))) monica$hosp <- as.numeric(factor(monica$hosp, levels=c("n", "y"))) CorLevelPlot(data = monica, x = c("outcome", "diabetes", "highbp", "hichol", "angina", "hosp"), y = c("sex", "age", "yronset"), col = c("darkblue", "blue2", "black", "red2", "darkred"), cexCorval = 1.5, colCorval = "white", fontCorval = 2, posLab = "bottomleft", rotLabX = 45, posColKey = "top", cexLabColKey = 1.2, scale = TRUE, main = "World Health Organization", colFrame = "white", plotRsquared = FALSE)
sessionInfo()
[@CorLevelPlot]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.