大数跨境

如何使用DeepSeek进行相关性分析和图表绘制?

如何使用DeepSeek进行相关性分析和图表绘制? SCIPainter
2026-09-11
3
导读:先收藏啦!

推荐公众号奥智生物,分享前沿组学技术、实用生信技能、数据挖掘思路


相关性分析用于衡量变量间关联的方向与强弱,而线性回归则用于建立变量间依赖关系的模型,二者常结合使用进行数据分析,如下图,图表展示环境营养指标掩盖海洋宏基因组中 GC 含量–温度的关联情况

Nature microbiology,2026)

那么,如何进行相关性分析并使用 DeepSeek 轻松复现出上图这样的相关性+线性拟合散点图?这里以稍复杂一点的 图e 为列看下如何复现吧!

01

提交需求


打开DeepSeek页面,切换到识图模式,将文章中的图表截图 和 图e的作图数据上传到附件,并输入提示词。注意,示例数据来自于文章的 Data availability 部分,为了便于模型精准识图,可将图表的原始图例文本加入到提示词中。


尝试输入分析绘图提示词:

你是一名生物学研究生,附件图片对应Fig9中的子图[a, c, e,],附件中的“testdata.xlsx”对应子图e的绘图数据;基于“testdata.xlsx”文件,给出复现子图e的绘图R代码,要求代码简洁明了,避免函数嵌套;附件中图表对应的图例为:Fig. 9 | Environmental nutrient metrics mask GCtemperature associations in marine metagenomes but not in soils. Scatter plots showing the relationship between GC content and temperature before adjustment [a, c, e, g] (upper row) and after partial correlation controlling for environmental nutrient metrics [b, d, f, h] (bottom row). Each panel contains only samples for which a measurement of the specific metric is available. The metric and the number of samples are noted above each panel. NEON soil samples are shown in panels [a, b, c, d]; Panels [a] and [b] show results for oxidized inorganic nitrogen (NOx-Nμg/g) before and after adjustment, respectively. Panels [c] and [d] show the same, but for the C:N ratio. Marine samples are shown in panels [e, f, g, h]; Panels [e] and [f] show results for nitrate (μmol/kg) before and after adjustment, respectively. Panels [g] and [h] show the same, but for phosphate (μmol/kg). In adjusted panels, both temperature and GC content were independently adjusted (controlled) for the indicated metric, and the plotted relationship reflects the correlation between the corresponding residuals. Each point represents a metagenomic sample. Linear fits are shown for visualization; shaded bands indicate 95% confidence intervals around the fitted regression lines. Pearson and Spearman correlation coefficients are reported in each panel. Statistical significance was assessed using two-sided tests for non-zero correlation coefficients. The p-values were not adjusted for multiple comparisons. Significance levels are denoted as: ns, p > 0.05; *, p  0.05; **, p  0.01; ***, p  0.001; ****, p  0.0001.


经过20秒的思考,DeepSeek 给出了绘制文章中图表的详细步骤和绘图代码


其中,代码包含数据读入绘图步骤和中文注释,结构一目了然!如果对代码内容感兴趣,也可以查看下方关键步骤的代码说明。


02

绘图测试


点击给出代码块右上角的“复制”按钮,将DeepSeek生成的R代码复制粘贴到Rstudio的脚本编辑器中运行,得到的初始绘图效果如下


03

图形调整


初始结果图表已基本满足我们的需求,不过我们还可以继续进行图表样式调整。比如散点的样式改为21号,并为散点的添加黑色描边,调整文本标签的位置,更改图表的theme等,效果如下图。


导出后的图片效果如下:

当然,如果对原文的配色不满意,我们也可以尝试自定义其他配色,如下图。


最终的绘图效果如下图:

调整后的绘图代码如下:

# 安装并加载必要的包library(readxl)library(ggplot2)# 1. 读取数据df<- read_excel("testdata.xlsx")df$Project <-factor(df$Project, levels=c("Tara""GEOTRACES"))# 2. 计算整体Pearson和Spearman相关系数pearson <- cor.test(df$Temperaturedf$GC, method ="pearson")spearman <- cor.test(df$Temperaturedf$GC, method ="spearman")# 3. 生成左上角的统计标签(原图中均为****,此处直接对应)label_text <- paste0("r = ", round(pearson$estimate, 2), " (****)\n","ρ = ", round(spearman$estimate, 2), " (****)")# 4. 绘制子图eggplot(df, aes(x = Temperature, y = GC, color = Project))+# 绘制散点,调整透明度以便看清重叠点  geom_point(alpha =0.7)+# 添加线性回归线和95%置信区间  geom_smooth(method ="lm", color ="black", fill ="grey80", alpha =0.5)+# 设置颜色:Tara为深灰,GEOTRACES为青蓝色  scale_color_manual(values =c("Tara"="grey40""GEOTRACES"="cyan4"))+# 添加左上角的标签(注:Nitrate标题及各项目样本数)  annotate("text", x =1, y =0.46,            label = paste0("Nitrate\nTara [78]   GEOTRACES [389]\n", label_text),            hjust =0, vjust =1, size =4)+# 设置坐标轴标签与范围  labs(x ="Temperature (°C)", y ="GC content")+  xlim(0, 30)+  ylim(0.34, 0.46)+# 使用经典主题  theme_classic()+# 调整图例位置  theme(legend.position =c(0.85, 0.9), legend.title = element_blank())# 5. 图形调整#自定义图表主题,对图表做精细调整;top.mar=0.2right.mar=0.2bottom.mar=0.2left.mar=0.2ggplot(df, aes(x = Temperature, y = GC, fill = Project))+# 绘制散点,调整透明度以便看清重叠点  geom_point(shape =21, size =2.5, colour ="black",alpha =0.6)+# 添加线性回归线和95%置信区间  geom_smooth(method ="lm", color ="black", fill ="grey80", alpha =0.5)+# 设置颜色:Tara为深灰,GEOTRACES为青蓝色  scale_fill_manual(values =c("Tara"="grey40""GEOTRACES"="cyan4"))+# 添加左上角的标签(注:Nitrate标题及各项目样本数)  annotate("text", x =1, y =0.49,            label = paste0("Nitrate\nTara [78]   GEOTRACES [389]\n"),            hjust =0, vjust =1, size =4)+  annotate("text", x =1, y =0.335,            label = label_text, color ="grey20",           hjust =0, vjust =1, size =4.5)+# 设置坐标轴标签与范围  labs(x ="Temperature (°C)", y ="GC content")+  scale_y_continuous(limits =c(0.31, 0.49),                     breaks =c(0.32,0.36,0.40,0.44,0.48))+  scale_x_continuous(limits =c(0, 30),                     breaks =c(0,10,20,30))+# 使用经典主题  theme_bw(base_size =13)+# 调整图例位置  theme(panel.grid = element_blank(),        legend.position =c(0.82, 0.92),        legend.background = element_blank(),        legend.title = element_blank(),        plot.margin=unit(x=c(top.mar,right.mar,bottom.mar,left.mar),                         units="inches"))# 6. 自定义颜色ggplot(df, aes(x = Temperature, y = GC, fill = Project))+# 绘制散点,调整透明度以便看清重叠点  geom_point(shape =21, size =3, colour ="black",alpha =0.6)+# 添加线性回归线和95%置信区间  geom_smooth(method ="lm", color ="black", fill ="grey80", alpha =0.5)+# 设置颜色:Tara为深灰,GEOTRACES为青蓝色  scale_fill_manual(values =c("Tara"="#FF99CC""GEOTRACES"="#99CC00"))+# 添加左上角的标签(注:Nitrate标题及各项目样本数)  annotate("text", x =1, y =0.49,            label = paste0("Nitrate\nTara [78]   GEOTRACES [389]\n"),            hjust =0, vjust =1, size =4)+  annotate("text", x =1, y =0.335,            label = label_text, color ="grey20",           hjust =0, vjust =1, size =4.5)+# 设置坐标轴标签与范围  labs(x ="Temperature (°C)", y ="GC content")+  scale_y_continuous(limits =c(0.31, 0.49),                     breaks =c(0.32,0.36,0.40,0.44,0.48))+  scale_x_continuous(limits =c(0, 30),                     breaks =c(0,10,20,30))+# 使用经典主题  theme_bw(base_size =13)+# 调整图例位置  theme(panel.grid = element_blank(),        legend.position =c(0.82, 0.92),        legend.background = element_blank(),        legend.title = element_blank(),        plot.margin=unit(x=c(top.mar,right.mar,bottom.mar,left.mar),                         units="inches"))

好了,本次的DeepSeek复现线性拟合散点图实操教程就分享到这里啦!

参考文献:
https://www.nature.com/articles/s41564-026-02451-y

图片

基迪奥生物不仅提供转录、翻译、蛋白、代谢、表观、微生物等常规组学服务,还拥有空间组学和单细胞等多种前沿技术,以及行业内最专业的开发团队和长达十几年的成果积累,始终专注于组学测序和生信分析,为不同领域研究人员提供最好的组学研究策略。欢迎有项目意向的老师扫描下方二维码填写信息,基迪奥为您定制个性化项目执行方案。

长按识别二维码咨询:

图片
长按识别二维码咨询

咨询当地销售或拨打客服电话:

020-39341079

*未经许可,不得以任何方式复制或抄袭本篇文章之部分或全部内容。版权所有,侵权必究。
图片
图片
# SCIPainter


分享科研绘图技能与工具

欢迎关注与转发~


图片

你的好友拍了拍你

并请你帮她点一下“分享”~

【声明】内容源于网络
0
0
SCIPainter
“一图胜千言”,好看的图形千篇一律,丑的配图千奇百怪。SCIPainter,旨在分享科研绘图的技能与工具。不保证日更,有时间就做线上培训,不排除录制付费视频的可能。如果你想看我日更,请多把我分享出去,说不定我就文思如尿崩了。
内容 928
粉丝 0
SCIPainter “一图胜千言”,好看的图形千篇一律,丑的配图千奇百怪。SCIPainter,旨在分享科研绘图的技能与工具。不保证日更,有时间就做线上培训,不排除录制付费视频的可能。如果你想看我日更,请多把我分享出去,说不定我就文思如尿崩了。
总阅读8.0k
粉丝0
内容928