ggplot2 绘制相关系数热图
加显著性 *
和 Pvaue
值
- 参考:哪里不会搜哪里,哈哈哈
- 其实归根到底就是全文要敲用判断和
paste()
或者 paste0()
tidyverse::case_when()
-
factor()
: 重定义因子顺序
- takecolor 下载链接
- 也可以看我 可能是 R 语言最全调色板:Paletteer 包 和 自己慢慢收集 的详细关于配色的操作
library(tidyverse)
library(reshape2)
a <- matrix(rnorm(90), 9, 10)
colnames(a) <- letters[1:10]
rownames(a) <- letters[1:9]
data <- melt(a)
data <- data %>%
mutate(text = case_when( # 一定要 get 到 case_when() 函数奥秘
value > 0 ~ paste(round(value, 2), "\n*"), # round() 只保留两位小数
value <= 0 ~ paste(round(value, 2), "\n**")))
data$new_Var1 <- factor(data$Var1, levels = c("a", "b", "c", "d", "e", "f", "g", "h", "i")) # 重新排序因子,决定坐标轴出图顺序
data$new_Var2 <- factor(data$Var2, levels = rev(c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"))) # 同上
ggplot(data, aes(new_Var1, new_Var2)) +
geom_tile(aes(fill = value), colour = "grey", size = 1)+
scale_fill_gradient2(low = "#5C5DAF",mid = "white",high = "#EA2E2D") + # 这里可以用 windowns 小工具 takecolor 取色,看中哪个文章就吸哪个文章
# 比如这篇 https://www.nature.com/articles/nmeth.1902
geom_text(aes(label=text),col ="black",size = 5) +
theme_minimal() + # 不要背景
theme(axis.title.x=element_blank(), # 去掉 title
axis.ticks.x=element_blank(), # 去掉x 轴
axis.title.y=element_blank(), # 去掉 y 轴
axis.text.x = element_text(angle = 45, hjust = 1, size = 14, face = "bold"), # 调整x轴文字,字体加粗
axis.text.y = element_text(size = 14, face = "bold")) + #调整y轴文字
labs(fill =paste0(" * p < 0","\n\n","** p > 0","\n\n","Correlation")) + # 修改 legend 内容
scale_x_discrete(position = "top") # 将 X 轴放置在最上面