格点dotplot条件填充颜色

use*_*503 4 plot r 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))
Run Code Online (Sandbox Code Playgroud)

这创建了一个情节:

Dotplot没有突出显示.

题:

我想实现以下情节:

具有针对性突出显示的Dotplot.

我怎样才能重构代码来实现这个目标?

Hen*_*rik 6

你可以试试这个:

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获得第二种颜色(红色)的情况.

在此输入图像描述

?dotplotgroup论点:
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.