## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE,
  dpi = 150,
  fig.align = "center",
  out.width = "80%"
)

## -----------------------------------------------------------------------------
library(ggcorrplot)
library(ggplot2)

data(mtcars)
corr <- round(cor(mtcars), 1)
p.mat <- cor_pmat(mtcars)

## ----clustered, fig.width = 6, fig.height = 5.4-------------------------------
ggcorrplot(corr, hc.order = TRUE, outline.color = "white")

## ----lower-lab, fig.width = 6, fig.height = 5.4-------------------------------
ggcorrplot(corr, hc.order = TRUE, type = "lower", lab = TRUE, lab_size = 3)

## ----significance, fig.show = "hold", out.width = "48%", fig.align = "default", fig.width = 5.4, fig.height = 5----
# non-significant cells left blank
ggcorrplot(corr, hc.order = TRUE, type = "lower", p.mat = p.mat, insig = "blank")
# significant cells starred
ggcorrplot(corr, p.mat = p.mat, insig = "stars")

## ----circle, fig.width = 6, fig.height = 5.4----------------------------------
ggcorrplot(corr, method = "circle", hc.order = TRUE, type = "upper", outline.color = "white")

## ----scale-square, fig.width = 6, fig.height = 5.4----------------------------
ggcorrplot(corr, scale.square = TRUE, hc.order = TRUE, outline.color = "white")

## ----cell-grid, fig.show = "hold", out.width = "48%", fig.align = "default", fig.width = 5.4, fig.height = 5----
# size-scaled squares in boxed cells
ggcorrplot(corr, scale.square = TRUE, cell.grid = TRUE, hc.order = TRUE, outline.color = "white")
# circles in boxed cells
ggcorrplot(corr, method = "circle", cell.grid = TRUE, hc.order = TRUE)

## ----palette, fig.show = "hold", out.width = "48%", fig.align = "default", fig.width = 5.4, fig.height = 5----
# one-token publication preset
ggcorrplot(corr, hc.order = TRUE, preset = "publication")
# a custom diverging palette on a minimal theme
ggcorrplot(corr,
  hc.order = TRUE, type = "lower", outline.color = "white",
  ggtheme = theme_minimal, colors = c("#6D9EC1", "white", "#E46726")
)

## ----edgeless, fig.width = 6, fig.height = 5.6--------------------------------
ggcorrplot(corr,
  outline.color = NA,
  colors = c("red", "white", "blue"),
  legend.title = "Correlation"
) +
  scale_x_discrete(position = "top")

## ----rectangular, fig.width = 6.5, fig.height = 4.4---------------------------
rect <- round(cor(
  mtcars[, c("mpg", "hp", "wt", "qsec")],
  mtcars[, c("disp", "drat", "vs", "am", "gear")]
), 1)
ggcorrplot(rect, hc.order = FALSE, lab = TRUE, outline.color = "white")

## ----polish, fig.width = 6, fig.height = 5.8----------------------------------
p <- ggcorrplot(corr,
  hc.order = TRUE, type = "lower", outline.color = "white",
  legend.title = "Pearson r"
) +
  labs(
    title = "Correlations among car-design variables",
    subtitle = "mtcars, Pearson correlation"
  ) +
  theme(plot.title = element_text(face = "bold"))
p

## ----save, eval = FALSE-------------------------------------------------------
# ggsave("correlogram.png", p, width = 7, height = 6, dpi = 300)

## ----session------------------------------------------------------------------
sessionInfo()

