R語言:lattice程序包
lattice程序包
lattice程序包主要用於處理結構稍稍複雜一些的數據集,入門容易,作圖速度較快,圖形函數種類較多,還可以進行三維繪圖。
lattice程序包中主要的函數如下:
實例
調用程序包ggplot2中的diamonds數據集,該數據集包括了磚石的重量、顏色、價格和質量信息等,共10個變量。部分數據如下:
安裝並加載程序包
install.packages("ggplot2")
library("ggplot2")
data(diamonds,package="ggplot2")
library(lattice)
從數據中隨機抽取1000個樣本作圖
sample=diamonds[sample(nrow(diamonds),1000),]
xyplot(price~carat,data=sample,groups=cut,auto.key=list(corner=c(1,0)),type=c("p","smooth"),span=0.7,main="Price VS. Carat")
二元圖
bwplot(color~price | cut,data=diamonds,main="Box-and-Whisker Plots of Price")
箱線圖
histogram(~price | color,data=diamonds,layout=c(2,4))
x=seq(-pi,pi,len = 20)
y=seq(-pi,pi,len = 20)
g=expand.grid(x=x,y=y) #構造一個數據框,內部變量爲x,y
g$z=sin(sqrt(g$x^2+g$y^2)) #對數據框g添加變量z
wireframe(z~x*y,data=g,drape=T,aspect=c(3,1),colorkey=TRUE,main=expression(z=sin(sqrt(x^2+y^2))))