1 plot函数ide
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能够写为:ui
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
经过不一样的符号展现每一个截面的数据spa
问题:3d
1 )若是Transect被编码的值不在pch的取值范围,那么有些截面的数据不能被展现blog
2 )若是Transect的长度与BARESOIL或者R的长度不同,则会出现错误图形ci
3) R不接受因子做为pch的参数值get
对pch是同变量it
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)