- 跟着Nature Communications学画图~Figur
- 跟着Nature Communications学画图~Figur
- 跟着Nature Communications学画图
- 跟着Nature Communications 学画图~ggpl
- 跟着Nature Communications 学画图~ggpl
- 跟着Nature Communications 学画图~ggpl
- R语言基础绘图函数散点图~跟着Nature Communicat
- 第一个跟着Nature Communications学画图系列合
- 跟着Nature Communications学作图 -- 复杂
- 跟着Nature Communications学作图:synvi
今天开始 跟着Nature Communications学画图 系列第一篇。学习用R语言的基础绘图函数画箱线图。
对应的 Nature Communications 的论文是
Fecal pollution can explain antibiotic resistance gene abundances in anthropogenically impacted environments
数据和代码链接是
https://github.com/karkman/crassphage_project
今天学习这幅截图中用红色方框圈住的箱线图

用到的数据是data文件夹下的HMP.txt文件。
第一步是读入数据
HMP <- read.table("data/HMP.txt")
查看数据维度
dim(HMP)

这个数据集里有449行,10列
做箱线图用到的数据是 rel_crAss 和 country,country是分类变量,rel_crAss是连续变量,画图的时候对这个数值进行了log10转换
基础绘图函数画箱线图用boxplot()函数。直接指定数据集和变量就可以了
先尝试一下用原始数据即不做log10转换画箱线图
boxplot(rel_crAss~country,data=HMP)

可以看到,不做log10转换其中一组数据方差较大的话另外的数据箱子压缩的就很扁,看起来可能不太好看。
log10转换的数据画图
boxplot(log10(rel_crAss)~country,data=HMP)

接下来是对图进行美化
首先是填充颜色,用到的是col参数
boxplot(log10(rel_crAss)~country,data=HMP,
col=c("red","blue","green"))

现在是垂直显示,将其改为水平显示,用到的是horizontal = TRUE参数
boxplot(log10(rel_crAss)~country,data=HMP,
col=c("red","blue","green"),
horizontal = TRUE)

去掉坐标轴的边框,加 axes=F参数
boxplot(log10(rel_crAss)~country,data=HMP,
col=c("red","blue","green"),
horizontal = TRUE,
axes=F)

自定义添加一个Y轴
boxplot(log10(rel_crAss)~country,data=HMP,
col=c("red","blue","green"),
horizontal = TRUE,
axes=F)
axis(2, at=1:3, labels=c("China", "Europe", "US"), las=0)

给图片添加一个标题
boxplot(log10(rel_crAss)~country,data=HMP,
col=c("red","blue","green"),
horizontal = TRUE,
axes=F)
axis(2, at=1:3, labels=c("China", "Europe", "US"), las=0)
title("A", adj = 0, line = 1)

欢迎大家关注我的公众号
小明的数据分析笔记本
网友评论