复现Nature正刊上3张图,换数据即可用!
本次使用R语言复现Nature正刊上的3张图!
R绘制-d图
✅d图是一种SCI中高频率使用的特殊散点图--火山图(Volcano plot),用于快速筛选两组数据之间显著差异的基因/蛋白/代谢物等。那怎么读这张图:
FC:全称Fold Change,代表两组数据某个变量的差异倍数,在上图中表示Astro BioID2和Neuro BioID2两种细胞中不同蛋白质的丰度倍数。实际使用时常用log2Fold Change,log2FC代替FC,原因如下表:
图中每个点:代表两组数据某个变量的差异倍数取log2,例如,上图中,每个点代表一种蛋白在Astro BioID2和Neuro BioID2两种细胞中的丰度相除并取log2,即log2(Astro BioID2/Neuro BioID2 FC) ; P:代表两组数据之间的变量差异是否统计学显著,一般认为P<0.05或者P<0.01为显著(如图中的水平虚线P=0.05,-log10(P)=1,3);有时候也会使用BH或者FDR方法校正的P值; FC阈值:科研中FC常取2、1.5,此时对应的log2FC为1和0.6,典型的火山图还会添加两条FC线,例如一张经典的火山图,
上调(up)变量筛选:火山图通常以 log2FC≥1(即FC ≥ 2),以及P<0.05来筛选上调(up)变量,即下图的绿色点; 下调(down)变量筛选:火山图通常以 log2FC≤ -1(即FC ≤ 0.5)以及P<0.05来筛选下调(down)变量,即下图的紫色点;
后续研究重点关注火山图中这些上调(up)变量和下调(down)变量。
✅读入测试数据
✅关键代码,
# ----------------------------
# 绘图
# ----------------------------
p <- ggplot(volcano_na_data, aes(x = log2FC, y = negLog10P)) +
geom_point(aes(fill = group),
color = "black", shape = 21, stroke = 0.35, size = 5, alpha = 1
) +
scale_fill_manual(values = colors_fill)
# P=0.05 横线与标注
p <- p + geom_hline(yintercept = -log10(0.05), linetype = "dashed", color = "black", linewidth = 0.5) +
annotate("text",
x = 7, y = -log10(0.05) + 0.1, label = expression(italic(P) == 0.05),
size = 3.5, vjust = 0
)
p <- p + annotate("text",
x = x_axis_pos + x_label_offset + 0.3, # 调整x坐标,靠近顶点
y = max(volcano_na_data$negLog10P) * 1.05, # 将y坐标设置为最大值,靠近顶部
label = expression(-log[10] ~ "(P)"),
angle = 90,
size = 3.5,
fontface = "bold",
hjust = 1.6,
vjust = 1.7# 调整垂直对齐,确保标签居中在顶点位置
)
✅值得学习的R可视化知识点:
将y轴移动到x=0处,简洁美观,
p <- p + annotate("text",
x = x_axis_pos + x_label_offset + 0.3, # 调整x坐标,靠近顶点
y = max(volcano_na_data$negLog10P) * 1.05, # 将y坐标设置为最大值,靠近顶部
label = expression(-log[10] ~ "(P)"),
angle = 90,
size = 3.5,
fontface = "bold",
hjust = 1.6,
vjust = 1.7# 调整垂直对齐,确保标签居中在顶点位置
)
-log10(P)的写法,expression(-log[10] ~ "(P)") geom_hline添加水平辅助线,
# P=0.05 横线与标注
p <- p + geom_hline(yintercept = -log10(0.05), linetype = "dashed", color = "black", linewidth = 0.5) +
annotate("text",
x = 7, y = -log10(0.05) + 0.1, label = expression(italic(P) == 0.05),
size = 3.5, vjust = 0
)
也可以换一个原文中好看的配色,
R绘制-i、j图
✅关键代码,
# 绘制图i左
plot_i_left <- ggplot(linescatter_ileft, aes(distance, Mean, color = Group)) +
geom_line(size = 0.2) +
geom_point(size = 1.5) +
geom_errorbar(aes(ymin = Mean - SE, ymax = Mean + SE),
width = 0.8, size = 0.3, alpha = 0.8
) +
geom_vline(xintercept = 10, linetype = "dotted", color = "black", size = 0.2) +
geom_vline(xintercept = 40, linetype = "dotted", color = "black", size = 0.2) +
scale_color_manual(values = c(WT = wt_color, KO = ko_color)) +
scale_x_continuous(
limits = c(0, 40),
breaks = seq(0, 40, by = 5),
expand = c(0.02, 0)
) +
scale_y_continuous(
limits = c(0, 700),
breaks = seq(0, 700, by = 100),
expand = c(0, 0)
) +
annotate("text",
x = 5, y = 700,
label = "Soma", size = 2.5, vjust = 1, lineheight = 0.8
) +
annotate("segment", x = 0.5, xend = 9.5, y = 650, yend = 650, size = 0.2) +
annotate("text",
x = 25, y = 700,
label = "Branches", size = 2.5, vjust = 1, lineheight = 0.8
) +
annotate("segment", x = 10.5, xend = 39.5, y = 650, yend = 650, size = 0.2) +
annotate("text",
x = 5, y = 550,
label = "NS", size = 2.5, vjust = 1, lineheight = 0.8
) +
annotate("segment", x = 3, xend = 8, y = 500, yend = 500, size = 0.2) +
annotate("text",
x = 25, y = 550, label = expression(italic(P) < 0.05),
parse = TRUE, size = 2.5, vjust = 1, lineheight = 0.8
) +
annotate("segment", x = 15, xend = 35, y = 500, yend = 500, size = 0.2) +
annotate("text",
x = distance[distance > 15],
y = WT_mean[distance > 15] + WT_se[distance > 15] + 30,
label = "*", size = 2, color = "black", vjust = 0.5
) +
labs(
x = "Distance from centre of soma (μm)",
y = "LifeAct GFP meannactin intensity (AU)"
) +
guides(color = guide_legend(override.aes = list(
linetype = 0,
shape = 16,
size = 2
))) +
theme_custom +
theme(
legend.position = c(0.1, 0.15),
legend.text = element_text(
size = 7,
family = "Helvetica",
margin = margin(l = -2)
),
legend.key.height = unit(8, "pt"),
legend.key.width = unit(12, "pt"),
)
✅值得学习的R可视化知识点:
使用annotate("text"......添加注释文字,
annotate("text",
x = 5, y = 700,
label = "Soma", size = 2.5, vjust = 1, lineheight = 0.8
)
使用annotate("segment"......添加辅助线,
annotate("segment", x = 0.5, xend = 9.5, y = 650, yend = 650, size = 0.2)
图例精确微调,例如,调节图例中红色点和标签KO之间的间距;调节KO和WT之间的间距;调节图例在整个图中的任意位置等,
theme(
legend.position = c(0.1, 0.15), # 图例内嵌定位
legend.text = element_text(margin = margin(l = -2)), # 紧凑排版
legend.key.height = unit(8, "pt"), # 图例项高度
legend.key.width = unit(12, "pt") # 图例项宽度
)
本期结束!
测试数据+详细代码,后期会加入👉《保姆级R可视化教程》来了!
加入学习(备注:299)