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.
1 Answer
Reset to default 0You 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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744877157a4598616.html
评论列表(0条)