R 學習筆記《八》 R語言初學者指南--基礎繪圖工具

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)

相關文章
相關標籤/搜索