R语言基础绘图函数散点图的示例分析

R语言基础绘图函数散点图的示例分析

今天就跟大家聊聊有关R语言基础绘图函数散点图的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

被红色框线圈住的散点图

R语言基础绘图函数散点图的示例分析  

基础绘图函数箱线图

有人留言说 和原图不是很像,因为配色没有按照论文中提供的代码来。 下面是完全重复论文中的代码

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)
 
R语言基础绘图函数散点图的示例分析  
image.png
 第二部分 基础绘图函数散点图
  • 读入数据
HMP<-read.table("data/HMP.txt")
 
  • 最基本的散点图
plot(rel_res~rel_crAss,data=HMP)
 
R语言基础绘图函数散点图的示例分析  
image.png

画图用plot()函数,需要指定画图用到的变量y和x,还有画图用到的数据data

原始代码分别对 rel_res 和 rel_crAss取了log10

plot(log10(rel_res)~log10(rel_crAss),data=HMP)
 
R语言基础绘图函数散点图的示例分析  
image.png

取log10以后可以看到散点分布的更加均匀了。

 接下来就是对图进行美化了
  • 按照国家分组填充颜色
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)
 
R语言基础绘图函数散点图的示例分析  
image.png
  • 更改点的大小
plot(log10(rel_res)~log10(rel_crAss), data=HMP, 
    bg=cols[as.factor(HMP$country)],pch=21,cex=2)
 
R语言基础绘图函数散点图的示例分析  
image.png
  • 更改x轴和y轴的标签
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)
 
R语言基础绘图函数散点图的示例分析  
image.png
  • 更改坐标轴的范围
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))
 
R语言基础绘图函数散点图的示例分析  
image.png

接下来将箱线图和散点图按照上下拼接到一起,用到的是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)
 
R语言基础绘图函数散点图的示例分析


看完上述内容,你们对R语言基础绘图函数散点图的示例分析有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。