格子点图条件填充颜色 [英] Lattice dotplot conditional fill color

查看:72
本文介绍了格子点图条件填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,我想用点阵的面板点图(而不是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产生逻辑向量.按字母顺序FALSETRUE之前.因此,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆