• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

R 学习笔记《八》 R语言初学者指南--基础绘图工具 - Martin-9

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

R 学习笔记《八》 R语言初学者指南--基础绘图工具

1 plot函数

setwd("E:/R/R-beginer-guide/data/RBook")
Veg <- read.table(file = "Vegetation2.txt",header = TRUE)
plot(Veg$BARESOIL,Veg$R)

 

为生成的图形添加标签,x,y轴以及设置x,y轴的界限 

其中xlim可以写为:

xlim = c(min(Veg$BARESOIL,na.rm=TRUE),max(Veg$BARESOI,na.rm=TRUE))
xlim = c(min(Veg$BARESOIL,na.rm=TRUE),max(Veg$BARESOI,na.rm=TRUE))
plot(x=Veg$BARESOIL,y=Veg$R,xlab="Exposed soil",
+       ylab="Species richess",main="Scatter plot",
+       xlim=c(0,45),ylim=c(4,19),pch=16)

  

 

增加了pch=16这个选项,通过是实心点来替换空心点

plot(x=Veg$BARESOIL,y=Veg$R,xlab="Exposed soil",
+       ylab="Species richess",main="Scatter plot",
+       xlim=c(0,45),ylim=c(4,19),pch=Veg$Transect)
pch=Veg$Transect

 

通过不同的符号展示每个截面的数据

问题:

1 )如果Transect被编码的值不在pch的取值范围,那么有些截面的数据不能被展示

2 )如果Transect的长度与BARESOIL或者R的长度不一样,则会出现错误图形

3) R不接受因子作为pch的参数值

 

对pch是同变量

Veg$Time2 <- Veg$Time
Veg$Time2[Veg$Time <= 1974] <- 1
Veg$Time2[Veg$Time > 1974] <- 16
Veg$Time2
[1]  1  1  1  1 16 16 16  1  1  1  1 16 16 16  1  1  1  1 16 16 16 16  1  1
[25]  1  1 16 16 16 16  1  1  1  1 16 16 16 16  1  1  1  1 16 16 16 16  1  1
[49]  1 16 16 16  1  1  1 16 16 16

plot(x=Veg$BARESOIL,y=Veg$R,xlab="Exposed soil",
+       ylab="Species richess",main="Scatter plot",
+       xlim=c(0,45),ylim=c(4,19),pch=Veg$Time2)

 

1974及1974年以前的数据通过空心圆表示,1974年以后的数据通过实心点表示

 

通过 ? points 命令查看 plot函数的帮助信息可以看到:

plot(x=Veg$BARESOIL,y=Veg$R,xlab="Exposed soil",
+       ylab="Species richess",main="Scatter plot",
+       xlim=c(0,45),ylim=c(4,19),col=3,pch=16)

  

绿色实心点表示

 

对col参数使用参数,需求

对从1958年到1974年的观察值绘制黑色实心方块,而对其他的数据采用红色实习圆

那么:

Veg$Time2 <-Veg$Time
Veg$Time2[Veg$Time <=1974]  <- 15
Veg$Time2[Veg$Time >1974]  <- 16
Veg$Col2 <- Veg$Time
Veg$Col2[Veg$Time <=1974] <- 1
Veg$Col2[Veg$Time >1974] <- 2
  
plot(x=Veg$BARESOIL,y=Veg$R,xlab="Exposed soil",
      ylab="Species richess",main="Scatter plot",
      xlim=c(0,45),ylim=c(4,19),pch=Veg$Time2, col=Veg$Col2)

  

 

改变绘图符号的尺寸

绘图符号的尺寸可以通过cex选项改变

plot(x=Veg$BARESOIL,y=Veg$R,xlab="Exposed soil",
      ylab="Species richess",main="Scatter plot",
      xlim=c(0,45),ylim=c(4,19),pch=Veg$Time2,
      col=Veg$Col2,cex=1.5)

 

效果:

 

对cex使用向量

需求:大号显示2002的界面数据

Veg$Cex2 <- Veg$Time
Veg$Cex2[Veg$Time == 2002] <- 2
Veg$Cex2[Veg$Time != 2002] <- 1
 plot(x=Veg$BARESOIL,y=Veg$R,xlab="Exposed soil",
      ylab="Species richess",main="Scatter plot",
      xlim=c(0,45),ylim=c(4,19),cex=Veg$Cex2,pch=16,col=3)

  

效果:

 

 

添加平滑线

plot(x=Veg$BARESOIL,y=Veg$R,xlab="Exposed soil",
     ylab="Species richess",main="Scatter plot",
     xlim=c(0,45),ylim=c(4,19))
      
M.loess <- loess(R ~ BARESOIL,data = Veg)
Fit <- fitted(M.loess)
lines(Veg$BARESOIL,Fit)

  

效果图

上面打代码应用平滑线重新绘制图形,并通过lines命令在图形上添加合适的平滑先

 由于lines命令的第一顺序是按照顺序连接的,所以上图的线条不好看。

plot(x=Veg$BARESOIL,y=Veg$R,xlab="Exposed soil",
     ylab="Species richess",main="Scatter plot",
     xlim=c(0,45),ylim=c(4,19))
      
M.loess <- loess(R ~ BARESOIL,data = Veg)
 
Fit <- fitted(M.loess)
 
Ord1 <- order(Veg$BARESOIL)
lines(Veg$BARESOIL[Ord1],Fit[Ord1],lwd=3,lty=2)

  

效果图

 

总结:

plot                y对x的图形      plot(y,x,xlab="",ylab="",xlim=c(1,4),ylim=c(4,9),main="main",pch=1,col=2)

lines                在以存在的图形上添加线        lines(x,y,lwd=3,lyt=2,col=1)

order               确定数据的顺序                   order(x)

loess               使用LOESS平滑                   M<-loess(y~x)

fitted               得到拟合值                       fitted(M)

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Rmarkdown用法与R语言动态报告发布时间:2022-07-18
下一篇:
R语言中的fitted() 和 predict()发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap