我可以根据逻辑条件将颜色分配给 MASS::parcoord() 吗?

Can I assign colors to MASS::parcoord() based on a logical condition?

下面是生成平行坐标图的代码:

require(MASS)
shoes <- data.frame(shoes)
parcoord(shoes)

shoes 数据集用于显示配对 t 检验的功效,这只是背景信息。鞋子有两列,A 和 B,代表两种鞋底材料的磨损。分析正确,材料之间存在巨大差异。

显示配对数据的一个好方法是使用平行坐标图,但如您所见,如果没有一些颜色,它几乎什么都没有。我想添加两种颜色,比如 A > B 时为红色,A < B 时为绿色。两种情况都会发生:

> shoes$A > shoes$B
 [1] FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE

我的问题是 parcoord() 在进行观察时会循环显示颜色,因此我不确定如何根据逻辑测试指定颜色。我试过了

parcoord(shoes, col = ifelse(shoes$A > shoes$B, "red", "green"))

以及

中的各种数字游戏(除了加 26 之外还有很多)
my_colors <- colors()[as.numeric(shoes$A > shoes$B) + 26]
parcoord(shoes, col = my_colors)

但似乎没有任何效果。我要么得到一系列颜色,要么得到一种颜色,要么得到除了顶部和底部条目之外的所有一种颜色。我希望 FALSE 生成一种颜色,TRUE 生成另一种颜色。

我不确定我是否明白这一点,但您的条件 A > B 仅适用于 shoes 的最大值和最小值。

shoes <- within(shoes, criterium <- ifelse(A > B, "bigger", "smaller"))

       A    B criterium
1  13.2 14.0   smaller
2   8.2  8.8   smaller
3  10.9 11.2   smaller
4  14.3 14.2    bigger
5  10.7 11.8   smaller
6   6.6  6.4    bigger
7   9.5  9.8   smaller
8  10.8 11.3   smaller
9   8.8  9.3   smaller
10 13.3 13.6   smaller

minmax <- c(min(min(shoes$A), min(shoes$B)), max(max(shoes$A), max(shoes$B)))

> minmax
[1]  6.4 14.3

因此您的平行坐标图将仅显示 "red" 中的顶部和底部条目。换句话说:你的解决方案是正确的。