今天繼續(xù) 跟著Nature Communications學(xué)畫圖系列第四篇涮瞻。學(xué)習(xí)R語言ggplot2包畫散點(diǎn)圖,然后分組添加擬合曲線加酵。對(duì)應(yīng)的是論文中的Figure2
image.png
對(duì)應(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í)素材。
首先是讀入數(shù)據(jù)、查看數(shù)據(jù)維度
crass_impact <- read.table("data/crass_impact.txt")
dim(crass_impact)
head(crass_impact)
最基本的散點(diǎn)圖
ggplot(crass_impact,aes(x=rel_crAss,y=rel_res))+
geom_point()
image.png
指定變量填充顏色
ggplot(crass_impact,aes(x=rel_crAss,y=rel_res,color=country))+
geom_point()
image.png
自定義顏色值
cols <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00")
ggplot(crass_impact,aes(x=rel_crAss,y=rel_res,color=country))+
geom_point()+
scale_color_manual(values = cols)
image.png
根據(jù)指定變量映射點(diǎn)的形狀毕源,并改變點(diǎn)的大小
ggplot(crass_impact,aes(x=rel_crAss,y=rel_res,color=country))+
geom_point(aes(shape=crAss_detection),size=5)+
scale_color_manual(values = cols)
image.png
添加擬合曲線
ggplot(crass_impact,aes(x=rel_crAss,y=rel_res,color=country))+
geom_point(aes(shape=crAss_detection),size=5)+
scale_color_manual(values = cols)+
geom_smooth(method = 'lm')
image.png
分別對(duì)x和y取log10進(jìn)行轉(zhuǎn)換
ggplot(crass_impact,aes(x=rel_crAss,y=rel_res,color=country))+
geom_point(aes(shape=crAss_detection),size=5)+
scale_color_manual(values = cols)+
geom_smooth(method = 'lm')+
scale_x_log10()+
scale_y_log10()
image.png
更改作圖的主題
ggplot(crass_impact,aes(x=rel_crAss,y=rel_res,color=country))+
geom_point(aes(shape=crAss_detection),size=5)+
scale_color_manual(values = cols)+
geom_smooth(method = 'lm')+
scale_x_log10()+
scale_y_log10()+
theme_classic()
image.png
更改x軸、y軸的標(biāo)題
ggplot(crass_impact,aes(x=rel_crAss,y=rel_res,color=country))+
geom_point(aes(shape=crAss_detection),size=5)+
scale_color_manual(values = cols)+
geom_smooth(method = 'lm')+
scale_x_log10()+
scale_y_log10()+
theme_classic()+
labs(y = "Normalized ARG abundance",
x="Normalized crAssphage abundance")
image.png
更改圖例的標(biāo)題
ggplot(crass_impact,aes(x=rel_crAss,y=rel_res,color=country))+
geom_point(aes(shape=crAss_detection),size=5)+
scale_color_manual(values = cols)+
geom_smooth(method = 'lm')+
scale_x_log10()+
scale_y_log10()+
theme_classic()+
labs(y = "Normalized ARG abundance",
x="Normalized crAssphage abundance",
color="Study",
shape="crAssphage detection")
image.png
這里注意到更改圖例的標(biāo)題以后圖例的順序也變了陕习。原來圖例的默認(rèn)順序也是按照首字母排序來的霎褐。
還想改圖中的哪些地方可以留言討論
歡迎大家關(guān)注我的公眾號(hào)
小明的數(shù)據(jù)分析筆記本