这个问题与我在这里问的问题有关。我正在尝试使用数据框列中指定的颜色为散点图中的各个点着色。数据来自制表符分隔的文件,如下所示:
> dput(ztestHSV)
structure(list(y = c(0, -1, -1, -2), x = c(0, 2, 0, -2), group = c("m",
"m", "m", "s"), colorHSV = c("0.02,0.83,0.89", "0.59,0.59,0.85",
"0.25,0.45,0.8", "0.55,0.41,0.8"), colorText = c("red", "blue",
"green", "turquoise")), .Names = c("y", "x", "group", "colorHSV",
"colorText"), class = "data.frame", row.names = c(NA, -4L))
IE
y x group colorHSV colorText
1 0 0 m 0.02,0.83,0.89 red
2 -1 2 m 0.59,0.59,0.85 blue
3 -1 0 m 0.25,0.45,0.8 green
4 -2 -2 s 0.55,0.41,0.8 turquoise
以下代码适用于 R 中可用的预定义颜色集:
ztestHSV=read.table(file="testHSV.txt",sep="\t",header=TRUE,
colClasses=c("numeric","numeric","character","character","character"))
plot(ztestHSV[ztestHSV$group=='m',]$x,ztestHSV[ztestHSV$group=='m',]$y,type='n')
points(ztestHSV[ztestHSV$group=='m',]$x,ztestHSV[ztestHSV$group=='m',]$y,
pch=23,cex=1.5,
bg=ztestHSV[ztestHSV$group=='m',]$colorText
)
我真正想做的是hsv()
用来指定颜色。但我无法弄清楚正确的bg
命令来使它工作。我已经尝试了几件事,并在下面列出了它们以及相关的结果。
1
bg=hsv(as.numeric(ztestHSV$colorHSV))
Error in hsv(as.numeric(ztestHSV$colorHSV)) :
bad hsv to rgb color conversion
In addition: Warning message:
In hsv(as.numeric(ztestHSV$colorHSV)) : NAs introduced by coercion
2
bg=hsv(as.numeric(strsplit(ztestHSV$colorHSV,",")))
Error in hsv(as.numeric(strsplit(ztestHSV$colorHSV, ","))) :
(list) object cannot be coerced to type 'double'
3
bg=hsv(unlist(strsplit(ztestHSV$colorHSV,",")))
以上运行但使用了错误的背景颜色
4
bg=hsv(as.numeric(unlist(strsplit(ztestHSV$colorHSV,","))))
以上不会产生错误但不会填充背景颜色
似乎这是将colorHSV的字符串转换为hsv()
可以使用的东西的问题,但我不知道该怎么做。任何帮助都会很棒。