我想增加相关值的字体,使其更大、更粗。我使用了这个命令
as<- ggcorrplot(sticor,
hc.order = TRUE,
type = "lower",
lab = TRUE)
as + theme(text = element_text(size = 20, face = "bold"),
legend.title=element_text(size=15),
legend.text=element_text(size=15),axis.text.y = element_text(size=20, face = "bold"),axis.text.x = element_text(size=20, face= "bold"))

发布于 2021-04-28 06:57:45
您只需修改函数即可。我认为这就是高级函数的问题所在,这些函数的设计不一定非常灵活。例如,通过添加...,您可以使此功能更灵活一些。下面是大量精简的ggcorrplot::ggcorrplot版本,它展示了基本思想-将...添加到函数参数中,然后添加到函数中的正确位置(对geom_text的调用),然后允许使用font_face -请参阅代码中的注释。
在这个函数版本中,我删除了所有if/else语句,并略微简化了绘图样式元素。
ggcorrplot2(cor(mtcars), fontface = "bold")

ggcorrplot2 <- function(corr,
show.diag = FALSE,
colors = c("blue", "white", "red"),
digits = 1,
... ### this is trick part 1
) {
corr <- as.matrix(corr)
corr <- base::round(x = corr, digits = digits)
corr <- ggcorrplot:::.get_lower_tri(corr, show.diag)
p.mat <- ggcorrplot:::.get_lower_tri(NULL, show.diag)
corr <- reshape2::melt(corr, na.rm = TRUE)
colnames(corr) <- c("Var1", "Var2", "value")
corr$pvalue <- rep(NA, nrow(corr))
corr$signif <- rep(NA, nrow(corr))
corr$abs_corr <- abs(corr$value) * 10
p <- ggplot2::ggplot(data = corr, mapping = ggplot2::aes_string(
x = "Var1",
y = "Var2", fill = "value"
)) +
ggplot2::geom_tile() +
ggplot2::scale_fill_gradient2(
low = colors[1], high = colors[3],
mid = colors[2], midpoint = 0, limit = c(-1, 1), space = "Lab",
name = "Corr"
) +
ggplot2::theme_minimal() +
ggplot2::coord_fixed()
label <- round(x = corr[, "value"], digits = digits)
p + ggplot2::geom_text(
mapping = ggplot2::aes_string(
x = "Var1", y = "Var2"
), label = label,
... ### this is the entire trick
)
}https://stackoverflow.com/questions/67289249
复制相似问题