我有一个数据框,我想用格子的面板点图(而不是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))
Run Code Online (Sandbox Code Playgroud)
这创建了一个情节:

我想实现以下情节:

我怎样才能重构代码来实现这个目标?
你可以试试这个:
dotplot(car ~ mpg | as.factor(cyl), data=df, layout=c(3,1),
pch = 19, groups = carb < 2, col = c("blue", "red"))
Run Code Online (Sandbox Code Playgroud)
该groups参数carb < 2产生逻辑向量.按字母顺序FALSE排在前面TRUE.因此,carb < 2FALSE获得第一种颜色(蓝色)的情况,以及carb <2获得第二种颜色(红色)的情况.

从?dotplot约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.
| 归档时间: |
|
| 查看次数: |
1494 次 |
| 最近记录: |