注:R语言的再复习之路
格式: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 ~ A 或A ~ x |
箱线图 | bwplot() |
x ~ A 或A ~ x |
点图 | dotplot() |
~ x | A |
柱状图 | histogram() |
~ x |
核密度图 | densityplot() |
~ x | A * B |
平行坐标曲线图 | parallelplot() |
dataframe |
散点图 | xyplot() |
y ~ x | A |
散点图矩阵 | splom() |
dataframe |
线框图 | stripplot() |
A ~ x 或x ~ 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 | 横轴范围、纵轴范围 |
当调节变量为连续性变量时,需要将其转换为因子
cut()
函数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)
## 例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)
## 例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)
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)'
)
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)