【发布时间】:2016-08-02 20:56:33
【问题描述】:
library(lattice)
# load reproducible data set
attach(mtcars)
# change the rownames into a column value and remove the rownames
mtcars <- as.data.frame(cbind(car.name=rownames(mtcars), mtcars))
rownames(mtcars) <- NULL
# panel function parameters so that I label the points that are 100 units
# different between the hp and disp values and label the points with the car name
panel = function (x, y, ...) {
panel.xyplot(x, y, ...)
x1 <- x[x-y > 100 | y-x > 100]
y1 <- y[x-y > 100 | y-x > 100]
panel.text(x1, y1, labels=mtcars$car.name, pos=3)
}
#calling the xyplot
xyplot(hp~disp, data=mtcars, main=NULL, ylab="hp", xlab="disp",
jitter=TRUE, pch=1, as.table=TRUE, panel=panel)
我可以看到在hp 和disp 之间显示100 个单位差异的所有点都有一个标签。
但是,标签与表中的实际数据不匹配。
我可以通过首先按disp 列的降序对mtcars 进行排序来判断这一点:
mtcars <- mtcars[order(-mtcars$disp),]
head(mtcars)
car.name mpg cyl disp hp drat wt qsec vs am gear carb
15 Cadillac Fleetwood 10.4 8 472 205 2.93 5.250 17.98 0 0 3 4
16 Lincoln Continental 10.4 8 460 215 3.00 5.424 17.82 0 0 3 4
17 Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
25 Pontiac Firebird 19.2 8 400 175 3.08 3.845 17.05 0 0 3 2
5 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
7 Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4
如您所见,car.name 最高的disp 是凯迪拉克弗利特伍德,图中没有标注,实际上出现了另一个车名。
如何根据我的情况显示正确的汽车名称,即只有在 disp 和 hp 之间有 >100 单位差异的汽车被标记?我的面板功能有问题。
【问题讨论】: