前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >跟着Nature Communications 学画图~ggplot2散点图分组添加拟合曲线

跟着Nature Communications 学画图~ggplot2散点图分组添加拟合曲线

作者头像
用户7010445
发布2020-11-13 09:55:25
4.3K0
发布2020-11-13 09:55:25
举报
文章被收录于专栏:小明的数据分析笔记本

今天继续 跟着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语言学习素材。

首先是读入数据、查看数据维度
代码语言:javascript
复制
crass_impact <- read.table("data/crass_impact.txt")
dim(crass_impact)
head(crass_impact)
最基本的散点图
代码语言:javascript
复制
ggplot(crass_impact,aes(x=rel_crAss,y=rel_res))+
  geom_point()

image.png

指定变量填充颜色
代码语言:javascript
复制
ggplot(crass_impact,aes(x=rel_crAss,y=rel_res,color=country))+
  geom_point()
自定义颜色值
代码语言:javascript
复制
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)
根据指定变量映射点的形状,并改变点的大小
代码语言:javascript
复制
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)
添加拟合曲线
代码语言:javascript
复制
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')
分别对x和y取log10进行转换
代码语言:javascript
复制
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()
更改作图的主题
代码语言:javascript
复制
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()
更改x轴、y轴的标题
代码语言:javascript
复制
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")
更改图例的标题
代码语言:javascript
复制
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")

这里注意到更改图例的标题以后图例的顺序也变了。原来图例的默认顺序也是按照首字母排序来的。

还想改图中的哪些地方可以留言讨论

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 首先是读入数据、查看数据维度
  • 最基本的散点图
  • 指定变量填充颜色
  • 自定义颜色值
  • 根据指定变量映射点的形状,并改变点的大小
  • 添加拟合曲线
  • 分别对x和y取log10进行转换
  • 更改作图的主题
  • 更改x轴、y轴的标题
  • 更改图例的标题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档