今天继续 跟着Nature Communications学画图系列第四篇。学习R语言ggplot2包画散点图,然后分组添加拟合曲线。对应的是论文中的Figure2
image.png
对应的 Nature Communications 的论文是 Fecal pollution can explain antibiotic resistance gene abundances in anthropogenically impacted environments
这篇论文数据分析和可视化的部分用到的数据和代码全部放到了github上 https://github.com/karkman/crassphage_project
非常好的R语言学习素材。
crass_impact <- read.table("data/crass_impact.txt")
dim(crass_impact)
head(crass_impact)
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()
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)
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)
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')
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()
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()
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")
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")
这里注意到更改图例的标题以后图例的顺序也变了。原来图例的默认顺序也是按照首字母排序来的。
还想改图中的哪些地方可以留言讨论