#清除以前的变量
rm(list = ls())
library(lattice)
attach(mtcars)
#赋予因子新的标签
gear <- factor(gear,levels = c(3,4,5),labels = c("3 gears","4 gears","5 gears"))
cyl <- factor(cyl,levels = c(4,6,8),labels = c("4 cylinders","6 cylinders","8 cylinders"))
##密度图 millers per gallon 出现次数的密度
densityplot(~mpg,main = "Density Plot",xlab = "Miles per Gallon")
densityplot(~mpg|cyl,main = "Density Plot by Number of Cylinders",xlab = "Miles per Gallon")
#箱线图
bwplot(cyl~mpg|gear,main = "Box Plots by Cylinders and Gears",xlab = "Miles per Gallon",ylab = "Cylinders")
#散点图
xyplot(mpg ~ wt|cyl*gear,main = "Scatter Plots by Cylinders and Gears",xlab = "Car Weight",ylab = "Miles per Gallon")
#三维散点图
cloud(mpg~wt*qsec|cyl,main = "3D Scatter Plots by Cylinders")
#点图
dotplot(cyl ~ mpg|gear,main = "Dots Plots by Number of Gears and Cylinders",xlab = "Miles Per Gallon")
#散点图矩阵
splom(mtcars[c(1,3,4,5,6)],main = "Scatter Plot Matrix for mtcars Data")
在 上面~mpg|cyl公式中, mpg|cyl 相当于y|x,只不过不是概率,而是在x的水平下y发生的次数
以上代码全部可以运行,自己动手,丰衣足食,图片不懂看下面图的解释
#加载lattice包
library(lattice)
#存储lattice包中的高级图
mygraph <- densityplot(~height|voice.part,data = singer)
plot(mygraph)
#updata 修改lattice图形对象,设置点线内容
#lwd="line width"
#pch 点的形状 cex 点的大小(1是原大小,0. 是缩小,大于一是放大)
update(mygraph,col = "red",pch = 16,cex = .8,jitter = 0.05, lwd = 2)
解释:此图中,每一个小矩形是一个voice.part因子,小矩形横坐标是height的值,纵坐标是这个值在这个水平下发生的概率
通常,条件变量是因子,上面提到的都是离散变量。但是,如果想以连续型变量为条件,该怎么办呢?
#equal.count 将会把连续型变量x分割到#区间中,重叠度为proportion,每个数值范围内的观测数相等,并
displacement <- equal.count(mtcars$disp,number = 3,overlap = 0)
plot(displacement)
xyplot(mpg ~ wt|displacement, data = mtcars, main = "Miles per Gallon vs Weight by Engin Displacesment",xlab = "Weight",ylab = "Miles per Gallon",layout = c(3,1),aspect = 1.5)
解释:此图中,每一个小矩形是一个displacement因子,小矩形横坐标是Weight的值,纵坐标是mpg的值,就相当于displcement将原数据分成三个部分,每一部分的mpg~wt图放在一起
panel = “”选项
#此处,我们将四个独立的绘制函数整合到自己的mypanel()函数中,然后通过xyplot()函
#数中的panel =选项将其展示出来。panel.xyplot()函数生成了填充圆圈(pch = 19)的
#散点图。panel.rug()函数在每个面板的x轴和y轴上添加了轴须线。panel.rug(x, FALSE)
#或panel.rug(FALSE, y)将分别只对横轴或纵轴添加轴须。panel.grid()函数添加了水平和
#竖直的网格线(使用负数强制它们与轴标签对齐)。最后,panel.lmline()函数添加了一条红
#色的(col="red")、标准粗细(lwd = 1)的虚线(lty = 2)回归线
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(mpg ~ wt|displacement, data = mtcars,
layout = c(3,1),aspect = 1.5,panel = mypanel,
main = "Miles per Gallon vs Weight by Engin Displacesment",xlab = "Weight",ylab = "Miles per Gallon")
继续添加平滑拟合曲线和水平均值线
#手动和自动车辆排放
mtcars$transmission <- factor(mtcars$am,levels = c(0,1),labels = c("Automatic","Manual"))
panel.smoother <- function(x,y){
panel.grid(h = -1,v = -1)
panel.xyplot(x,y)
#添加平滑拟合曲线
panel.loess(x,y)
#添加水平均值线
panel.abline(h = mean(y) , lwd = 2,lty = 2,col = "green")
}
#aspect 是单个图片长和宽的比
xyplot(mpg~disp|transmission,data = mtcars,scales = list(cex = .8,col = "red"),
panel = panel.smoother,
xlab = "Displacement",ylab = "Miles per Gallon",
main = "MGP vs Displacement by Transmission Type",
sub = "Dotted lines are Group Means",aspect = 1)
aspect 是单个图片长和宽的比
library(lattice)
mtcars$transmission <- factor(mtcars$am,levels = c(0,1),labels = c("Automatic","Manual"))
#group =选项默认将分组变量各个水平下的图形叠加到一起。绘制的点为空
#心圆圈,线为实线,水平信息用颜色来区分。不过以灰色调输出时,颜色将很难区分。稍后我们
#将看看如何修改这些默认值。
#auto.key = TRUE将可以创建一个摆放在图形上方的、初步的图例符号,你可将所做的修改以列表形式添加到自动图例符号中
densityplot(~mpg,data = mtcars,
group = transmission,
main = "MPG Distribution by Transmission Type",
xlab = "Miles per Gallon",
auto.key = TRUE
)
library(lattice)
colors = "darkgreen"
symbols = c(1:12)
linetype = c(1:3)
#keyValue setup
key.spectes <- list(title = "Plant",space = "right",text = list(levels(CO2$Plant)),
points = list(pch = symbols, col = colors))
xyplot(uptake~conc|Type*Treatment, data = CO2, group = Plant, type = "o",
pch = symbols, col = colors,lty = linetype,
main = "Carbon Dioxide Uptake\n in Grass Plants",
key = key.spectes
)
显示上面代码的设置,自己运行
show.settings()
mysettings <- trellis.par.get()
mysettings$superpose.symbol
图片放置 重点: newPage = FALSE
###########################################
#图片放置 *Important = newPage
###########################################
library(lattice)
graph1 <- histogram(~height|voice.part,data = singer,main = "Heights of Choral Singers by Voice Part")
graph2 <- densityplot(~height,data = singer, group= voice.part,
plot.points = FALSE,auto.key = list(columns = 4))
# plot(graph1,split = c(1,1,1,2))
# plot(graph2,split = c(1,2,1,2),newpage = FALSE)
#position = c(xmin, ymin, xmax, ymax)
#x轴和y轴的维度范围都是从0到1,原点(0, 0)在图形左下角。
plot(graph1,position = c(0,0.3,1,1))
plot(graph2,position = c(0,0,1,0.3), newpage = FALSE)
position函数是图片在整个显示中的比例position = c(x1,x2,y1,y2)
#smoothScatter()函数可利用核密度估计生成用颜色密度来表示点分布的散点图
## A largish data set
n <- 10000
x1 <- matrix(rnorm(n), ncol = 2)
x2 <- matrix(rnorm(n, mean = 3, sd = 1.5), ncol = 2)
x <- rbind(x1, x2)
smoothScatter(x)
本文详细解析了R语言中的lattice函数包用于绘制图形的方法,包括以因子为条件的图形创建,如何处理连续型变量作为条件,以及如何添加平滑拟合曲线和水平均值线。通过实例代码和解释,帮助读者理解lattice包的使用技巧。
714

被折叠的 条评论
为什么被折叠?



