微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

需要在ggcorrplot包中用粗体增加相关值的字体大小

如何解决需要在ggcorrplot包中用粗体增加相关值的字体大小

我想增加相关值的字体更大更粗。我用过这个命令

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,axis.text.x = element_text(size=20,face= "bold"))

解决方法

你可以修改函数。我认为这就是设计不一定非常灵活的高级功能的问题。例如,通过添加 ... 可以使此函数更加灵活。在 ggcorrplot::ggcorrplotmassively 精简版下方,它展示了基本思想 - 将 ... 添加到函数参数,然后添加到函数中的正确位置(对 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
  )
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。