1. 程式人生 > >lattice繪圖工具

lattice繪圖工具

library(lattice)
data1 <- data.frame(x=seq(0,14),y=seq(3,17),z=rep(c("a","b","c"),times=5))
xyplot(y~x,data = data1)

在這裡插入圖片描述
在這裡插入圖片描述

引數 含義
grid.pars 網格圖形引數
fontsize 用於文字和符號兩個元件(每個元件都是數字標量)的列表
clip 面板和條帶兩個元件的列表(每個元件都有一個字串,“開”或“關”)

在這裡插入圖片描述

show.settings()

在這裡插入圖片描述

lattice包通過顏色區分不同組別而不是形狀。
xyplot(y~x,groups = z,data = data1)
mysettings <- trellis.par.get()
mysettings$superpose.symbol$col <-"black"
mysettings$superpose.symbol$pch <-1:10
trellis.par.set(mysettings)
xyplot(y~x,groups = z,data = data1)

在這裡插入圖片描述
在這裡插入圖片描述
條件變數
graph_function(formula|v,data=,options)
如果條件變數為連續型,需要轉為離散型

xyplot(y~x|z,data = data1,layout=c(3,1))

在這裡插入圖片描述
面板函式
在這裡插入圖片描述

mypanel <- function(...){
  panel.abline(a=1,b=1)
  panel.xyplot(...)
}
xyplot(y~x|z,data = data1,layout=c(3,1),panel = mypanel)

在這裡插入圖片描述
分組變數
將不同水平的變數疊加到一起

densityplot(~mpg,data = mtcars,lty=1:2,col=1:2,lwd=2,
            groups = factor(am),
            main=list("MPG分佈",cex=1.5),
            xlab = "英里/加侖",
            key=list(column=2,space="bottom",
                     title="型別(0=自動,1=手動)",
                     text=list(levels(factor(mtcars$am))),
                     lines=list(lty=1:2,col=1:2,lwd=2)))

在這裡插入圖片描述
頁面擺放
藉助plot函式的splite和position

graph1 <- xyplot(mpg~wt,data = mtcars,xlab = "重量",ylab = "英里/加侖")
displacement <- equal.count(mtcars$disp,number=3,overlap=0)
graph2 <- xyplot(mpg~wt|displacement,data= mtcars,layout=c(3,1),
                 xlab = "重量",ylab = "英里/加侖")
plot(graph1,split = c(1,1,2,1))
plot(graph2,split = c(2,1,2,1),newpage = FALSE)

在這裡插入圖片描述

plot(graph1,position = c(0,0,0.5,1))      #圖形左下、右上座標
plot(graph2,position = c(0.6,0.3,1,1),newpage = FALSE)

在這裡插入圖片描述
lattice包繪圖函式的常用引數

引數 含義
x 要繪製的物件
data x為表示式時,動用一個數據框
allow.multiple 對於Y1+Y2X/Z,TRUE時重疊繪製Y1X和Y2X,FALSE時繪製Y1+Y2X
outer FALSE繪製疊加,TRUE不在一個面板顯示
box.ratio 內部矩形長寬比
horizontal 水平或者垂直
panel 面板函式
aspect 不同面板的寬高比
groups
auto.keys 新增分組變數的圖例符號
prepanel
strip
xlab,ylab
scales
subscripts
subset
xlim,ylim
drop.unused.levels
default.scales
options

barchart

trellis.par.get("axis.text")
trellis.par.set(list(axis.text = list(cex=1)))
barchart(Titanic,layout=c(4,1),auto.key=TRUE)

在這裡插入圖片描述

barchart(Titanic,layout=c(4,1),auto.key=TRUE,scales = list(x="free"))

在這裡插入圖片描述

barchart(Sex~Freq|Class+Age,data = as.data.frame(Titanic),groups=Survived,
         stack=TRUE,layout=c(8,1),auto.key=TRUE,scales=list(x="free"))

在這裡插入圖片描述

barchart(Sex~Freq|Class+Age,data = as.data.frame(Titanic),groups=Survived,
         stack=TRUE,layout=c(8,1),auto.key=list(title="Survived",columns=2),scales=list(x="free"))

在這裡插入圖片描述
點圖

dotplot(VADeaths,pch=1:4,col=1:4,main=list("死亡率",cex=1.5),
        xlab="比率/千人",
        key=list(column=4,text=list(colnames(VADeaths)),points=list(pch=1:4,col=1:4)))

在這裡插入圖片描述
dotplot(VADeaths,groups = FALSE,layout=c(1,4),aspect=0.5,origin=0,type=c(“p”,“h”))

在這裡插入圖片描述
直方圖
histogram(x,
data,
allow.multiple, outer = TRUE,
auto.key = FALSE,
aspect = “fill”,
panel = lattice.getOption(“panel.histogram”),
prepanel, scales, strip, groups,
xlab, xlim, ylab, ylim,
type = c(“percent”, “count”, “density”),
nint = if (is.factor(x)) nlevels(x)
else round(log2(length(x)) + 1),
endpoints = extend.limits(range(as.numeric(x),
finite = TRUE), prop = 0.04),
breaks,
equal.widths = TRUE,
drop.unused.levels =
lattice.getOption(“drop.unused.levels”),
…,
lattice.options = NULL,
default.scales = list(),
default.prepanel =
lattice.getOption(“prepanel.default.histogram”),
subscripts,
subset)
核密度圖
densityplot(x,
data,
allow.multiple = is.null(groups) || outer,
outer = !is.null(groups),
auto.key = FALSE,
aspect = “fill”,
panel = lattice.getOption(“panel.densityplot”),
prepanel, scales, strip, groups, weights,
xlab, xlim, ylab, ylim,
bw, adjust, kernel, window, width, give.Rkern,
n = 512, from, to, cut, na.rm,
drop.unused.levels =
lattice.getOption(“drop.unused.levels”),
…,
lattice.options = NULL,
default.scales = list(),
default.prepanel =
lattice.getOption(“prepanel.default.densityplot”),
subscripts,
subset)
帶狀圖
panel.stripplot(x, y, jitter.data = FALSE,
factor = 0.5, amount = NULL,
horizontal = TRUE, groups = NULL,
…,
identifier = “stripplot”)
Q-Q圖
根據理論分佈繪製樣本的分位數-分位數圖
qqmath(x,
data,
allow.multiple = is.null(groups) || outer,
outer = !is.null(groups),
distribution = qnorm,
f.value = NULL,
auto.key = FALSE,
aspect = “fill”,
panel = lattice.getOption(“panel.qqmath”),
prepanel = NULL,
scales, strip, groups,
xlab, xlim, ylab, ylim,
drop.unused.levels = lattice.getOption(“drop.unused.levels”),
…,
lattice.options = NULL,
default.scales = list(),
default.prepanel = lattice.getOption(“prepanel.default.qqmath”),
subscripts,
subset)
箱型圖
bwplot(x,
data,
allow.multiple = is.null(groups) || outer,
outer = FALSE,
auto.key = FALSE,
aspect = “fill”,
panel = lattice.getOption(“panel.bwplot”),
prepanel = NULL,
scales = list(),
strip = TRUE,
groups = NULL,
xlab,
xlim,
ylab,
ylim,
box.ratio = 1,
horizontal = NULL,
drop.unused.levels = lattice.getOption(“drop.unused.levels”),
…,
lattice.options = NULL,
default.scales,
default.prepanel = lattice.getOption(“prepanel.default.bwplot”),
subscripts = !is.null(groups),
subset = TRUE)
散點圖矩陣

splom(mtcars[c(1,3:7)],groups = mtcars$cyl,
      pscales = 0 ,pch=1:3,col=1:3,
      varnames = c("M","D","G","R","W","1/4"),
      key=list(columns=3,title="數值",text=list(levels(factor(mtcars$cyl))),
               points=list(pch=1:3,col=1:3)))

在這裡插入圖片描述
三維水平圖

data(Cars93,package = "MASS") 
cor_car93 <- cor(Cars93[,!sapply(Cars93,is.factor)],use = "pair")
levelplot(cor_car93,scales=list(x=list(rot=90)))

在這裡插入圖片描述
三維等高線圖

contourplot(volcano,cuts=20,label=FALSE)

在這裡插入圖片描述
三維散點圖

parset <- list(axis.line = list(col="transparent"),clip=list(panel="off"))
cloud(Sepal.Length~Petal.Length*Petal.Width,data = iris,
      cex=.8,pch=1:3,col=c("blue","green","red"),
      groups = Species,screen=list(z=20,x=-70,y=0),
      par.settings=parset,scales = list(col="black"),
      key=list(title="種類",
      column=3,
      space="bottom",
      text=list(levels(iris$Species)),
      points=list(pch=1:3,col=c("blue","green","red"))))

在這裡插入圖片描述
三維曲面圖

wireframe(volcano,shade=TRUE,aspect = c(56/90,0.4),
          light.source=c(10,0,10))

在這裡插入圖片描述