散点图命令,plot函数_R语言基础绘图函数散点图~跟着Nature Communications学画图~Figure1

 2023-09-26 阅读 29 评论 0

摘要:今天继续 跟着Nature Communications学画图系列第二篇。学习R语言基础绘图函数画散点图。对应的 Nature Communications 的论文是 Fecal pollution can explain antibiotic resistance gene abundances in anthropogenically impacted environments这篇论文数据分析和可视化的

今天继续 跟着Nature Communications学画图系列第二篇。学习R语言基础绘图函数画散点图。

对应的 Nature Communications 的论文是 Fecal pollution can explain antibiotic resistance gene abundances in anthropogenically impacted environments

这篇论文数据分析和可视化的部分用到的数据和代码全部放到了github上 https://github.com/karkman/crassphage_project

散点图命令,非常好的R语言学习素材。

今天学习Figure1中被红色框线圈住的散点图

7ea73769b7c4a6ddab1db87ffbba4997.png
image.png
第一部分先收一下上一篇文章的尾

跟着Nature Communications学画图~Figure1~基础绘图函数箱线图

这篇文章中有人留言说 和原图不是很像,因为配色没有按照论文中提供的代码来。 下面是完全重复论文中的代码

cols boxplot(log10(rel_crAss)~country,data=HMP,col=cols,
axes=F,xlab=NULL,ylab=NULL,
horizontal = T)
axis(2,at=c(1,2,3),labels=c("China", "Europe", "US"),las=1)
title("a",adj=0,line=0)
d08150d6af03086a32070d18a61f2686.png
image.png
第二部分 基础绘图函数散点图
  • 读入数据
HMP
  • 最基本的散点图
plot(rel_res~rel_crAss,data=HMP)
b8f2f9b50d826a40e76840f41397bc82.png
image.png

matplotlib画图?画图用plot()函数,需要指定画图用到的变量y和x,还有画图用到的数据data

原始代码分别对 rel_res 和 rel_crAss取了log10

plot(log10(rel_res)~log10(rel_crAss),data=HMP)
a0b2a5fc0ff72a43feabcd6512d5e83f.png
image.png

取log10以后可以看到散点分布的更加均匀了。

接下来就是对图进行美化了
  • 按照国家分组填充颜色
cols plot(log10(rel_res)~log10(rel_crAss), data=HMP, 
bg=cols[as.factor(HMP$country)],pch=21)
b47172f85882e9a36c648341e28de309.png
image.png
  • 更改点的大小
plot(log10(rel_res)~log10(rel_crAss), data=HMP, 
bg=cols[as.factor(HMP$country)],pch=21,cex=2)
fd667d68575831db35c6b36fb22bf08e.png
image.png
  • 更改x轴和y轴的标签
plot(log10(rel_res)~log10(rel_crAss), data=HMP, bg=cols[as.factor(HMP$country)], pch=21,
ylab = "Normalized ARG abundance (log10)",
xlab="Normalized crAssphage abundance (log10)", cex=2)
70af9501eb8169af199608c7f4383476.png
image.png
  • 更改坐标轴的范围
plot(log10(rel_res)~log10(rel_crAss), data=HMP, 
bg=cols[as.factor(HMP$country)], pch=21,
ylab = "Normalized ARG abundance (log10)",
xlab="Normalized crAssphage abundance (log10)",
cex=2,
ylim=c(2.5, 4.5))
3e072b7c22abd94aa8a5da395357c00f.png
image.png

接下来将箱线图和散点图按照上下拼接到一起,用到的是par(fig=c(a,b,c,d)),这里需要满足 a

r语言绘制散点图?具体可以参考链接 https://blog.csdn.net/qingchongxinshuru/article/details/52004182

par(fig=c(0,1,0,0.75))
plot(log10(rel_res)~log10(rel_crAss), data=HMP,
bg=cols[as.factor(HMP$country)], pch=21,
ylab = "Normalized ARG abundance (log10)",
xlab="Normalized crAssphage abundance (log10)",
cex=2,
ylim=c(2.5, 4.5))
par(fig=c(0,1,0.5,1),new=T)
boxplot(log10(rel_crAss)~country,data=HMP,col=cols,
axes=F,xlab=NULL,ylab=NULL,
horizontal = T)
axis(2,at=c(1,2,3),labels=c("China", "Europe", "US"),las=1)
title("a",adj=0,line=0)
ae1e62fa72f772b08b856070e24d6e5c.png
image.png

欢迎大家关注我的公众号

小明的数据分析笔记本504f1e24bc74c123600b6b0cbbbd72f8.png

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/98720.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息