admin管理员组

文章数量:1404923

I created the correlation matrix heat map using metan package. I would like to change the text color to white.

library(metan)
ccoeff <- corr_coef(df_GDPdata)
plot(ccoeff, type = "upper", reorder="FALSE")

I am looking for a heatmap (correlation matrix) with metan package, where the text colour can be changed.

I created the correlation matrix heat map using metan package. I would like to change the text color to white.

library(metan)
ccoeff <- corr_coef(df_GDPdata)
plot(ccoeff, type = "upper", reorder="FALSE")

I am looking for a heatmap (correlation matrix) with metan package, where the text colour can be changed.

Share Improve this question edited Mar 9 at 12:22 Tim G 6,2201 gold badge3 silver badges19 bronze badges asked Mar 9 at 5:26 ss tss t 91 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 0

You can change the text-color to white like this

#install.packages("metan")
library(metan) 
ccoeff <- corr_coef(mtcars) 
p <- plot(ccoeff, type = "upper", reorder="FALSE")
p$layers[[3]]$geom$default_aes$colour <- "white" # change the text-color
p

Using Shadowtext

The cleaner approach is removing both text label layers and replacing them with the shadowtext.

library(metan)
library(ggplot2)
library(shadowtext) 

ccoeff <- corr_coef(mtcars)
p <- plot(ccoeff, type = "upper", reorder = FALSE)

p$layers[[3]] <- NULL
p$layers[[2]] <- NULL

cor_data_lower <- data.frame(
  var1 = p$data$v2,
  var2 = p$data$v1,
  cor = p$data$cor,
  label = paste0(sprintf("%.2f", p$data$cor), "\n", p$data$pval_star)
) 

# Add shadow text layer
p <- p + 
  geom_shadowtext(
    data = cor_data_lower,
    aes(x = var2, y = var1, label = label),
    color = "white",
    bg.color = "black",
    bg.r = 0.15,  # Controls the shadow/outline size
    fontface = "bold",
    show.legend = FALSE
  )

p


Or make your own Shadowtext

library(metan)
library(ggplot2)
ccoeff <- corr_coef(mtcars)
p <- plot(ccoeff, type = "upper", reorder = FALSE)

p$layers[[3]] <- NULL
p$layers[[2]] <- NULL

cor_data_lower <- data.frame(
  var1 = p$data$v2,
  var2 = p$data$v1,
  cor = p$data$cor, # is needed
  label = paste0(sprintf("%.2f", p$data$cor), "\n", p$data$pval_star)
) 

size <- 4

p <- p +
  geom_text(
    data = cor_data_lower,
    aes(x = var2, y = var1, label = label, family = "mono", fontface = "bold"), 
    size = size, 
    colour = "black",
    nudge_x = size/200
  ) +
  geom_text(
    data = cor_data_lower,
    aes(x = var2, y = var1, label = label, family = "mono", fontface = "bold"), 
    size = size, 
    colour = "black",
    nudge_x = -size/200
  ) +
  geom_text(
    data = cor_data_lower,
    aes(x = var2, y = var1, label = label, family = "mono", fontface = "bold"), 
    size = size, 
    colour = "black",
    nudge_y = size/220
  ) +
    geom_text(
      data = cor_data_lower,
      aes(x = var2, y = var1, label = label, family = "mono", fontface = "bold"), 
      size = size, 
      colour = "black",
      nudge_y = -size/220
    )  +
  geom_text(
    data = cor_data_lower,
    aes(x = var2, y = var1, label = label, family = "mono", fontface = "bold"),  
    size = size, 
    colour = "white"
  )
p

本文标签: