# Compute correlation matrix
#::::::::::::::::::::::::::::::::::::::::::
cor.mat <- mtcars %>%
select(mpg, disp, hp, drat, wt, qsec) %>%
cor_mat()
# Visualize correlation matrix
#::::::::::::::::::::::::::::::::::::::::::
# Full correlation matrix,
# insignificant correlations are marked by crosses
cor.mat %>% cor_plot()
# Reorder by correlation coefficient
# pull lower triangle and visualize
cor.lower.tri <- cor.mat %>%
cor_reorder() %>%
pull_lower_triangle()
cor.lower.tri %>% cor_plot()
# Change visualization methods
#::::::::::::::::::::::::::::::::::::::::::
cor.lower.tri %>%
cor_plot(method = "pie")
cor.lower.tri %>%
cor_plot(method = "color")
cor.lower.tri %>%
cor_plot(method = "number")
# Show the correlation coefficient: label = TRUE
# Blank the insignificant correlation
#::::::::::::::::::::::::::::::::::::::::::
cor.lower.tri %>%
cor_plot(
method = "color",
label = TRUE,
insignificant = "blank"
)
# Change the color palettes
#::::::::::::::::::::::::::::::::::::::::::
# Using custom color palette
# Require ggpubr: install.packages("ggpubr")
if(require("ggpubr")){
my.palette <- get_palette(c("red", "white", "blue"), 200)
cor.lower.tri %>%
cor_plot(palette = my.palette)
}
# Using RcolorBrewer color palette
if(require("ggpubr")){
my.palette <- get_palette("PuOr", 200)
cor.lower.tri %>%
cor_plot(palette = my.palette)
}
Run the code above in your browser using DataLab