图形类型
|
函数及说明
|
表达式示例
|
示例
|
三维等高线图 | contourpolt() |
z~x*y
|
contourplot(volcano)
|
三维水平图 | levelpolt() |
z~y*x
|
levelplot(volcano)
|
三维散点图 | cloud() |
z~x*y|A
|
|
三维线框图 | wireframe() |
z~y*x
|
wireframe(volcano)
|
条形图 | barchart() | x~A或A~x | |
箱线图 | bwplot() |
x~A或A~x
|
bwplot(mtcars$mpg)
|
点图 | dotplot() | ~x|A | |
直方图 | histogram() | ~x |
|
核密度图 | densityplot() | ~x|A*B | |
平行坐标图 |
parallelplot()
在这个函数中可以设置
alpha = 0.01参数控制线条粗细
|
dataframe | |
散点图 | xyplot() | y~x|A | |
散点图矩阵 | splom() | dataframe |
splom(mtcars[c(1,3,4,5)])
|
带状图 | stripplot() | A~x或x~A |
stripplot(mtcars$mpg~factor(mtcars$cyl))
|
注意,在这些表达式中,小写字母代表数值变量,大写字母表示类型变量
|
选项
|
描述
|
示例
|
aspect | 数值,设定每个面板中图形的宽高比 |
见index.cond
|
col、pch、lty、lwd |
向量,分别设置图形中的颜色、符号、线条类型和宽度
pch除了使用数值变量,还可以直接使用字符变量
|
xyplot(lat ~ long | cut(depth, 3), data = quakes,
aspect = "iso", pch = ".", cex = 2, type = c("p", "g"), xlab = "Longitude", ylab = "Latitude", strip = strip.custom(strip.names = TRUE, var.name = "Depth")) |
groups | 用来分组的变量(因子) |
mtcars$transmission <- factor(mtcars$am,levels = c(0,1),labels = c("Automatic","Manual"))
colors <- c("red","blue")
lines <- c(1,2)
points <- c(16,17)
key.trans <- list(title = "Trasmission",space = "bottom",columns = 2,
text = list(levels(mtcars$transmission)),
points = list(pch = points,col = colors),
lines = list(col = colors,lty = lines),
cex.title = 1,cex = .9)
densityplot(~mpg,data = mtcars,
groups = transmission,
main = "MPG Distribution by Transmission Type",
xlab = "Mile per Gallon",
pch = points,lty = lines,col = colors,lwd = 2,jitter = .005,
key = key.trans)
|
index.cond | 列表,设置面板的展示顺序 |
mypanel <-function(x,y){
panel.xyplot(x,y,pch=19)
panel.rug(x,y)
panel.grid(h=-1,v=-1)
panel.lmline(x,y,col = "red",lwd = 1,lty = 2)
}
xyplot(mtcars$mpg~mtcars$wt|displacement,
main = "Miles per Gallon vs. Weight by Engine Displacement",
scales = list(cex=.8,col = "red"),
xlab = "Weight",ylab = "Mile per Gallon",
layout = c(3,1),aspect = 1.5,panel = mypanel,index.cond = list(c(2,1,3)))
#不设置index.cond = list(c(2,1,3))
xyplot(mtcars$mpg~mtcars$wt|displacement,
main = "Miles per Gallon vs. Weight by Engine Displacement",
scales = list(cex=.8,col = "red"),
xlab = "Weight",ylab = "Mile per Gallon",
layout = c(3,1),aspect = 1.5,panel = mypanel)
|
key(或auto.key) | 函数,添加分组变量的图例符号 | 见groups |
layout | 两元素数值型向量,设定面板的摆放方式(行数和列数);如需要,可以添加第三个元素,以指定页数 | 见panel |
main、sub | 字符向量,设定主标题和副标题 |
|
panel
|
函数,设定每个面板要生成的图形
|
见index.cond
|
scales |
列表,添加坐标轴标注信息
|
见index.cond
|
strip | 函数,设定面板条带区域 | |
split、position |
数值向量,在一页上绘制多个图形.
由于lattice函数不识
别par() 设置,因此你需要另辟蹊径。最简单的方法便是先将lattice图形存储到对象中,然后利
用plot() 函数中的split =或position =选项来进行控制。
|
graph1 <- histogram(~height|voice.part,data = singer,
main = "Heights of Choral Singers by Voice Part")
graph2 <- densityplot(~height,data = singer,groups = voice.part,
plot.points = F,auto.key = list(columns = 4))
plot(graph1,split = c(1,1,1,2))
plot(graph2,
split = c(1,2,1,2),#split 参数说明,c(列位置号码,行位置号码,列数,行数)
newpage = F)
plot(graph1,position = c(0,.3,1,1))
plot(graph2,
position = c(0,0,1,.3),#position = c(xmin, ymin, xmax, ymax)
newpage = F)
|
type | 字符型向量,设定一个或多个散点图的绘图参数(如p=点、l=线、r=回归、smooth=平滑曲线、g=格点) |
|
xlab、ylab | 字符向量,设定横轴和纵轴标签 | |
xlim,ylim | 两个元素的数值向量,分别设置横轴和纵轴的最小值和最大值 |
equal.count(x,number=,overlap=):
将一个连续变量变成一个类似因子类型的,single类,可以用作画图条件,
例如:
(displacement <- equal.count(mtcars$disp,number = 3,overlap = 0))#overlap设置重叠部分
xyplot(mtcars$mpg~mtcars$wt|displacement,
main = "Miles per Gallon vs. Weight by Engine Displacement",
xlab = "Weight",ylab = "Mile per Gallon",
layout = c(3,1),aspect = 1.5)
|
panel.grid/panel.xyplot/......
面板函数,在使用lattice包画图的时候,供panel参数使用,能够在一幅图中组织多个图像。
示例将上面
|
update.trellis(graph,.....):
这个函数用来修改已经做好的图的参数,例如要将一副已经做好的图的线条颜色改成红色,就可以用这个函数。
|
show.settings()、trellis.par.get()和trellis.par.set(mysettings)
我们学习了如何利用par() 函数查看和设置默认的图形参数。不过它们只对R中简单的图形系统生成的图形有效,对于lattice图形来说这些设置是无效的。在lattice图形中, lattice函数默认的图形参数包含在一个很大的列表对象中,你可通过trellis.par.get() 函数来获
取,并用trellis.par.set() 函数来修改。 show.settings() 函数可展示当前的图形参数设
置情况。
示例:
show.settings()
mysettings <- trellis.par.get()
mysettings$superpose.symbol
mysettings$superpose.symbol$pch <- 1:10
trellis.par.set(mysettings)
show.settings()
以上代码把lattice包的默认值从以下的第一幅图变成第二幅:
变成
|