安装包
remotes::install_github("r-link/corrmorant")
加载包和绘图
library(corrmornt)
library(tidyverse)
corrmorant(mpg)
换颜色绘图
corrmorant(drosera, style = "light")
corrmorant(drosera, style = "blue_red")
更复杂的绘图
ggcorrm(data = mpg)+
lotri(geom_point(alpha=0.5))+
lotri(geom_smooth())+
utri_heatmap()+
utri_corrtext()+
dia_names(y_pos = 0.15,size=3)+
dia_histogram(lower = 0.3,fill="grey80",color=1)+
scale_fill_corr()+
labs(title="Correlation Plot")
data1 <- MASS::mvrnorm(100, colMeans(mtcars), cov(mtcars))[, 1:8]
colnames(data1) <- paste("Var.", 1:ncol(data1))
# create plot
ggcorrm(data1,
mapping = aes(col = .corr, fill = .corr),
bg_dia = "grey20",
rescale = "by_sd") +
lotri(geom_smooth(method = "lm", size = .3)) +
lotri(geom_point(alpha = 0.5)) +
utri_corrtext(nrow = 2, squeeze = 0.6) +
dia_names(y_pos = 0.15, size = 3, color = "white") +
dia_histogram(lower = 0.3, color = "grey80", fill = "grey60", size = .3) +
scale_color_corr(aesthetics = c("fill", "color"))
select(mtcars, mpg, disp:qsec) %>%
ggcorrm(rescale = "by_sd") +
utri_heatmap(alpha = 0.5) +
lotri_heatcircle(alpha = 0.5, col = 1) +
utri_corrtext() +
dia_names(y_pos = 0.15, size = 3) +
dia_density(lower = 0.3, fill = "lightgrey", color = 1) +
scale_fill_corr()
# function to compute linear model slopes
lmslope <- function(x, y) round(coef(lm(x ~ y))[2], 2)
# add slopes using a function
ggcorrm(drosera, mapping = aes(col = species, fill = species)) +
lotri(geom_point(alpha = 0.4)) +
lotri(geom_smooth(alpha = 0.4, method = "lm")) +
utri_funtext(fun = lmslope, squeeze = 0.6) +
dia_density(lower = 0.3, col = 1, alpha = 0.4) +
dia_names(y_pos = 0.15)