第23章 使用lattice进行高级绘图

注:R语言的再复习之路
 
 

1. lattice包

格式:graph_function(formula, data = , options)

  • graph_function是各种绘图函数
  • formula指定要展示变量和任意调节变量
  • data = 指定数据框
  • options是用逗号分隔的参数

在公式中,主要格式为y ~ x | A * B,竖线左侧的变量称为主要变量,右边的变量称为调节变量。对于单变量图,用~ x代替y ~ x;对于3D图,用z ~ x * y代替y ~ x

图类型函数公式例子
3D等高线图contourplot()z ~ x * y
3D水平图levelplot()z ~ x * y
3D散点图cloud()z ~ x * y | A
3D线框图wireframe()z ~ y * x
条形图barchart()x ~ AA ~ x
箱线图bwplot()x ~ AA ~ x
点图dotplot()~ x | A
柱状图histogram()~ x
核密度图densityplot()~ x | A * B
平行坐标曲线图parallelplot()dataframe
散点图xyplot()y ~ x | A
散点图矩阵splom()dataframe
线框图stripplot()A ~ xx ~ A
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'))

densityplot(~ mpg)
densityplot(~ mpg | cyl)
bwplot(cyl ~ mpg | gear)
xyplot(mpg ~ wt | cyl * gear)
cloud(mpg ~ wt * qsec | cyl)
dotplot(cyl ~ mpg | gear)
splom(mtcars[c(1, 3, 4, 5, 6)])
detach(mtcars)
选项描述
aspect指定每个面板图形的纵横比(高度/宽度)的一个数字
col, pch, lty, lwd颜色、符号、线条类型、线条宽度
group分组变量(因子)
index.cond列出展示面板顺序的列表
key支持分组变量中图例的函数
layout指定面板设置(列数、行数)的二元数值向量
main, sub主标题、副标题
panel在每个面板中生成图的函数
scales列出提供坐标轴注释信息的列标配
strip用于自定义面板条带的函数
split, position数值型向量,在一页上绘制多幅图形
type指定一个或多个散点图绘图选项
xlab, ylab横轴标签、纵轴标签
xlim, ylim横轴范围、纵轴范围

 
 

2. 调节变量

当调节变量为连续性变量时,需要将其转换为因子

  • 方法1:使用R自带的cut()函数
  • 方法2:使用lattice中的equal.count()
## 格式
myshingle <- equal.count(x, number = n, overlap = proportion)

## 例子
displacement <- equal.count(mtcars$disp, number = 3, overlap = 0)
xyplot(mpg ~ wt | displacement, data = mtcars, layout = c(3, 1), aspect = 1.5)

 
 

3. 面板函数

## 例1
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)

## 例2
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 = 'darkgreen')
  panel.abline(h = mtcars$mpg %>% mean(), col = 'yellow', lty = 3, lwd = 3)
}

xyplot(mpg ~ disp | transmission, data = mtcars, scales = list(cex = .8, col = 'red'), panel = panel.smoother,
       xlab = 'Displacement', ylab = 'Miles Per Gallon',
       main = 'MPG vs Displacement by Transmission Type',
       sub = 'Dotted lines are Group Means', aspect = 1)

 
 

4. 分组变量

## 例1
colors <- c('red', 'blue')
lines <- c(1, 2)
points <- c(16, 17)
key.trans <- list(title = 'Transmission', 
                  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, group = transmission, pch = points, lty = lines, col = colors, lwd = 2, jitter = .005, key = key.trans)

## 例2
colors <- 'darkgreen'
symbols <- c(1:12)
linetype <- c(1:3)
key.species <- 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\nin Grass Plants',
       ylab = expression(paste('Uptake ', bgroup('(', italic(frac('umol', 'm'^2)), ')'))),
       xlab = expression(paste('Concentration ', bgroup('(', italic(frac(mL, L)), ')'))),
       sub = 'Grass Species: Eachinochloa crus-galli',
       key = key.species)

 
 

5. 自定义图形条带

histogram(~ height | voice.part, data = singer,
          strip = strip.custom(bg = 'lightgrey',
                               par.strip.text = list(col = 'black', cex = .8, font = 3)),
          main = 'Distribution of Heights by Voice Pitch',
          xlab = 'Height (inches)'
          )

 
 

7. 页面布局

graph1 <- histogram(~ height | voice.part, data = singer, main = 'Heights of Choral Singers by Voice Part')
graph2 <- bwplot(height ~ voice.part, data = singer)
plot(graph1, split = c(1, 1, 1, 2))
plot(graph2, split = c(1, 2, 1, 2), newpage = FALSE)
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值