格子点图条件填充颜色 [英] Lattice dotplot conditional fill color
问题描述
我有一个数据框,我想用点阵的面板点图(而不是ggplot2)来可视化.它包含一个变量,应有条件地使用该变量通过不同的颜色填充来突出显示数据.
I have a data frame, which I want to visualize with lattice's panel dot plot (not ggplot2). It contains a variable which should be used conditionally to highlight data by different color fill.
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))
这将创建一个图:
我想实现以下情节:
如何重构代码以实现这一目标?
How can I refactor the code to achieve this?
推荐答案
您可以尝试以下方法:
dotplot(car ~ mpg | as.factor(cyl), data=df, layout=c(3,1),
pch = 19, groups = carb < 2, col = c("blue", "red"))
groups
自变量carb < 2
产生逻辑向量.按字母顺序FALSE
在TRUE
之前.因此,carb < 2
为FALSE的情况为第一色(蓝色),而carb< c为0. 2获得第二种颜色,红色.
The groups
argument carb < 2
results in a logical vector. Alphabetically FALSE
comes before TRUE
. Thus, cases where carb < 2
is FALSE get the first colour (blue), and cases where carb < 2 get the second colour, red.
从?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.
From ?dotplot
about group
argument:
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.
这篇关于格子点图条件填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!