r - 将颜色分配给从 DF 中选择相应颜色的标志
问题描述
我想在我的情节中C
为特定的标志 ( ) 分配特定的颜色 ( ) 。POP
我试过
PC <- data.frame(POP = c("A", "C", "C", "B"),
X = c(0.1,0.2,0.3,0.4),
Y = c(0.1,0.2,0.3,0.4))
COLS <- data.frame(C = c("red", "grey", "brown"),
P = c("A", "B","C"))
plot(PC$X, PC$Y, col = COLS$C[which(COLS$P == PC$POP)])
但得到以下错误
Warning messages:
1: In `==.default`(COLS$P, PC$POP): longer object length is not a multiple of shorter object length
2: In is.na(e1) | is.na(e2): longer object length is not a multiple of shorter object length
结果图中的颜色不正确。
解决方案
代替col = COLS$C[match(PC$POP, COLS$P)]
。看?match
它的用途。
您不能安全地应用于"=="
两个不同长度的向量。逻辑与添加两个不同长度的向量相同。适用回收规则。此规则在某些应用程序中很有用,但您有责任让它做正确的事情/产生所需的结果。
此外,您需要调用stringsAsFactors = FALSE
以data.frame()
避免将颜色变量(字符串)编码为因子(整数)。
推荐阅读
- firebase - 在firebase控制台中按日期过滤firestore查询
- plot - 朱莉娅:当我有一个情节时,如何找到最佳拟合曲线/方程?
- javascript - 允许我为 cypress 测试创建随机电子邮件的功能
- tcp - Hazelcast tcp-ip config REST API 未启用。未知协议:I^@^@
- python - 如何使用 OpenPyXL 遍历 Excel 表中的所有行?
- r - R 中的 tolower(txt) 非字符参数错误(用于文本挖掘)
- node.js - 尽管安装并需要,但找不到模块“firebase-tools”的声明文件
- reactjs - MakeStyles (Material UI) 将样式应用到子元素
- ios - 按自定义时间间隔对对象数组进行分组
- flutter - 登录后立即执行 SetState