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

今天继续 跟着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

画图用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

具体可以参考链接 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值