├── .Rbuildignore ├── .gitignore ├── .travis.yml ├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── NEWS.md ├── R ├── ggcorrplot.R └── ggcorrplot.mixed.R ├── README.Rmd ├── README.md ├── figs ├── README-unnamed-chunk-2-1.png ├── README-unnamed-chunk-2-10.png ├── README-unnamed-chunk-2-11.png ├── README-unnamed-chunk-2-12.png ├── README-unnamed-chunk-2-2.png ├── README-unnamed-chunk-2-3.png ├── README-unnamed-chunk-2-4.png ├── README-unnamed-chunk-2-5.png ├── README-unnamed-chunk-2-6.png ├── README-unnamed-chunk-2-7.png ├── README-unnamed-chunk-2-8.png ├── README-unnamed-chunk-2-9.png ├── README-unnamed-chunk-3-1.png └── README-unnamed-chunk-3-2.png ├── ggcorrplot2.Rproj ├── inst └── CITATION └── man ├── ggcorrplot.Rd └── ggcorrplot.mixed.Rd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^\.travis\.yml$ 4 | ^LICENSE$ 5 | ^README\.Rmd$ 6 | ^figs$ 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # History files 2 | .Rhistory 3 | .Rapp.history 4 | 5 | # Session Data files 6 | .RData 7 | 8 | # User-specific files 9 | .Ruserdata 10 | 11 | # Example code in package build process 12 | *-Ex.R 13 | 14 | # Output files from R CMD build 15 | /*.tar.gz 16 | 17 | # Output files from R CMD check 18 | /*.Rcheck/ 19 | 20 | # RStudio files 21 | .Rproj.user/ 22 | 23 | # produced vignettes 24 | vignettes/*.html 25 | vignettes/*.pdf 26 | 27 | # OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 28 | .httr-oauth 29 | 30 | # knitr and R markdown default cache directories 31 | *_cache/ 32 | /cache/ 33 | 34 | # Temporary files created by R markdown 35 | *.utf8.md 36 | *.knit.md 37 | 38 | # R Environment Variables 39 | .Renviron 40 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # R for travis: see documentation at https://docs.travis-ci.com/user/languages/r 2 | language: r 3 | cache: packages 4 | os: 5 | - osx 6 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: ggcorrplot2 2 | Type: Package 3 | Title: Visualize a Correlation Matrix using ggplot2 4 | Version: 0.1.2 5 | Date: 2022-05-07 6 | Authors@R: c( 7 | person("Jun", "Cai", role = c("aut", "cre"), 8 | email = "cai-j12@mails.tsinghua.edu.cn", comment = c(ORCID = "0000-0001-9495-1226")), 9 | person("Granville", "Matheson", role = "ctb", 10 | email= "mathesong@gmail.com", comment = c(ORCID = "0000-0002-5646-4547")), 11 | person("Samson", "Leonard Daniël", role = "ctb", 12 | email= "l.d.samson@gmail.com", comment = c(ORCID = "0000-0002-6252-7639")) 13 | ) 14 | Maintainer: Jun Cai 15 | Description: An implementation of the corrplot package using ggplot2. 16 | Depends: 17 | R (>= 3.2) 18 | Imports: ggplot2, dplyr, rlang 19 | Suggests: ggforce, RColorBrewer, reshape2 20 | License: GPL-3 21 | URL: https://github.com/caijun/ggcorrplot2 22 | BugReports: https://github.com/caijun/ggcorrplot2/issues 23 | Encoding: UTF-8 24 | LazyData: true 25 | RoxygenNote: 7.1.2 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Jun Cai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(ggcorrplot) 4 | export(ggcorrplot.mixed) 5 | import(ggplot2) 6 | importFrom(dplyr,"%>%") 7 | importFrom(dplyr,case_when) 8 | importFrom(dplyr,group_by) 9 | importFrom(dplyr,group_modify) 10 | importFrom(dplyr,mutate) 11 | importFrom(rlang,.data) 12 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | ### CHANGES IN ggcorrplot VERSION 0.1.0 2 | 3 | #### NEW FEATURES 4 | 5 | - first version of ggcorrplot 6 | 7 | #### NOTE 8 | 9 | - in this NEWS file, #n means the issue number on GitHub, e.g. #1 is https://github.com/caijun/ggcorrplot/issues/1 10 | -------------------------------------------------------------------------------- /R/ggcorrplot.R: -------------------------------------------------------------------------------- 1 | # internal 2 | # @title Add the geom of correlation matrix to the existing plot 3 | # 4 | # @param p a ggplot object 5 | # @param data a data.frame to be plotted 6 | # @param method a character indicating the visualization method of correlation matrix to be used. Currently, it supports four methods, named "circle" (default), "square", "ellipse", "number". 7 | #' @import ggplot2 8 | #' @importFrom dplyr %>% mutate group_by group_modify case_when 9 | #' @importFrom rlang .data 10 | plot.method <- function(p, data, method = c("circle", "square", "ellipse", "number")) { 11 | if (method == "ellipse") { 12 | ellipse.xy <- function(rho, length = 100) { 13 | theta <- seq(0, 2 * pi, length = length) 14 | if (rho == 1) rho <- rho - 1e-4 15 | d <- acos(-rho) 16 | x <- cos(theta + d / 2) / 2 17 | y <- cos(theta - d / 2) / 2 18 | as.data.frame(cbind(x, y)) 19 | } 20 | 21 | myfun <- function(df) { 22 | res <- ellipse.xy(df$rho) %>% 23 | mutate(rid = df$rid, cid = df$cid, rho = df$rho) %>% 24 | # using .data prevents R CMD check from giving a NOTE about undefined global variable 25 | mutate(x1 = 0.9 * .data$x + .data$cid, y1 = 0.9 * .data$y + .data$rid, 26 | group = paste(.data$rid, .data$cid, sep = "-")) 27 | } 28 | 29 | ellipse.dat <- data %>% 30 | group_by(.data$Var1, .data$Var2) %>% 31 | group_modify(~myfun(.x)) 32 | 33 | p <- p + 34 | geom_polygon(data = ellipse.dat, 35 | mapping = aes(x = .data$x1, y = .data$y1, fill = .data$rho, 36 | group = .data$group), 37 | color = NA) 38 | } else if (method == "circle") { 39 | p <- p + 40 | ggforce::geom_circle(data = data, 41 | mapping = aes(x0 = .data$cid, y0 = .data$rid, 42 | r = .data$abs.rho/2 - 0.02, fill = .data$rho), 43 | color = NA) 44 | } else if (method == "square") { 45 | p <- p + 46 | geom_rect(data = data, mapping = aes(xmin = .data$cid - 0.5*(.data$abs.rho - 0.04), 47 | xmax = .data$cid + 0.5*(.data$abs.rho - 0.04), 48 | ymin = .data$rid - 0.5*(.data$abs.rho - 0.04), 49 | ymax = .data$rid + 0.5*(.data$abs.rho - 0.04), 50 | fill = .data$rho)) 51 | } else if (method == "number") { 52 | p <- p + 53 | geom_text(data = data, mapping = aes(x = .data$cid, y = .data$rid, colour = .data$rho), 54 | label = data$rho.label) 55 | } 56 | return(p) 57 | } 58 | 59 | #' @title Visualize a correlation matrix 60 | #' 61 | #' @param corr a correlation matrix to be visualized 62 | #' @param method a character indicating the visualization method of correlation matrix to be used. Currently, it supports four methods, named \code{"circle"} (default), \code{"square"}, \code{"ellipse"}, \code{"number"}. 63 | #' @param type a character indicating that the \code{"full"} (default), \code{"upper"} or \code{"lower"} triangular matrix is displayed. 64 | #' @param col a vector of the colors to be used, which are distributed uniformly from -1 to 1. If NULL, col will be set to \code{RColorBrewer::brewer.pal(n = 11, name = "RdBu")}, the default colour scheme of \code{corrplot}. 65 | #' @param p.mat a matrix of p-value 66 | #' @param sig.lvl a numeric vector specifying significant level(s). If the p-value in \code{p.mat} is bigger than \code{sig.lvl} (0.05 by default), then the corresponding correlation coefficient is regarded as insignificant. If \code{insig} is \code{"label_sig"}, this may be an increasing vector of significance levels, for example \code{c(0.05, 0.01, 0.001)}, in which case \code{pch} will be used once for the highest p-value interval and multiple times (e.g. "*", "**", "***") for each lower p-value interval. 67 | #' @param number.digits the number of decimal digits (2 by default) while the visualization method is \code{"number"}. 68 | #' @param show.diag a logical indicating whether display the correlation coefficients on the principal diagonal. 69 | #' @param insig a character specialized insignificant correlation coefficients, \code{"pch"} (default), \code{"blank"}, or \code{"label_sig"}. 70 | #' @param pch a point character indicating the shape of insignificant correlation coefficients. 71 | #' @param pch.cex a number controlling the shape size of insignificant correlation coefficients. 72 | #' @export 73 | ggcorrplot <- function(corr, method = c("circle", "square", "ellipse", "number"), 74 | type = c("full", "lower", "upper"), p.mat = NULL, 75 | col = NULL, 76 | sig.lvl = 0.05, number.digits = 2, 77 | show.diag = TRUE, insig = c("pch", "blank", "label_sig"), 78 | pch = 4, pch.cex = 5) { 79 | method <- match.arg(method) 80 | type <- match.arg(type) 81 | insig <- match.arg(insig) 82 | 83 | if (is.null(colnames(corr))) { 84 | stop("Correlation matrix needs column names!") 85 | } 86 | vars <- colnames(corr) 87 | # number of variables 88 | nvars <- length(vars) 89 | 90 | corr <- reshape2::melt(corr, value.name = "rho") %>% 91 | mutate(rid = as.integer(as.factor(.data$Var1)), 92 | cid = as.integer(as.factor(.data$Var2))) %>% 93 | mutate(part = case_when( 94 | .$rid < .$cid ~ "upper", 95 | .$rid == .$cid ~ "diag", 96 | .$rid > .$cid ~ "lower" 97 | )) %>% 98 | mutate(abs.rho = abs(.data$rho)) %>% 99 | mutate(rho.label = ifelse(.data$rho == 1, .data$rho, format(round(.data$rho, digits = number.digits), 100 | nsmall = number.digits))) 101 | 102 | if (!is.null(p.mat)) { 103 | p.mat <- reshape2::melt(p.mat, value.name = "pval") %>% 104 | mutate(rid = as.integer(as.factor(.data$Var1)), 105 | cid = as.integer(as.factor(.data$Var2))) %>% 106 | mutate(part = case_when( 107 | .$rid < .$cid ~ "upper", 108 | .$rid == .$cid ~ "diag", 109 | .$rid > .$cid ~ "lower" 110 | )) 111 | 112 | sig.codes <- sapply(seq_along(sig.lvl), function(i) { 113 | # By default, mark significance with * 114 | if (!is.character(pch)) { 115 | pch <- "*" 116 | } 117 | paste(rep(pch, i), collapse = "") 118 | }) 119 | 120 | corr <- corr %>% 121 | mutate(pval = p.mat$pval) %>% 122 | mutate(signif = as.numeric(.data$pval <= max(sig.lvl))) %>% 123 | mutate(sig.codes = cut(.data$pval, breaks = c(sig.lvl, 0, 1), labels = c(rev(sig.codes), ""), 124 | include.lowest = TRUE)) 125 | 126 | # insignificant p value matrix 127 | p.mat.insig <- p.mat %>% 128 | dplyr::filter(.data$pval > max(sig.lvl)) 129 | 130 | if (insig == "blank") { 131 | corr <- corr %>% 132 | mutate(rho = .data$rho * signif) 133 | } 134 | } else { 135 | p.mat.insig <- NULL 136 | } 137 | 138 | if (type == "lower") { 139 | corr <- corr %>% 140 | dplyr::filter(.data$part != "upper") 141 | if (!is.null(p.mat.insig)) { 142 | p.mat.insig <- p.mat.insig %>% 143 | dplyr::filter(.data$part != "upper") 144 | } 145 | } else if(type == "upper") { 146 | corr <- corr %>% 147 | dplyr::filter(.data$part != "lower") 148 | if (!is.null(p.mat.insig)) { 149 | p.mat.insig <- p.mat.insig %>% 150 | dplyr::filter(.data$part != "lower") 151 | } 152 | } 153 | 154 | if (!show.diag) { 155 | corr <- corr %>% 156 | dplyr::filter(.data$part != "diag") 157 | } 158 | 159 | if(is.null(col)) { 160 | # default palette of corrplot 161 | col <- RColorBrewer::brewer.pal(n = 11, name = "RdBu") 162 | } 163 | col2 <- grDevices::colorRampPalette(col) 164 | 165 | p <- ggplot(data = corr) + 166 | geom_rect(mapping = aes(xmin = .data$cid - 0.5, xmax = .data$cid + 0.5, 167 | ymin = .data$rid - 0.5, ymax = .data$rid + 0.5), 168 | color = "grey92", fill = NA) + 169 | coord_fixed() + 170 | theme_bw() + 171 | theme(legend.margin = margin(0, unit = 'cm'), 172 | axis.title = element_blank(), 173 | axis.ticks = element_blank(), 174 | panel.border = element_blank(), 175 | panel.grid.major = element_blank(), 176 | panel.grid.minor = element_blank()) 177 | 178 | if (!show.diag & type == "full") { 179 | p <- p + 180 | geom_rect(mapping = aes(xmin = 1 - 0.5, xmax = 1 + 0.5, 181 | ymin = 1 - 0.5, ymax = 1 + 0.5), 182 | color = "grey92", fill = NA) + 183 | geom_rect(mapping = aes(xmin = nvars - 0.5, xmax = nvars + 0.5, 184 | ymin = nvars - 0.5, ymax = nvars + 0.5), 185 | color = "grey92", fill = NA) 186 | } 187 | 188 | if (any(is.na(corr$rho))) { 189 | corr.NA <- corr %>% 190 | dplyr::filter(is.na(.data$rho)) 191 | corr <- corr %>% 192 | dplyr::filter(!is.na(.data$rho)) 193 | } else { 194 | corr.NA <- NULL 195 | } 196 | 197 | p <- plot.method(p, data = corr, method = method) 198 | # add significant codes except number method 199 | if (!is.null(p.mat) & insig == "label_sig" & method != "number") { 200 | p <- p + 201 | geom_text(data = corr, mapping = aes(x = .data$cid, y = .data$rid), label = corr$sig.codes, 202 | size = pch.cex) 203 | } 204 | 205 | # indicate insigificant p value with point character 206 | if (!is.null(p.mat.insig) & insig == "pch") { 207 | p <- p + geom_point(data = p.mat.insig, mapping = aes(x = .data$cid, y = .data$rid), 208 | shape = pch, size = pch.cex) 209 | } 210 | 211 | # indicate NA correlation coefficient 212 | if (!is.null(corr.NA)) { 213 | p <- p + 214 | geom_text(data = corr.NA, mapping = aes(x = .data$cid, y = .data$rid), label = "NA", 215 | size = pch.cex, color = "grey92") 216 | } 217 | 218 | # colorbar 219 | if (type %in% c("full", "upper")) { 220 | if (method %in% c("ellipse", "circle", "square")) { 221 | p <- p + scale_fill_gradientn(colours = col2(200), limits = c(-1, 1), 222 | guide = guide_colorbar( 223 | title = "", 224 | nbin = 1000, 225 | ticks.colour = "black", 226 | frame.colour = "black", 227 | barwidth = 1.5, 228 | barheight = 15)) 229 | } else { 230 | p <- p + scale_colour_gradientn(colours = col2(200), limits = c(-1, 1), 231 | guide = guide_colorbar( 232 | title = "", 233 | nbin = 1000, 234 | ticks.colour = "black", 235 | frame.colour = "black", 236 | barwidth = 1.5, 237 | barheight = 15)) 238 | } 239 | } else if (type %in% c("lower")) { 240 | if (method %in% c("ellipse", "circle", "square")) { 241 | p <- p + scale_fill_gradientn(colours = col2(200), limits = c(-1, 1), 242 | guide = guide_colorbar( 243 | direction = "horizontal", 244 | title = "", 245 | nbin = 1000, 246 | ticks.colour = "black", 247 | frame.colour = "black", 248 | barwidth = 15, 249 | barheight = 1.5)) 250 | } else { 251 | p <- p + scale_colour_gradientn(colours = col2(200), limits = c(-1, 1), 252 | guide = guide_colorbar( 253 | direction = "horizontal", 254 | title = "", 255 | nbin = 1000, 256 | ticks.colour = "black", 257 | frame.colour = "black", 258 | barwidth = 15, 259 | barheight = 1.5)) 260 | } 261 | p <- p + theme(legend.position = "bottom") 262 | } 263 | 264 | # variable labels 265 | axis.text.fontsize <- 11 # in pt 266 | geom.text.fontsize <- axis.text.fontsize / ggplot2::.pt # in mm 267 | if (type == "full") { 268 | p <- p + 269 | scale_x_continuous(breaks = 1:nvars, labels = vars, expand = c(0, 0)) + 270 | scale_y_reverse(breaks = 1:nvars, labels = vars, expand = c(0, 0)) + 271 | theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 272 | } else if (type == "lower") { 273 | if (show.diag) { 274 | y.vars <- data.frame(x = 0, y = 1:nvars) 275 | diag.vars <- data.frame(x = 1:nvars, y = 0:(nvars - 1)) 276 | y.label <- diag.label <- vars 277 | } else { 278 | y.vars <- data.frame(x = 0, y = 2:nvars) 279 | diag.vars <- data.frame(x = 1:(nvars - 1), y = 1:(nvars - 1)) 280 | y.label <- utils::tail(vars, -1) 281 | diag.label <- utils::head(vars, -1) 282 | } 283 | p <- p + 284 | geom_text(data = diag.vars, mapping = aes(.data$x, .data$y+0.4), label = diag.label, 285 | colour = "grey30", size = geom.text.fontsize, angle = 90, hjust = 0) + 286 | scale_x_continuous(expand = c(0, 0)) + 287 | scale_y_reverse(breaks = y.vars$y, labels = y.label, 288 | expand = expansion(mult = c(0, 0.02*nchar(diag.label[1]))), 289 | limits = c(nvars + 0.5, min(diag.vars$y) - 0.5)) + 290 | theme(axis.text.x = element_blank(), 291 | axis.text.y = element_text(size = axis.text.fontsize)) 292 | } else if (type == "upper") { 293 | if (show.diag) { 294 | x.vars <- data.frame(x = 1:nvars, y = 0) 295 | diag.vars <- data.frame(x = 0:(nvars - 1), y = 1:nvars) 296 | x.label <- diag.label <- vars 297 | } else { 298 | x.vars <- data.frame(x = 2:nvars, y = 0) 299 | diag.vars <- data.frame(x = 1:(nvars - 1), y = 1:(nvars - 1)) 300 | x.label <- utils::tail(vars, -1) 301 | diag.label <- utils::head(vars, -1) 302 | } 303 | p <- p + 304 | geom_text(data = diag.vars, mapping = aes(.data$x+0.4, .data$y), label = diag.label, 305 | colour = "grey30", size = geom.text.fontsize, hjust = 1) + 306 | scale_x_continuous(breaks = x.vars$x, labels = x.label, 307 | expand = expansion(mult = c(0.02*nchar(diag.label[1]), 0)), 308 | position = "top", limits = c(min(diag.vars$x) - 0.5, nvars + 0.5)) + 309 | scale_y_reverse(expand = c(0, 0)) + 310 | theme(axis.text.x = element_text(size = axis.text.fontsize, angle = 90, hjust = 0), 311 | axis.text.y = element_blank()) 312 | } 313 | 314 | return(p) 315 | } 316 | -------------------------------------------------------------------------------- /R/ggcorrplot.mixed.R: -------------------------------------------------------------------------------- 1 | #' @title Visualize a correlation matrix using mixed methods 2 | #' 3 | #' @param corr a correlation matrix to be visualized 4 | #' @param upper a character indicating the visualization method of the upper triangular matrix to be used. Currently, it supports four methods, named \code{"circle"} (default), \code{"square"}, \code{"ellipse"}, \code{"number"}. 5 | #' @param lower a character indicating the visualization method of the lower triangular matrix to be used. Currently, it supports four methods, named \code{"circle"}, \code{"square"}, \code{"ellipse"}, \code{"number"}(default). 6 | #' @param col a vector of the colors to be used, which are distributed uniformly from -1 to 1. If NULL, col will be set to \code{RColorBrewer::brewer.pal(n = 11, name = "RdBu")}, the default colour scheme of \code{corrplot}. 7 | #' @param p.mat a matrix of p-value 8 | #' @param sig.lvl a numeric vector specifying significant level(s). If the p-value in \code{p.mat} is bigger than \code{sig.lvl} (0.05 by default), then the corresponding correlation coefficient is regarded as insignificant. If \code{insig} is \code{"label_sig"}, this may be an increasing vector of significance levels, for example \code{c(0.05, 0.01, 0.001)}, in which case \code{pch} will be used once for the highest p-value interval and multiple times (e.g. "*", "**", "***") for each lower p-value interval. 9 | #' @param number.digits the number of decimal digits (2 by default) while the visualization method is \code{"number"}. 10 | #' @param insig a character specialized insignificant correlation coefficients, \code{"pch"} (default), \code{"blank"}, or \code{"label_sig"}. 11 | #' @param pch a point character indicating the shape of insignificant correlation coefficients. 12 | #' @param pch.cex a number controlling the shape size of insignificant correlation coefficients. 13 | #' @export 14 | ggcorrplot.mixed <- function(corr, upper = c("circle", "square", "ellipse", "number"), 15 | lower = c("number", "square", "ellipse", "circle"), 16 | col = NULL, 17 | p.mat = NULL, sig.lvl = 0.05, number.digits = 2, 18 | insig = c("pch", "blank", "label_sig"), 19 | pch = 4, pch.cex = 5) { 20 | upper <- match.arg(upper) 21 | lower <- match.arg(lower) 22 | insig <- match.arg(insig) 23 | 24 | if (is.null(colnames(corr))) { 25 | stop("Correlation matrix needs column names!") 26 | } 27 | vars <- colnames(corr) 28 | # number of variables 29 | nvars <- length(vars) 30 | 31 | corr <- reshape2::melt(corr, value.name = "rho") %>% 32 | mutate(rid = as.integer(as.factor(.data$Var1)), 33 | cid = as.integer(as.factor(.data$Var2))) %>% 34 | mutate(part = case_when( 35 | .$rid < .$cid ~ "upper", 36 | .$rid == .$cid ~ "diag", 37 | .$rid > .$cid ~ "lower" 38 | )) %>% 39 | mutate(abs.rho = abs(.data$rho)) %>% 40 | mutate(rho.label = ifelse(.data$rho == 1, .data$rho, format(round(.data$rho, digits = number.digits), 41 | nsmall = number.digits))) 42 | 43 | if (!is.null(p.mat)) { 44 | p.mat <- reshape2::melt(p.mat, value.name = "pval") %>% 45 | mutate(rid = as.integer(as.factor(.data$Var1)), 46 | cid = as.integer(as.factor(.data$Var2))) %>% 47 | mutate(part = case_when( 48 | .$rid < .$cid ~ "upper", 49 | .$rid == .$cid ~ "diag", 50 | .$rid > .$cid ~ "lower" 51 | )) 52 | 53 | sig.codes <- sapply(seq_along(sig.lvl), function(i) { 54 | # By default, mark significance with * 55 | if (!is.character(pch)) { 56 | pch <- "*" 57 | } 58 | paste(rep(pch, i), collapse = "") 59 | }) 60 | 61 | corr <- corr %>% 62 | mutate(pval = p.mat$pval) %>% 63 | mutate(signif = as.numeric(.data$pval <= max(sig.lvl))) %>% 64 | mutate(sig.codes = cut(.data$pval, breaks = c(sig.lvl, 0, 1), labels = c(rev(sig.codes), ""), 65 | include.lowest = TRUE)) 66 | 67 | # insignificant p value matrix 68 | p.mat.insig <- p.mat %>% 69 | dplyr::filter(.data$pval > max(sig.lvl)) 70 | 71 | if (insig == "blank") { 72 | corr <- corr %>% 73 | mutate(rho = .data$rho * signif) 74 | } 75 | } else { 76 | p.mat.insig <- NULL 77 | } 78 | 79 | if(is.null(col)) { 80 | # default palette of corrplot 81 | col <- RColorBrewer::brewer.pal(n = 11, name = "RdBu") 82 | } 83 | col2 <- grDevices::colorRampPalette(col) 84 | 85 | 86 | p <- ggplot(data = corr) + 87 | geom_rect(mapping = aes(xmin = .data$cid - 0.5, xmax = .data$cid + 0.5, 88 | ymin = .data$rid - 0.5, ymax = .data$rid + 0.5), 89 | color = "grey92", fill = NA) + 90 | coord_fixed() + 91 | scale_x_continuous(expand = c(0, 0)) + 92 | scale_y_reverse(expand = c(0, 0)) + 93 | theme_bw() + 94 | theme(legend.margin = margin(0, unit = 'cm'), 95 | axis.text.x = element_blank(), 96 | axis.text.y = element_blank(), 97 | axis.title = element_blank(), 98 | axis.ticks = element_blank(), 99 | panel.border = element_blank(), 100 | panel.grid.major = element_blank(), 101 | panel.grid.minor = element_blank()) 102 | 103 | if (any(is.na(corr$rho))) { 104 | corr.NA <- corr %>% 105 | dplyr::filter(is.na(.data$rho)) 106 | corr <- corr %>% 107 | dplyr::filter(!is.na(.data$rho)) 108 | } else { 109 | corr.NA <- NULL 110 | } 111 | 112 | # add upper plot 113 | upper.dat <- corr %>% 114 | dplyr::filter(.data$part == "upper") 115 | 116 | p <- plot.method(p, data = upper.dat, method = upper) 117 | # add significant codes except number method 118 | if (!is.null(p.mat) & insig == "label_sig" & upper != "number") { 119 | p <- p + 120 | geom_text(data = upper.dat, mapping = aes(x = .data$cid, y = .data$rid), label = upper.dat$sig.codes, 121 | size = pch.cex) 122 | } 123 | 124 | # add lower plot 125 | lower.dat <- corr %>% 126 | dplyr::filter(.data$part == "lower") 127 | 128 | p <- plot.method(p, data = lower.dat, method = lower) 129 | # add significant codes except number method 130 | if (!is.null(p.mat) & insig == "label_sig" & lower != "number") { 131 | p <- p + 132 | geom_text(data = lower.dat, mapping = aes(x = .data$cid, y = .data$rid), label = upper.dat$sig.codes, 133 | size = pch.cex) 134 | } 135 | 136 | # indicate insigificant p value with point character 137 | if (!is.null(p.mat.insig) & insig == "pch") { 138 | p <- p + geom_point(data = p.mat.insig, mapping = aes(x = .data$cid, y = .data$rid), 139 | shape = pch, size = pch.cex) 140 | } 141 | 142 | # indicate NA correlation coefficient 143 | if (!is.null(corr.NA)) { 144 | p <- p + 145 | geom_text(data = corr.NA, mapping = aes(x = .data$cid, y = .data$rid), label = "NA", 146 | size = pch.cex, color = "grey92") 147 | } 148 | 149 | # colorbar 150 | p <- p + scale_fill_gradientn(colours = col2(200), limits = c(-1, 1), 151 | guide = guide_colorbar( 152 | title = "", 153 | nbin = 1000, 154 | ticks.colour = "black", 155 | frame.colour = "black", 156 | barwidth = 1.5, 157 | barheight = 15)) + 158 | scale_colour_gradientn(colours = col2(200), limits = c(-1, 1), 159 | guide = guide_colorbar( 160 | title = "", 161 | nbin = 1000, 162 | ticks.colour = "black", 163 | frame.colour = "black", 164 | barwidth = 1.5, 165 | barheight = 15)) 166 | 167 | diag.vars <- data.frame(x = 1:nvars, y = 1:nvars) 168 | diag.label <- vars 169 | p <- p + 170 | geom_text(data = diag.vars, mapping = aes(.data$x, .data$y), label = diag.label, colour = "grey30") 171 | 172 | return(p) 173 | } 174 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | # ggcorrplot2 6 | 7 | [![Build Status](https://travis-ci.org/caijun/ggcorrplot2.svg?branch=master)](https://travis-ci.org/caijun/ggcorrplot2) 8 | 9 | Implementation of corrplot using ggplot2 10 | 11 | ## Introduction 12 | 13 | Reinventing wheels is not what I like doing. [corrplot](https://CRAN.R-project.org/package=corrplot) is a great R package, but I am really tired of customizing the appearance of corrplot, for example, the space between colorbar and its tick labels, the space around the plot that I don't know how to control when writing it to PDF on my macOS. This is most likely because I am more familiar with the Grammar of Graphics implemented in ggplot2 than the base plotting system in R. There are several R packages (e.g., [ggcorrplot](https://github.com/kassambara/ggcorrplot) developed by Alboukadel Kassambara, [ggcorr](https://github.com/briatte/ggcorr) developed by François Briatte) that can visualize a correlation matrix into a [corrgram](https://www.tandfonline.com/doi/abs/10.1198/000313002533) using ggplot2; however, they are unable to visualize a correlation matrix using ellipse and mixed methods. **ggcorrplot2** has implemented only a subset of features of **corrplot** to meet my urgent needs. See examples in the **Getting started** section. More functionality will be added in the future as needed. 14 | 15 | ## Installation 16 | 17 | Get the development version from github: 18 | 19 | ```r 20 | if (!requireNamespace("devtools")) install.packages("devtools") 21 | devtools::install_github("caijun/ggcorrplot2") 22 | ``` 23 | 24 | ## Getting started 25 | 26 | The `mtcars` dataset will be used to demonstrate the usages of **ggcorrplot2**. Most parameters of **ggcorrplot2** functions are the same as those of **corrplot**. Therefore, it's easy for users to migrate from **corrplot** to **ggcorrplot2**. 27 | 28 | ```{r, echo = FALSE} 29 | knitr::opts_chunk$set( 30 | dpi = 200, 31 | fig.path = "figs/README-" 32 | ) 33 | ``` 34 | 35 | ```{r, fig.height=4} 36 | library(ggcorrplot2) 37 | 38 | data(mtcars) 39 | # Use corr.test() from psych package to calculate the correlation matrix and 40 | # corresponding p value matrix without adjustment. 41 | library(psych) 42 | ct <- corr.test(mtcars, adjust = "none") 43 | corr <- ct$r 44 | p.mat <- ct$p 45 | 46 | # Visualize the correlation matrix 47 | # -------------------------------- 48 | # method = "circle" (default) 49 | ggcorrplot(corr) 50 | 51 | # method = "square" 52 | ggcorrplot(corr, method = "square") 53 | 54 | # method = "ellipse" 55 | ggcorrplot(corr, method = "ellipse") 56 | 57 | # method = "number", display the correlation coefficients 58 | ggcorrplot(corr, method = "number") 59 | 60 | # Visualize the upper or lower triangle of correlation matrix 61 | # ----------------------------------------------------------- 62 | # the upper triangle 63 | ggcorrplot(corr, type = "upper") 64 | 65 | # the lower triangle 66 | ggcorrplot(corr, type = "lower") 67 | 68 | # Visualize the correlation matrix using mixed methods 69 | # ---------------------------------------------------- 70 | # default: upper = "circle", lower = "number" 71 | ggcorrplot.mixed(corr) 72 | 73 | # upper = "ellipse", lower = "number" 74 | ggcorrplot.mixed(corr, upper = "ellipse", lower = "number") 75 | 76 | # Combine correlogram with the significance test 77 | # ---------------------------------------------- 78 | # Insignificant coefficients according to the default significant level 79 | # (sig.lvl = 0.05) are indicated by X by default. 80 | ggcorrplot.mixed(corr, upper = "ellipse", lower = "number", p.mat = p.mat) 81 | 82 | # Leave blank on insignificant coefficients 83 | ggcorrplot.mixed(corr, upper = "ellipse", lower = "number", p.mat = p.mat, 84 | insig = "blank") 85 | 86 | # Label significant coefficients with asterisks (*, default) denoting the significance level 87 | ggcorrplot.mixed(corr, upper = "ellipse", lower = "number", p.mat = p.mat, 88 | insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001)) 89 | 90 | # Label significant coefficients with varying number of + denoting the significance level 91 | (p <- ggcorrplot.mixed(corr, upper = "ellipse", lower = "number", p.mat = p.mat, 92 | insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001), pch = "+", 93 | pch.cex = 4)) 94 | ``` 95 | 96 | The above examples reproduce some features of **corrplot**. In the following example, the added advantages of implementing **corrplot** using ggplot2, such as customizing the appearance of corrgram, combining a corrgram with other plots (including non-corrgrams) into one plot using [cowplot](https://github.com/wilkelab/cowplot), are demonstrated. 97 | 98 | ```{r, fig.width=9, fig.height=5, fig.align="center", message=FALSE} 99 | # Customize the appearance of corrplot using functions from ggplot2 100 | library(ggplot2) 101 | # Use different color palette 102 | col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white", 103 | "cyan", "#007FFF", "blue", "#00007F")) 104 | # Change the colorbar direction to horizontal and place it at the bottom 105 | # As mixed methods are used, there are two scales: color filled in ellipse and 106 | # number color 107 | p <- p + scale_fill_gradientn(colours = col1(10), limits = c(-1, 1), 108 | guide = guide_colorbar( 109 | direction = "horizontal", 110 | title = "", 111 | nbin = 1000, 112 | ticks.colour = "black", 113 | frame.colour = "black", 114 | barwidth = 15, 115 | barheight = 1.5)) + 116 | scale_colour_gradientn(colours = col1(10), limits = c(-1, 1), 117 | guide = guide_colorbar( 118 | direction = "horizontal", 119 | title = "", 120 | nbin = 1000, 121 | ticks.colour = "black", 122 | frame.colour = "black", 123 | barwidth = 15, 124 | barheight = 1.5)) + 125 | theme(legend.position = "bottom") 126 | p 127 | 128 | # Combine a lower corrgram and a mixed corrgram side by side with a shared colorbar on the bottom 129 | 130 | # NAs are allowed in correlation matrix and p value matrix, which are labelled as NA 131 | # Assign NAs 132 | rid <- c(2, 3, 1, 2) 133 | cid <- c(3, 2, 2, 1) 134 | pos <- cbind(rid, cid) 135 | corr[pos] <- NA 136 | p.mat[pos] <- NA 137 | 138 | # a lower corrgram 139 | p1 <- ggcorrplot(corr, type = "lower", method = "square", p.mat = p.mat, 140 | insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001), show.diag = FALSE) 141 | # a mixed corrgram 142 | p2 <- ggcorrplot.mixed(corr, upper = "ellipse", lower = "number", p.mat = p.mat, 143 | insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001), 144 | pch = "+", pch.cex = 4) 145 | 146 | library(cowplot) 147 | prow <- plot_grid(p1 + theme(legend.position = "none"), 148 | p2 + theme(legend.position = "none"), 149 | rel_widths = c(1, 1), nrow = 1, align = 'hv', 150 | labels = c("(a)", "(b)"), label_x = 0, label_y = 1) 151 | 152 | # Extract the legend from the first corrgram 153 | legend <- get_legend(p1) 154 | # Add the legend to the bottom of the plot row we made earlier. 155 | p <- cowplot::plot_grid(prow, legend, ncol = 1, rel_heights = c(1, 0.15)) 156 | p 157 | ``` 158 | 159 | ## Citation 160 | 161 | To cite the 'ggcorrplot2' package in publications use: 162 | ``` 163 | Jun Cai, Granville Matheson and Samson Leonard Daniël (2022). ggcorrplot2: Visualize a Correlation Matrix using ggplot2. R package version 0.1.2. 164 | ``` 165 | 166 | A BibTeX entry for LaTeX users is 167 | ``` 168 | @Manual{, 169 | title = {ggcorrplot2: Visualize a Correlation Matrix using ggplot2}, 170 | author = {Jun Cai and Granville Matheson and Samson Leonard Daniël}, 171 | year = {2022}, 172 | note = {R package version 0.1.2}, 173 | url = {https://github.com/caijun/ggcorrplot2}, 174 | } 175 | ``` 176 | 177 | The above citation information can be generated by calling `citation("ggcorrplot2")` in R. 178 | 179 | ## Contact 180 | 181 | Bugs and feature requests can be filed to 182 | . Pull requests are also welcome. 183 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # ggcorrplot2 3 | 4 | [![Build 5 | Status](https://travis-ci.org/caijun/ggcorrplot2.svg?branch=master)](https://travis-ci.org/caijun/ggcorrplot2) 6 | 7 | Implementation of corrplot using ggplot2 8 | 9 | ## Introduction 10 | 11 | Reinventing wheels is not what I like doing. 12 | [corrplot](https://CRAN.R-project.org/package=corrplot) is a great R 13 | package, but I am really tired of customizing the appearance of 14 | corrplot, for example, the space between colorbar and its tick labels, 15 | the space around the plot that I don’t know how to control when writing 16 | it to PDF on my macOS. This is most likely because I am more familiar 17 | with the Grammar of Graphics implemented in ggplot2 than the base 18 | plotting system in R. There are several R packages (e.g., 19 | [ggcorrplot](https://github.com/kassambara/ggcorrplot) developed by 20 | Alboukadel Kassambara, [ggcorr](https://github.com/briatte/ggcorr) 21 | developed by François Briatte) that can visualize a correlation matrix 22 | into a 23 | [corrgram](https://www.tandfonline.com/doi/abs/10.1198/000313002533) 24 | using ggplot2; however, they are unable to visualize a correlation 25 | matrix using ellipse and mixed methods. **ggcorrplot2** has implemented 26 | only a subset of features of **corrplot** to meet my urgent needs. See 27 | examples in the **Getting started** section. More functionality will be 28 | added in the future as needed. 29 | 30 | ## Installation 31 | 32 | Get the development version from github: 33 | 34 | ``` r 35 | if (!requireNamespace("devtools")) install.packages("devtools") 36 | devtools::install_github("caijun/ggcorrplot2") 37 | ``` 38 | 39 | ## Getting started 40 | 41 | The `mtcars` dataset will be used to demonstrate the usages of 42 | **ggcorrplot2**. Most parameters of **ggcorrplot2** functions are the 43 | same as those of **corrplot**. Therefore, it’s easy for users to migrate 44 | from **corrplot** to **ggcorrplot2**. 45 | 46 | ``` r 47 | library(ggcorrplot2) 48 | 49 | data(mtcars) 50 | # Use corr.test() from psych package to calculate the correlation matrix and 51 | # corresponding p value matrix without adjustment. 52 | library(psych) 53 | ct <- corr.test(mtcars, adjust = "none") 54 | corr <- ct$r 55 | p.mat <- ct$p 56 | 57 | # Visualize the correlation matrix 58 | # -------------------------------- 59 | # method = "circle" (default) 60 | ggcorrplot(corr) 61 | ``` 62 | 63 | ![](figs/README-unnamed-chunk-2-1.png) 64 | 65 | ``` r 66 | # method = "square" 67 | ggcorrplot(corr, method = "square") 68 | ``` 69 | 70 | ![](figs/README-unnamed-chunk-2-2.png) 71 | 72 | ``` r 73 | # method = "ellipse" 74 | ggcorrplot(corr, method = "ellipse") 75 | ``` 76 | 77 | ![](figs/README-unnamed-chunk-2-3.png) 78 | 79 | ``` r 80 | # method = "number", display the correlation coefficients 81 | ggcorrplot(corr, method = "number") 82 | ``` 83 | 84 | ![](figs/README-unnamed-chunk-2-4.png) 85 | 86 | ``` r 87 | # Visualize the upper or lower triangle of correlation matrix 88 | # ----------------------------------------------------------- 89 | # the upper triangle 90 | ggcorrplot(corr, type = "upper") 91 | ``` 92 | 93 | ![](figs/README-unnamed-chunk-2-5.png) 94 | 95 | ``` r 96 | # the lower triangle 97 | ggcorrplot(corr, type = "lower") 98 | ``` 99 | 100 | ![](figs/README-unnamed-chunk-2-6.png) 101 | 102 | ``` r 103 | # Visualize the correlation matrix using mixed methods 104 | # ---------------------------------------------------- 105 | # default: upper = "circle", lower = "number" 106 | ggcorrplot.mixed(corr) 107 | ``` 108 | 109 | ![](figs/README-unnamed-chunk-2-7.png) 110 | 111 | ``` r 112 | # upper = "ellipse", lower = "number" 113 | ggcorrplot.mixed(corr, upper = "ellipse", lower = "number") 114 | ``` 115 | 116 | ![](figs/README-unnamed-chunk-2-8.png) 117 | 118 | ``` r 119 | # Combine correlogram with the significance test 120 | # ---------------------------------------------- 121 | # Insignificant coefficients according to the default significant level 122 | # (sig.lvl = 0.05) are indicated by X by default. 123 | ggcorrplot.mixed(corr, upper = "ellipse", lower = "number", p.mat = p.mat) 124 | ``` 125 | 126 | ![](figs/README-unnamed-chunk-2-9.png) 127 | 128 | ``` r 129 | # Leave blank on insignificant coefficients 130 | ggcorrplot.mixed(corr, upper = "ellipse", lower = "number", p.mat = p.mat, 131 | insig = "blank") 132 | ``` 133 | 134 | ![](figs/README-unnamed-chunk-2-10.png) 135 | 136 | ``` r 137 | # Label significant coefficients with asterisks (*, default) denoting the significance level 138 | ggcorrplot.mixed(corr, upper = "ellipse", lower = "number", p.mat = p.mat, 139 | insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001)) 140 | ``` 141 | 142 | ![](figs/README-unnamed-chunk-2-11.png) 143 | 144 | ``` r 145 | # Label significant coefficients with varying number of + denoting the significance level 146 | (p <- ggcorrplot.mixed(corr, upper = "ellipse", lower = "number", p.mat = p.mat, 147 | insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001), pch = "+", 148 | pch.cex = 4)) 149 | ``` 150 | 151 | ![](figs/README-unnamed-chunk-2-12.png) 152 | 153 | The above examples reproduce some features of **corrplot**. In the 154 | following example, the added advantages of implementing **corrplot** 155 | using ggplot2, such as customizing the appearance of corrgram, combining 156 | a corrgram with other plots (including non-corrgrams) into one plot 157 | using [cowplot](https://github.com/wilkelab/cowplot), are demonstrated. 158 | 159 | ``` r 160 | # Customize the appearance of corrplot using functions from ggplot2 161 | library(ggplot2) 162 | # Use different color palette 163 | col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white", 164 | "cyan", "#007FFF", "blue", "#00007F")) 165 | # Change the colorbar direction to horizontal and place it at the bottom 166 | # As mixed methods are used, there are two scales: color filled in ellipse and 167 | # number color 168 | p <- p + scale_fill_gradientn(colours = col1(10), limits = c(-1, 1), 169 | guide = guide_colorbar( 170 | direction = "horizontal", 171 | title = "", 172 | nbin = 1000, 173 | ticks.colour = "black", 174 | frame.colour = "black", 175 | barwidth = 15, 176 | barheight = 1.5)) + 177 | scale_colour_gradientn(colours = col1(10), limits = c(-1, 1), 178 | guide = guide_colorbar( 179 | direction = "horizontal", 180 | title = "", 181 | nbin = 1000, 182 | ticks.colour = "black", 183 | frame.colour = "black", 184 | barwidth = 15, 185 | barheight = 1.5)) + 186 | theme(legend.position = "bottom") 187 | p 188 | ``` 189 | 190 | 191 | 192 | ``` r 193 | # Combine a lower corrgram and a mixed corrgram side by side with a shared colorbar on the bottom 194 | 195 | # NAs are allowed in correlation matrix and p value matrix, which are labelled as NA 196 | # Assign NAs 197 | rid <- c(2, 3, 1, 2) 198 | cid <- c(3, 2, 2, 1) 199 | pos <- cbind(rid, cid) 200 | corr[pos] <- NA 201 | p.mat[pos] <- NA 202 | 203 | # a lower corrgram 204 | p1 <- ggcorrplot(corr, type = "lower", method = "square", p.mat = p.mat, 205 | insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001), show.diag = FALSE) 206 | # a mixed corrgram 207 | p2 <- ggcorrplot.mixed(corr, upper = "ellipse", lower = "number", p.mat = p.mat, 208 | insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001), 209 | pch = "+", pch.cex = 4) 210 | 211 | library(cowplot) 212 | prow <- plot_grid(p1 + theme(legend.position = "none"), 213 | p2 + theme(legend.position = "none"), 214 | rel_widths = c(1, 1), nrow = 1, align = 'hv', 215 | labels = c("(a)", "(b)"), label_x = 0, label_y = 1) 216 | 217 | # Extract the legend from the first corrgram 218 | legend <- get_legend(p1) 219 | # Add the legend to the bottom of the plot row we made earlier. 220 | p <- cowplot::plot_grid(prow, legend, ncol = 1, rel_heights = c(1, 0.15)) 221 | p 222 | ``` 223 | 224 | 225 | 226 | ## Citation 227 | 228 | To cite the ‘ggcorrplot2’ package in publications use: 229 | 230 | Jun Cai, Granville Matheson and Samson Leonard Daniël (2022). ggcorrplot2: Visualize a Correlation Matrix using ggplot2. R package version 0.1.2. 231 | 232 | A BibTeX entry for LaTeX users is 233 | 234 | @Manual{, 235 | title = {ggcorrplot2: Visualize a Correlation Matrix using ggplot2}, 236 | author = {Jun Cai and Granville Matheson and Samson Leonard Daniël}, 237 | year = {2022}, 238 | note = {R package version 0.1.2}, 239 | url = {https://github.com/caijun/ggcorrplot2}, 240 | } 241 | 242 | The above citation information can be generated by calling 243 | `citation("ggcorrplot2")` in R. 244 | 245 | ## Contact 246 | 247 | Bugs and feature requests can be filed to 248 | . Pull requests are also 249 | welcome. 250 | -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-1.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-10.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-11.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-12.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-2.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-3.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-4.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-5.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-6.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-7.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-8.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-2-9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-2-9.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /figs/README-unnamed-chunk-3-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caijun/ggcorrplot2/e77127b2251856db0a794a4b3a5ca90e3802b14d/figs/README-unnamed-chunk-3-2.png -------------------------------------------------------------------------------- /ggcorrplot2.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: XeLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | citHeader("To cite the 'ggcorrplot2' package in publications use:") 2 | 3 | year = sub('.*(2[[:digit:]]{3})-.*', '\\1', meta$Date, perl = TRUE) 4 | vers = paste('R package version', meta$Version) 5 | 6 | citEntry( 7 | entry = 'manual', 8 | title = paste('ggcorrplot2:', meta$Title), 9 | author = personList(as.person(Filter(function(p) 'aut' %in% p$role, as.person(meta$Author))), 10 | as.person(Filter(function(p) 'ctb' %in% p$role, as.person(meta$Author)))), 11 | year = year, 12 | note = vers, 13 | url = meta$URL, 14 | textVersion = paste('Jun Cai, Granville Matheson and Samson Leonard Daniël (', year, '). ggcorrplot2: ', meta$Title, '. ', vers, '.', sep = '') 15 | ) 16 | -------------------------------------------------------------------------------- /man/ggcorrplot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ggcorrplot.R 3 | \name{ggcorrplot} 4 | \alias{ggcorrplot} 5 | \title{Visualize a correlation matrix} 6 | \usage{ 7 | ggcorrplot( 8 | corr, 9 | method = c("circle", "square", "ellipse", "number"), 10 | type = c("full", "lower", "upper"), 11 | p.mat = NULL, 12 | col = NULL, 13 | sig.lvl = 0.05, 14 | number.digits = 2, 15 | show.diag = TRUE, 16 | insig = c("pch", "blank", "label_sig"), 17 | pch = 4, 18 | pch.cex = 5 19 | ) 20 | } 21 | \arguments{ 22 | \item{corr}{a correlation matrix to be visualized} 23 | 24 | \item{method}{a character indicating the visualization method of correlation matrix to be used. Currently, it supports four methods, named \code{"circle"} (default), \code{"square"}, \code{"ellipse"}, \code{"number"}.} 25 | 26 | \item{type}{a character indicating that the \code{"full"} (default), \code{"upper"} or \code{"lower"} triangular matrix is displayed.} 27 | 28 | \item{p.mat}{a matrix of p-value} 29 | 30 | \item{col}{a vector of the colors to be used, which are distributed uniformly from -1 to 1. If NULL, col will be set to \code{RColorBrewer::brewer.pal(n = 11, name = "RdBu")}, the default colour scheme of \code{corrplot}.} 31 | 32 | \item{sig.lvl}{a numeric vector specifying significant level(s). If the p-value in \code{p.mat} is bigger than \code{sig.lvl} (0.05 by default), then the corresponding correlation coefficient is regarded as insignificant. If \code{insig} is \code{"label_sig"}, this may be an increasing vector of significance levels, for example \code{c(0.05, 0.01, 0.001)}, in which case \code{pch} will be used once for the highest p-value interval and multiple times (e.g. "*", "**", "***") for each lower p-value interval.} 33 | 34 | \item{number.digits}{the number of decimal digits (2 by default) while the visualization method is \code{"number"}.} 35 | 36 | \item{show.diag}{a logical indicating whether display the correlation coefficients on the principal diagonal.} 37 | 38 | \item{insig}{a character specialized insignificant correlation coefficients, \code{"pch"} (default), \code{"blank"}, or \code{"label_sig"}.} 39 | 40 | \item{pch}{a point character indicating the shape of insignificant correlation coefficients.} 41 | 42 | \item{pch.cex}{a number controlling the shape size of insignificant correlation coefficients.} 43 | } 44 | \description{ 45 | Visualize a correlation matrix 46 | } 47 | -------------------------------------------------------------------------------- /man/ggcorrplot.mixed.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ggcorrplot.mixed.R 3 | \name{ggcorrplot.mixed} 4 | \alias{ggcorrplot.mixed} 5 | \title{Visualize a correlation matrix using mixed methods} 6 | \usage{ 7 | ggcorrplot.mixed( 8 | corr, 9 | upper = c("circle", "square", "ellipse", "number"), 10 | lower = c("number", "square", "ellipse", "circle"), 11 | col = NULL, 12 | p.mat = NULL, 13 | sig.lvl = 0.05, 14 | number.digits = 2, 15 | insig = c("pch", "blank", "label_sig"), 16 | pch = 4, 17 | pch.cex = 5 18 | ) 19 | } 20 | \arguments{ 21 | \item{corr}{a correlation matrix to be visualized} 22 | 23 | \item{upper}{a character indicating the visualization method of the upper triangular matrix to be used. Currently, it supports four methods, named \code{"circle"} (default), \code{"square"}, \code{"ellipse"}, \code{"number"}.} 24 | 25 | \item{lower}{a character indicating the visualization method of the lower triangular matrix to be used. Currently, it supports four methods, named \code{"circle"}, \code{"square"}, \code{"ellipse"}, \code{"number"}(default).} 26 | 27 | \item{col}{a vector of the colors to be used, which are distributed uniformly from -1 to 1. If NULL, col will be set to \code{RColorBrewer::brewer.pal(n = 11, name = "RdBu")}, the default colour scheme of \code{corrplot}.} 28 | 29 | \item{p.mat}{a matrix of p-value} 30 | 31 | \item{sig.lvl}{a numeric vector specifying significant level(s). If the p-value in \code{p.mat} is bigger than \code{sig.lvl} (0.05 by default), then the corresponding correlation coefficient is regarded as insignificant. If \code{insig} is \code{"label_sig"}, this may be an increasing vector of significance levels, for example \code{c(0.05, 0.01, 0.001)}, in which case \code{pch} will be used once for the highest p-value interval and multiple times (e.g. "*", "**", "***") for each lower p-value interval.} 32 | 33 | \item{number.digits}{the number of decimal digits (2 by default) while the visualization method is \code{"number"}.} 34 | 35 | \item{insig}{a character specialized insignificant correlation coefficients, \code{"pch"} (default), \code{"blank"}, or \code{"label_sig"}.} 36 | 37 | \item{pch}{a point character indicating the shape of insignificant correlation coefficients.} 38 | 39 | \item{pch.cex}{a number controlling the shape size of insignificant correlation coefficients.} 40 | } 41 | \description{ 42 | Visualize a correlation matrix using mixed methods 43 | } 44 | --------------------------------------------------------------------------------