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.
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
本文标签:
版权声明:本文标题:heatmap - changing the color of text inside the correlation matrix heat map using metan and plot function in r - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744877156a2629984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论