当前位置:天才代写 > tutorial > 其他教程 > R 进修条记《八》 R语言初学者指南–基本画图东西

R 进修条记《八》 R语言初学者指南–基本画图东西

2017-12-04 08:00 星期一 所属: 其他教程 浏览:638

 1 plot函数

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

R 学习笔记《八》 R语言初学者指南--根基绘图对象

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

R 学习笔记《八》 R语言初学者指南--根基绘图对象

个中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)
> 

R 学习笔记《八》 R语言初学者指南--根基绘图对象 

增加了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

R 学习笔记《八》 R语言初学者指南--根基绘图对象

通过差异的标记展示每个截面的数据

问题:

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)
> 

R 学习笔记《八》 R语言初学者指南--根基绘图对象

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

通过 ? points 呼吁查察 plot函数的辅佐信息可以看到:

R 学习笔记《八》 R语言初学者指南--根基绘图对象

> 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)
> 
> 

绿色实心点暗示

R 学习笔记《八》 R语言初学者指南--根基绘图对象

对col参数利用参数,需求

对从1958年到1974年的调查值绘制玄色实心方块,而对其他的数据回收赤色实习圆

那么:

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

R 学习笔记《八》 R语言初学者指南--根基绘图对象

改变画图标记的尺寸

画图标记的尺寸可以通过cex选项改变

#p#分页标题#e#

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

结果:

R 学习笔记《八》 R语言初学者指南--根基绘图对象

对cex利用向量

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

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

结果:

R 学习笔记《八》 R语言初学者指南--根基绘图对象

添加滑腻线

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

结果图

R 学习笔记《八》 R语言初学者指南--根基绘图对象

上面打代码应用滑腻线从头绘制图形,并通过lines呼吁在图形上添加符合的滑腻先

 由于lines呼吁的第一顺序是凭据顺序毗连的,所以上图的线条欠悦目。

#p#分页标题#e#

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

结果图

R 学习笔记《八》 R语言初学者指南--根基绘图对象

总结:

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) 

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式