R绘图lattice包
来源:互联网 发布:思想深度 知乎 编辑:程序博客网 时间:2024/05/16 11:22
lattice包:
学习此包的网站:
http://lmdvr.r-forge.r-project.org
http://cm.bell-labs.com/cm/ms/departments/sia/doc/trellis.user.pdf
另外lattice包有一个扩展包是基于lattice的,就是R的 latticeExtra包
由于它可以轻松生成栅栏图形,因此许多用户都会使用它。
lattice包提供了丰富的函数,可生成单变量图形(点图、核密度图、直方图、柱状图和箱线图)、双变量图形(散点图、带状图和平行箱线图)和多变量图形(三维图和散点图矩阵)。
各种高级绘图函数都服从以下格式:
graph_function(formula,data= ,options)
- graph_function是表16-2的第二栏列出的某个函数。
- formula指定要展示的变量和条件变量。
- data指定一个数据框。
- options是逗号分隔参数,用来修改图形的内容、摆放方式和标注。表16-3列出了一些常见的选项。
lattice包中的图形类型和相应函数
图形类型
函数及说明
表达式示例
示例
三维等高线图contourpolt()z~x*y
contourplot(volcano)
z~y*x
levelplot(volcano)
三维散点图cloud()z~x*y|A
z~y*x
wireframe(volcano)
条形图barchart()x~A或A~x 箱线图bwplot()x~A或A~x
bwplot(mtcars$mpg)
点图dotplot()~x|A 直方图histogram()~xparallelplot()
在这个函数中可以设置 alpha = 0.01参数控制线条粗细
dataframe parallelplot(mtcars[1:3])
散点图xyplot()y~x|A 散点图矩阵splom()dataframe splom(mtcars[c(1,3,4,5)])
带状图stripplot()A~x或x~Astripplot(mtcars$mpg~factor(mtcars$cyl))
注意,在这些表达式中,小写字母代表数值变量,大写字母表示类型变量
表达式形式通常为:
y~x|A*B
在竖线左边的变量称为主要(primary)变量,右边的变量称为条件(conditioning)变量。主要变量将变量映射到每个面板的坐标轴上,此处,y ~ x表示变量分别映射到纵轴和横轴上。对于单变量绘图,用~ x代替y ~ x即可;对于三维图形,用z ~ x*y代替y ~ x,而对于多变量绘图(散点图矩阵或平行坐标图)用一个数据框代替y ~ x即可。注意,条件变量总是可以自行挑选的。
根据上述的逻辑,~ x | A即展示因子A各个水平下数值型变量x的分布情况;y ~ x | A*B即展示因子A和B各个水平组合下数值型变量x和y间的关系。而A ~ x则表示类别型变量A在纵轴上,数值型变量x在横轴上进行展示。~ x表示仅展示数值型变量
lattice中高级绘图函数的常见选项
选项
描述
示例
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"))
aspect = "iso",pch = ".", cex = 2, type = c("p", "g"),
xlab = "Longitude", ylab = "Latitude",
strip = strip.custom(strip.names = TRUE, var.name = "Depth"))
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)
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=格点)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包的默认值从以下的第一幅图变成第二幅:
变成
1 0
- R绘图lattice包
- lattice 包高级绘图函数
- R语言lattice包介绍
- 慕课—R语言之数据可视化—学习笔记 3.4Lattice绘图系统
- R lattice作图基本知识
- 《数据挖掘与商务分析R语言》-1-数据预处理-R语言-Lattice包-条形图
- R语言绘图——Graphics包
- R语言绘图之ggplot2包
- 【R作图】lattice包,画多个分布柱形图,hist图纵轴转换为百分比
- R lattice包中的panel函数参数传输的一个问题
- lattice 包的用法
- 如何在lattice包绘图时在strip中使用表达式,例如$x^2$之类的文字
- R绘图
- R绘图
- r绘图
- R绘图
- lattice 包中的直方图绘制
- lattice函数包作图函详解
- leetcode contains duplicate III
- UIImage剪切图片为自定义大小
- WebSocket形象理解
- 快速索引 (对View的自定义,黑马程序员)
- C# DataGridView内容合并相同的行
- R绘图lattice包
- Codeforces Round #322 (Div. 2)
- Oracle基本用法
- light oj 1100 - Again Array Queries
- Android获取控件大小的方法
- 万维网WWW简介
- C语言使用正则表达式
- java简单的各种进制转换
- pygame编写的坦克游戏(七)