1引言
lattice 是 R 里面一个强大的绘图系统,仅次于 ggplot2,其使用语法也不同与 ggplot2, 但是感觉使用的人好像没有 ggplot2 多,后续也会陆续介绍其绘制的一些图形。
参考链接:
2示例
下载测试数据:
## data import from URL
gdURL <- "http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt"
gDat <- read.delim(file = gdURL)
## drop Oceania
jDat <- droplevels(subset(gDat, continent != "Oceania"))
str(jDat)
# 'data.frame': 1680 obs. of 6 variables:
# $ country : chr "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
# $ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
# $ pop : num 8425333 9240934 10267083 11537966 13079460 ...
# $ continent: chr "Asia" "Asia" "Asia" "Asia" ...
# $ lifeExp : num 28.8 30.3 32 34 36.1 ...
# $ gdpPercap: num 779 821 853 836 740 ...
简单绘制:
library(lattice)
xyplot(lifeExp ~ gdpPercap, jDat)
xyplot(lifeExp ~ gdpPercap, jDat,
grid = TRUE)
对 x 数值变换:
## log, the sub-optimal way
xyplot(lifeExp ~ log10(gdpPercap), jDat,
grid = TRUE)
## logging, better way ... step 1
xyplot(lifeExp ~ gdpPercap, jDat,
grid = TRUE,
scales = list(x = list(log = 10)))
坐标轴显示:
xyplot(lifeExp ~ gdpPercap, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)))
添加回归线:
xyplot(lifeExp ~ gdpPercap, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
type = "p")
xyplot(lifeExp ~ gdpPercap, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
type = c("p", "r"))
修改方法:
xyplot(lifeExp ~ gdpPercap, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
type = c("p", "r"), col.line = "darkorange", lwd = 3)
xyplot(lifeExp ~ gdpPercap, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
type = c("p", "smooth"), col.line = "darkorange", lwd = 3)
添加分组:
xyplot(lifeExp ~ gdpPercap, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
group = continent)
## auto.key
xyplot(lifeExp ~ gdpPercap, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
group = continent, auto.key = TRUE)
同时添加拟合曲线:
## groups + type "smooth"
xyplot(lifeExp ~ gdpPercap, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
group = continent, auto.key = TRUE,
type = c("p", "smooth"), lwd = 4)
## making key more compact
xyplot(lifeExp ~ gdpPercap, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
group = continent, auto.key = list(columns = nlevels(jDat$continent)),
type = c("p", "smooth"), lwd = 4)
分面:
xyplot(lifeExp ~ gdpPercap | continent, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)))
xyplot(lifeExp ~ gdpPercap | continent, jDat,
group = continent,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)))
添加曲线:
## conditioning + type "r" or "smooth"
xyplot(lifeExp ~ gdpPercap | continent, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
type = c("p", "smooth"), col.line = "darkorange", lwd = 4)
xyplot(lifeExp ~ gdpPercap | continent, jDat,
grid = TRUE, group = continent,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
type = c("p", "smooth"), lwd = 4)
去除上边和右边刻度:
xyplot(lifeExp ~ gdpPercap | continent, jDat,
grid = TRUE, group = continent,
scales = list(x = list(log = 10, equispaced.log = FALSE),
alternating = 1,
tck = c(1, 0)),
type = c("p", "smooth"), lwd = 4)
密度展示:
xyplot(lifeExp ~ gdpPercap | continent, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
type = c("p", "smooth"), lwd = 4, alpha = 1/2)
xyplot(lifeExp ~ gdpPercap | continent, jDat,
grid = TRUE,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
panel = panel.smoothScatter)
蜂形图:
#install.packages("hexbin", dependencies = TRUE)
library(hexbin)
hexbinplot(lifeExp ~ gdpPercap, jDat,
scales = list(x = list(log = 10, equispaced.log = FALSE)),
aspect = 1, bins=50)
3结尾
路漫漫其修远兮,吾将上下而求索。
欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群 (微信交流群需收取 20 元入群费用,一旦交费,拒不退还!(防止骗子和便于管理)) 。QQ 群可免费加入, 记得进群按格式修改备注哦。
声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/257972.html