复现nature图表:双重组合-渐变背景火山图-效果?无需多言杠杠的!

复现一篇Nature medicine的火山图,如下:



(reference:Sex-dependent APOE4 neutrophil–microglia interactions drive cognitive impairment in Alzheimer’s disease)

话不多说,看代码(直播视频:哔哩哔哩https://space.bilibili.com/471040659?spm_id_from=333.1007.0.0):


setwd('D:\\KS项目\\公众号文章\\复现火山图')

#Loading packages
library(Seurat)
library(ggplot2)
library(ggrepel)
library(cowplot)
library(grid)
#load data & prepare data for plot
uterus <- readRDS("D:/KS项目/公众号文章/RNA速率分析/uterus.rds")
SMC <- subset(uterus, celltype=='Smooth muscle cells')
EH <- FindMarkers(uterus, ident.1 = "EEC", ident.2 = "HC",logfc.threshold = 0, min.pct = 0.2, group.by = 'orig.ident')#差异基因分析用来构建演示作图数据
AH <- FindMarkers(uterus, ident.1 = "AEH", ident.2 = "HC",logfc.threshold = 0, min.pct = 0.2, group.by = 'orig.ident')#差异基因分析用来构建演示作图数据


EH$gene <- rownames(EH)#添加gene列
AH$gene <- rownames(AH)#添加gene列

EH$log_pval <- -log10(EH$p_val_adj)#p值取对数
AH$log_pval <- -log10(AH$p_val_adj)#p值取对数



AH$log_pval <- -AH$log_pval #这一组的log_pval变成负值,主要是为了图的倒转结合

#sig gene for label,or you can chose any genes if you want
sig_EH <- EH[EH$p_val_adj <= 0.01 & abs(EH$avg_log2FC)>=1.2,]
sig_AH <- AH[AH$p_val_adj <= 0.01 & abs(AH$avg_log2FC)>=1.2,]


#merge data
df <- rbind(EH,AH)


#设置自己需要的渐变背景
# rect1 <- rasterGrob(alpha(c('#FFF7F3', '#FDE0DD', '#FCC5C0', '#FA9FB5', '#F768A1','#DD3497', '#7A0177'),0.5),
#                     width=unit(1,"npc"), height = unit(1,"npc"), interpolate = TRUE)
# rect2<- rasterGrob(alpha(c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d'),0.5), 
#                    width=unit(1,"npc"), height = unit(1,"npc"), interpolate = TRUE) 
# rect3<- rasterGrob(c("#FFFFD9", "#EDF8B1", "#C7E9B4", "#7FCDBB", "#41B6C4"), width=unit(1,"npc"), height = unit(1,"npc"), interpolate = TRUE) 
# 
# rect4<- rasterGrob(c("#DC6F58","#F7B698","#FAE7DC","#E1EDF3","#A7CFE4"),width=unit(1,"npc"), height = unit(1,"npc"), interpolate = TRUE) 


rect1 <- rasterGrob(matrix(rev(alpha(c('#FFF7F3', '#FDE0DD', '#FCC5C0', '#FA9FB5', '#F768A1','#DD3497', '#7A0177'),0.5)),nrow = 1),
                    width=unit(1,"npc"), height = unit(1,"npc"), interpolate = TRUE)
rect2<- rasterGrob(matrix(alpha(c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d'),0.5),nrow = 1), 
                   width=unit(1,"npc"), height = unit(1,"npc"), interpolate = TRUE) 
rect3<- rasterGrob(matrix(rev(c("#FFFFD9", "#EDF8B1", "#C7E9B4", "#7FCDBB", "#41B6C4")),nrow = 1), width=unit(1,"npc"), height = unit(1,"npc"), interpolate = TRUE) 

rect4<- rasterGrob(matrix(rev(c("#DC6F58","#F7B698","#FAE7DC","#E1EDF3")),nrow = 1),width=unit(1,"npc"), height = unit(1,"npc"), interpolate = TRUE) 



#plot
p = ggplot(df, aes(avg_log2FC,log_pval))+
  annotation_custom(rect1,xmin=-Inf,xmax=-0.3,ymin=20,ymax=Inf)+
  annotation_custom(rect2, xmin=0.3,xmax=Inf,ymin=20,ymax=Inf)+
  annotation_custom(rect3,xmin=-Inf,xmax=-0.3,ymin=-20,ymax=-Inf)+
  annotation_custom(rect4, xmin=0.3,xmax=Inf,ymin=-20,ymax=-Inf)+
  geom_hline(aes(yintercept=20), color = "#999999", linetype="dashed", size=1) +#添加横线
  geom_hline(aes(yintercept=-20), color = "#999999", linetype="dashed", size=1) +#添加横线
  geom_vline(aes(xintercept=-0.3), color = "#999999", linetype="dashed", size=1) + #添加纵线
  geom_vline(aes(xintercept=0.3), color = "#999999", linetype="dashed", size=1) + #添加纵线
  geom_point_rast(stroke = 0.5, size=2, shape=21, fill="grey")+  
  geom_point_rast(data = df[abs(df$log_pval) >= 20  &  abs(df$avg_log2FC) >=0.3,], 
             aes(fill=avg_log2FC),stroke = 0.5, size=3, shape=21)+##显著基因特别标注,size比非显著的大一点,颜色用logFC表示
  scale_fill_gradient2(limits=c(-max(df$avg_log2FC), max(df$avg_log2FC)), low="blue", mid="whitesmoke", high = "red", na.value = 'blue')+ #密度颜色设置,并限定范围
  geom_text_repel(data = sig_EH[sig_EH$avg_log2FC >0,], aes(label=gene),
                  nudge_y = 80,
                  nudge_x = 2,
                  color = 'black', size = 4,fontface = 'italic',
                  min.segment.length = 0,
                  max.overlaps = Inf,
                  box.padding = unit(0.5, 'mm'),
                  point.padding = unit(0.5, 'mm')) + #label需要标注的基因
  geom_text_repel(data = sig_EH[sig_EH$avg_log2FC <0,], aes(label=gene),
                  nudge_y = 80,
                  nudge_x = -2,
                  color = 'black', size = 4,fontface = 'italic',
                  min.segment.length = 0,
                  max.overlaps = Inf,
                  box.padding = unit(0.5, 'mm'),
                  point.padding = unit(0.5, 'mm')) + #label需要标注的基因
  geom_text_repel(data = sig_AH[sig_AH$avg_log2FC >0,],aes(label=gene),
                  color = 'black',size = 4, fontface = 'italic',
                  nudge_y = -80,
                  nudge_x = 2,
                  min.segment.length =0,
                  max.overlaps = Inf,
                  point.padding = unit(0.5, 'mm'))+#label需要标注的基因
  geom_text_repel(data = sig_AH[sig_AH$avg_log2FC <0,],aes(label=gene),
                  color = 'black',size = 4, fontface = 'italic',
                  nudge_y = -80,
                  nudge_x = -2,
                  min.segment.length =0,
                  max.overlaps = Inf,
                  point.padding = unit(0.5, 'mm'))+#label需要标注的基因
  theme_classic()+ #设置主题
  theme(aspect.ratio = 1,
        axis.text.x=element_text(colour="black",size =12),
        axis.text.y=element_text(colour="black",size =12),
        axis.ticks=element_line(colour="black"),
        axis.title = element_blank(),
        plot.margin=unit(c(0,0,1,0),"line"),
        legend.direction = 'horizontal',
        legend.position = 'top',
        legend.justification=c(1,2),
        legend.key.width=unit(0.5,"cm"),
        legend.key.height = unit(0.3, "cm"),
        legend.title.position = 'top')+ #修改主题标签文字等等
  geom_rect(aes(xmin =-Inf, xmax = Inf, ymin = 20, ymax = 400),
            fill = "transparent", color = "red", size = 1.5,linetype="dashed")+
  geom_rect(aes(xmin =-Inf, xmax = Inf, ymin = -20, ymax = -400),
            fill = "transparent", color = "blue", size = 1.5,linetype="dashed")+
  scale_y_continuous(expand = c(0,0),breaks = c(-200,0,200), labels = c(200,0,200))#修改y轴标签


p


#add other label
ggdraw(xlim = c(0, 1), ylim = c(0,1.1))+  # 设置绘图区域的界限
  draw_plot(p,x = 0, y =0) +  # 添加主图(热图)
  draw_line(x = c(0.3,0.8), y = c(0.01,0.01),lineend = "round",
            size =1, col = "black",  # 添加箭头
            arrow=arrow(angle = 15, length = unit(0.1,"inches"),type = "closed"))+
  draw_text(text = "Log2FC", size = 12,
            x = 0.2, y = 0.02,color="black",fontface = "italic")+
  draw_line(x = c(0.1,0.1), y = c(0.2,0.85),
            lineend = "round",size =1, col = "black",  # 添加箭头
            arrow=arrow(angle = 15, length = unit(0.1,"inches"),type = "closed"))+
  draw_text(text = "-Log(P)", size = 12,angle=90,
            x = 0.1, y = 0.15,color="black",fontface = "italic")+
  draw_text(text = "EEC vs HC", size = 12,angle=90,x = 0.9, y = 0.7,color="black",fontface = "bold")+
  draw_text(text = "AEC vs HC", size = 12,angle=90,x = 0.9, y = 0.3,color="black",fontface = "bold")
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,163评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,301评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,089评论 0 352
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,093评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,110评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,079评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,005评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,840评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,278评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,497评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,667评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,394评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,980评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,628评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,649评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,548评论 2 352

推荐阅读更多精彩内容

  • 激发斗志视频:1《天道》里的“高人思维”到底是什么?_哔哩哔哩_bilibili[https://www.bili...
    阳明先生_X自主阅读 70评论 0 1
  • 1.白板推导系列,up主shuhuai008的个人空间 - 哔哩哔哩 ( ゜- ゜)つロ 乾杯~ Bilibili...
    南_橘子猪阅读 455评论 0 0
  • 3D建模到底难不难? 如果是新手听到这个问题,那毫无疑问呢是难,特别难;但是其实你要是去问学成了的人来说,那肯定就...
    雨惜带你学建模阅读 168评论 0 0
  • 第一章 【CG短片】个人科幻短片《DEEP:深海》 ——耗时两年半独立完成的个人CG作品_哔哩哔哩_bilibil...
    识迩阅读 65评论 0 1
  • 一、期末大作业 1、游戏🎮《别再踩白块了》辅助器项目 2、发票和收据🧾文本 OCR 识别项目 3、基于 CVzon...
    Du1in9阅读 108评论 0 2