简体   繁体   中英

need to increase the font size of correlations value with bold font in ggcorrplot package

I would like to increase the font of the correlation values to be bigger and bold. I used this command

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"))

You could just modify the function. I think that's the problem with such high level functions that are not necessarily very flexibly designed. For example, by adding ... you could make this function a bit more flexible. Below a massively stripped down version of ggcorrplot::ggcorrplot , which shows the principle idea - adding ... to the function arguments and then to the right place in the function (the call to geom_text) allows then the use of font_face - see comments in code.

In this function version, I have removed all if/else statements, and slightly simplified the plot styling elements.

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
  )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM