Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ggtheme also accepts objects of 'theme' type #11

Merged
merged 1 commit into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ Suggests:
knitr
URL: http://www.sthda.com/english/wiki/ggcorrplot
BugReports: https://github.com/kassambara/ggcorrplot/issues
RoxygenNote: 5.0.1
RoxygenNote: 6.1.0.9000
137 changes: 85 additions & 52 deletions R/ggcorrplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#' @param method character, the visualization method of correlation matrix to be
#' used. Allowed values are "square" (default), "circle".
#' @param type character, "full" (default), "lower" or "upper" display.
#' @param ggtheme function, ggplot2 theme name. Default value is theme_minimal.
#' Allowed values are the official ggplot2 themes including theme_gray,
#' theme_bw, theme_minimal, theme_classic, theme_void, ....
#' @param ggtheme ggplot2 function or theme object. Default value is
#' `theme_minimal`. Allowed values are the official ggplot2 themes including
#' theme_gray, theme_bw, theme_minimal, theme_classic, theme_void, .... Theme
#' objects are also allowed (e.g., `theme_classic()`).
#' @param title character, title of the graph.
#' @param show.legend logical, if TRUE the legend is displayed.
#' @param legend.title a character string for the legend title. lower
Expand Down Expand Up @@ -68,63 +69,76 @@
#' # Types of correlogram layout
#' # --------------------------------
#' # Get the lower triangle
#' ggcorrplot(corr, hc.order = TRUE, type = "lower",
#' outline.col = "white")
#' ggcorrplot(corr,
#' hc.order = TRUE, type = "lower",
#' outline.col = "white"
#' )
#' # Get the upeper triangle
#' ggcorrplot(corr, hc.order = TRUE, type = "upper",
#' outline.col = "white")
#' ggcorrplot(corr,
#' hc.order = TRUE, type = "upper",
#' outline.col = "white"
#' )
#'
#' # Change colors and theme
#' # --------------------------------
#' # Argument colors
#' ggcorrplot(corr, hc.order = TRUE, type = "lower",
#' outline.col = "white",
#' ggtheme = ggplot2::theme_gray,
#' colors = c("#6D9EC1", "white", "#E46726"))
#' ggcorrplot(corr,
#' hc.order = TRUE, type = "lower",
#' outline.col = "white",
#' ggtheme = ggplot2::theme_gray,
#' colors = c("#6D9EC1", "white", "#E46726")
#' )
#'
#' # Add correlation coefficients
#' # --------------------------------
#' # argument lab = TRUE
#' ggcorrplot(corr, hc.order = TRUE, type = "lower",
#' lab = TRUE)
#' ggcorrplot(corr,
#' hc.order = TRUE, type = "lower",
#' lab = TRUE,
#' ggtheme = ggplot2::theme_dark(),
#' )
#'
#' # Add correlation significance level
#' # --------------------------------
#' # Argument p.mat
#' # Barring the no significant coefficient
#' ggcorrplot(corr, hc.order = TRUE,
#' type = "lower", p.mat = p.mat)
#' ggcorrplot(corr,
#' hc.order = TRUE,
#' type = "lower", p.mat = p.mat
#' )
#' # Leave blank on no significant coefficient
#' ggcorrplot(corr, p.mat = p.mat, hc.order = TRUE,
#' type = "lower", insig = "blank")
#'
#' ggcorrplot(corr,
#' p.mat = p.mat, hc.order = TRUE,
#' type = "lower", insig = "blank"
#' )
#' @name ggcorrplot
#' @rdname ggcorrplot
#' @export
ggcorrplot <- function (corr, method = c("square", "circle"),
type = c("full", "lower", "upper"),
ggtheme = ggplot2::theme_minimal,
title = "", show.legend = TRUE, legend.title = "Corr", show.diag = FALSE,
colors = c("blue", "white", "red"), outline.color = "gray",
hc.order = FALSE, hc.method = "complete",
lab = FALSE, lab_col = "black", lab_size = 4,
p.mat = NULL, sig.level = 0.05, insig = c("pch", "blank"),
pch = 4, pch.col = "black", pch.cex = 5,
tl.cex = 12, tl.col = "black", tl.srt = 45) {

ggcorrplot <- function(corr, method = c("square", "circle"),
type = c("full", "lower", "upper"),
ggtheme = ggplot2::theme_minimal,
title = "", show.legend = TRUE, legend.title = "Corr", show.diag = FALSE,
colors = c("blue", "white", "red"), outline.color = "gray",
hc.order = FALSE, hc.method = "complete",
lab = FALSE, lab_col = "black", lab_size = 4,
p.mat = NULL, sig.level = 0.05, insig = c("pch", "blank"),
pch = 4, pch.col = "black", pch.cex = 5,
tl.cex = 12, tl.col = "black", tl.srt = 45) {
type <- match.arg(type)
method <- match.arg(method)
insig <- match.arg(insig)

if (!is.matrix(corr) & !is.data.frame(corr))
if (!is.matrix(corr) & !is.data.frame(corr)) {
stop("Need a matrix or data frame!")
}
corr <- as.matrix(corr)

if (hc.order) {
ord <- .hc_cormat_order(corr)
corr <- corr[ord, ord]
if (!is.null(p.mat))
if (!is.null(p.mat)) {
p.mat <- p.mat[ord, ord]
}
}

# Get lower or upper triangle
Expand All @@ -145,32 +159,44 @@ ggcorrplot <- function (corr, method = c("square", "circle"),
p.mat <- reshape2::melt(p.mat, na.rm = TRUE)
corr$coef <- corr$value
corr$pvalue <- p.mat$value
corr$signif <- as.numeric(p.mat$value <= sig.level)
corr$signif <- as.numeric(p.mat$value <= sig.level)
p.mat <- subset(p.mat, p.mat$value > sig.level)
if (insig == "blank")
if (insig == "blank") {
corr$value <- corr$value * corr$signif
}
}

corr$abs_corr <- abs(corr$value) * 10

# Heatmap
p <-
ggplot2::ggplot(corr, ggplot2::aes_string("Var1", "Var2", fill = "value"))
if (method == "square")
if (method == "square") {
p <- p + ggplot2::geom_tile(color = outline.color)
else if (method == "circle") {
p <- p + ggplot2::geom_point(color = outline.color,
shape = 21, ggplot2::aes_string(size = "abs_corr")) +
} else if (method == "circle") {
p <- p + ggplot2::geom_point(
color = outline.color,
shape = 21, ggplot2::aes_string(size = "abs_corr")
) +
ggplot2::scale_size(range = c(4, 10)) + ggplot2::guides(size = FALSE)
}

p <-
p + ggplot2::scale_fill_gradient2(
low = colors[1], high = colors[3], mid = colors[2],
midpoint = 0, limit = c(-1,1), space = "Lab",
midpoint = 0, limit = c(-1, 1), space = "Lab",
name = legend.title
) +
ggtheme() +
)

# depending on the class of the object, add the specified theme
if (class(ggtheme)[[1]] == "function") {
p <- p + ggtheme()
} else if (class(ggtheme)[[1]] == "theme") {
p <- p + ggtheme
}


p <- p +
ggplot2::theme(
axis.text.x = ggplot2::element_text(
angle = tl.srt, vjust = 1, size = tl.cex, hjust = 1
Expand All @@ -180,13 +206,14 @@ ggcorrplot <- function (corr, method = c("square", "circle"),
ggplot2::coord_fixed()

label <- round(corr[, "value"], 2)
if (lab)
if (lab) {
p <- p +
ggplot2::geom_text(
ggplot2::aes_string("Var1", "Var2"),
label = label,
color = lab_col, size = lab_size
)
ggplot2::geom_text(
ggplot2::aes_string("Var1", "Var2"),
label = label,
color = lab_col, size = lab_size
)
}

if (!is.null(p.mat) & insig == "pch") {
p <- p + ggplot2::geom_point(
Expand All @@ -197,10 +224,12 @@ ggcorrplot <- function (corr, method = c("square", "circle"),
}

# Add titles
if (title != "")
if (title != "") {
p <- p + ggplot2::ggtitle(title)
if (!show.legend)
}
if (!show.legend) {
p <- p + ggplot2::theme(legend.position = "none")
}


p <- p + .no_panel()
Expand Down Expand Up @@ -239,21 +268,25 @@ cor_pmat <- function(x, ...) {

# Get lower triangle of the correlation matrix
.get_lower_tri <- function(cormat, show.diag = FALSE) {
if (is.null(cormat))
if (is.null(cormat)) {
return(cormat)
}
cormat[upper.tri(cormat)] <- NA
if (!show.diag)
if (!show.diag) {
diag(cormat) <- NA
}
return(cormat)
}

# Get upper triangle of the correlation matrix
.get_upper_tri <- function(cormat, show.diag = FALSE) {
if (is.null(cormat))
if (is.null(cormat)) {
return(cormat)
}
cormat[lower.tri(cormat)] <- NA
if (!show.diag)
if (!show.diag) {
diag(cormat) <- NA
}
return(cormat)
}

Expand Down
7 changes: 4 additions & 3 deletions ggcorrplot.Rproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: No

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
Expand All @@ -18,3 +18,4 @@ StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
62 changes: 36 additions & 26 deletions man/ggcorrplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.