ggplot做热图添加*号

有人问我,ggplot做热图上面如何标记显著性,且不说之前公众号发过相关性分析的热图做法,也讲过好多次标注标签。其实很简单,只是穿了个马甲就不认识了。这里我们用GSEA的数据做个图,GSEA的分析参考之前的帖子,不再赘述(https://mp.weixin.qq.com/s?__biz=Mzg5OTYzMzY5Ng==&mid=2247484990&idx=1&sn=894597500ba8f805116521eddc0d34cd&chksm=c0510171f7268867c30dc8b49f4d12a5183689d69abea2f06488dbf52356de263bdeeddb1c4e&scene=21#wechat_redirect)。数据纯属虚构,没有意义,也不要问我为什么数据是这样。示例数据及代码已上传群文件!

图片

加载数据及R包,按照阈值增加标签:

etwd("D:/KS项目/公众号文章/ggplot图加符号")
df <- read.csv("GSEA_plot.csv", header = T)
library(ggplot2)
library(tidyverse)
library(reshape2)
colnames(df)
data <- df %>% mutate(text = case_when(
  p.adjust < 0.01 ~ "*"))

ggplot作图即可:

ggplot(data, aes(group, Description)) + 
  geom_tile(aes(fill = NES), colour = "black", size = 0.5)+
  scale_fill_gradient2(low = '#1A5592',mid = "white",high = "#B83D3D") + 
  geom_text(aes(label=text),col ="black",size = 5) +
  theme_minimal() + 
  theme(axis.title.x=element_blank(), 
        axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), 
        axis.text.x = element_text(size = 10,color = 'black',angle = 90, hjust = 1), 
        axis.text.y = element_text(size = 10,color = 'black'),
        legend.position = 'top') + 
  scale_y_discrete(position = "right")
图片

可以用*号,也可以用其他任何能打出来的符号:

data1 <- df %>% mutate(text = case_when(
  p.adjust < 0.01 ~ "⚪"))
ggplot(data1, aes(group, Description)) + 
  geom_tile(aes(fill = NES), colour = "black", size = 0.5)+
  scale_fill_gradient2(low = '#1A5592',mid = "white",high = "#B83D3D") + 
  geom_text(aes(label=text),col ="black",size = 5) +
  theme_minimal() + 
  theme(axis.title.x=element_blank(), 
        axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), 
        axis.text.x = element_text(size = 10,color = 'black',angle = 90, hjust = 1), 
        axis.text.y = element_text(size = 10,color = 'black'),
        legend.position = 'top') + 
  scale_y_discrete(position = "right")
图片

重点来了,一个小知识点,ggplot作图很多时候因子排列顺序会按照默认的比如说首字母,会与自己想要的顺序不一致,经常有小伙伴问这个问题。其实用因子提前排好序就可以了。


data1$group_new <- factor(data1$group, levels = c("NK","DC","CD14+ Mono","FCGR3A+ Mono")) 
ggplot(data1, aes(group_new, Description)) + 
  geom_tile(aes(fill = NES), colour = "black", size = 0.5)+
  scale_fill_gradient2(low = '#1A5592',mid = "white",high = "#B83D3D") + 
  geom_text(aes(label=text),col ="black",size = 5) +
  theme_minimal() + 
  theme(axis.title.x=element_blank(), 
        axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), 
        axis.text.x = element_text(size = 10,color = 'black',angle = 90, hjust = 1), 
        axis.text.y = element_text(size = 10,color = 'black'),
        legend.position = 'top') + 
  scale_y_discrete(position = "right")
图片

每天学一点,每次懂一点知识,离学会就不远了。觉得分享对你有的话,点个赞,风向一下再走呗!!!

你可能感兴趣的:(ggplot做热图添加*号)