Problem:
我有一个数据框,我想用lattice的面板点图(不是ggplot2)对其进行可视化。它包含一个变量,应有条件地使用该变量通过不同的颜色填充突出显示数据。
可重现的例子:
require(lattice)
# Make reproducable data frame
df= mtcars
df= cbind(car = rownames(df), df)
rownames(df)= NULL
df=df[1:5, c("car", "mpg", "cyl", "carb")]
df
# output:
# car mpg cyl carb
# Mazda RX4 21.0 6 4
# Mazda RX4 Wag 21.0 6 4
# Datsun 710 22.8 4 1
# Hornet 4 Drive 21.4 6 1
# Hornet Sportabout 18.7 8 2
# I am interested to highlight those data which have carb=1
df[df$carb==1,]
# car mpg cyl carb
# Datsun 710 22.8 4 1
# Hornet 4 Drive 21.4 6 1
dotplot(car ~ mpg | as.factor(cyl), data=df, layout=c(3,1))
这创建了一个情节:
问题:
我想实现以下情节:
我如何重构代码来实现这一目标?
你可以试试这个:
dotplot(car ~ mpg | as.factor(cyl), data=df, layout=c(3,1),
pch = 19, groups = carb < 2, col = c("blue", "red"))
The groups
争论carb < 2
结果是一个逻辑向量。按字母顺序FALSE
出现在之前TRUE
。因此,情况carb < 2
为 FALSE 时得到第一种颜色(蓝色),当 carb
From ?dotplot
about group
争论:
A variable or expression to be evaluated in data, expected to act as a grouping variable within each panel, typically used to distinguish different groups by varying graphical parameters like color and line type.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)