R語言基礎(chǔ)繪圖函數(shù)散點(diǎn)圖~跟著Nature Communications學(xué)畫圖~Figure1

今天繼續(xù) 跟著Nature Communications學(xué)畫圖系列第二篇。學(xué)習(xí)R語言基礎(chǔ)繪圖函數(shù)畫散點(diǎn)圖。

對應(yīng)的 Nature Communications 的論文是 Fecal pollution can explain antibiotic resistance gene abundances in anthropogenically impacted environments

這篇論文數(shù)據(jù)分析和可視化的部分用到的數(shù)據(jù)和代碼全部放到了github上 https://github.com/karkman/crassphage_project

非常好的R語言學(xué)習(xí)素材。

今天學(xué)習(xí)Figure1中被紅色框線圈住的散點(diǎn)圖

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

https://mp.weixin.qq.com/s/dFM1bJoEGPjWhEFTaQ0Krg
跟著Nature Communications學(xué)畫圖~ Figure1 ~基礎(chǔ)繪圖函數(shù)箱線圖

這篇文章中有人留言說 和原圖不是很像,因?yàn)榕渖珱]有按照論文中提供的代碼來。
下面是完全重復(fù)論文中的代碼

cols <- c("#E69F00", "#56B4E9", "#009E73")
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)
image.png
第二部分 基礎(chǔ)繪圖函數(shù)散點(diǎn)圖
  • 讀入數(shù)據(jù)
HMP<-read.table("data/HMP.txt")
  • 最基本的散點(diǎn)圖
plot(rel_res~rel_crAss,data=HMP)
image.png

畫圖用plot()函數(shù),需要指定畫圖用到的變量y和x,還有畫圖用到的數(shù)據(jù)data

原始代碼分別對 rel_res 和 rel_crAss取了log10

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

取log10以后可以看到散點(diǎn)分布的更加均勻了。

接下來就是對圖進(jìn)行美化了
  • 按照國家分組填充顏色
cols <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
plot(log10(rel_res)~log10(rel_crAss), data=HMP, 
     bg=cols[as.factor(HMP$country)],pch=21)
image.png
  • 更改點(diǎn)的大小
plot(log10(rel_res)~log10(rel_crAss), data=HMP, 
     bg=cols[as.factor(HMP$country)],pch=21,cex=2)
image.png
  • 更改x軸和y軸的標(biāo)簽
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)
image.png
  • 更改坐標(biāo)軸的范圍
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))
image.png

接下來將箱線圖和散點(diǎn)圖按照上下拼接到一起,用到的是par(fig=c(a,b,c,d)),這里需要滿足 a<b,c<d

具體可以參考鏈接
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)
image.png

歡迎大家關(guān)注我的公眾號
小明的數(shù)據(jù)分析筆記本

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。