R图:使用数据帧中的RGB值
我在使用数据框中的RGB颜色时遇到问题R图:使用数据帧中的RGB值,r,dataframe,rgb,R,Dataframe,Rgb,我在使用数据框中的RGB颜色时遇到问题 color.table V1 V2 V3 1 Red 255,0,0 2 Orange Red 255,69,0 3 LimeGreen 50,205,50 4 Green 0,128,0 5 DarkGreen 0,100,0 6 GreenYellow 194,225,5 7 Yellow 255,255,0 8 Aqua
color.table
V1 V2 V3
1 Red 255,0,0
2 Orange Red 255,69,0
3 LimeGreen 50,205,50
4 Green 0,128,0
5 DarkGreen 0,100,0
6 GreenYellow 194,225,5
7 Yellow 255,255,0
8 Aquamarine 102,205,170
9 PaleTurquoise 138,145,208
10 IndianRed 205,92,92
11 DarkSalmon 233,150,122
12 DarkKhaki 189,183,107
13 Silver 128,128,128
14 Gainsboro 192,192,192
15 White 255,255,255
barplot(seq(1:15) , col=color.table$V3 )
Error in rect(y1, x1, y2, x2, ...) :
invalid color specification "255,0,0"
似乎我无法使用此数据帧中的RGB
color.table
V1 V2 V3
1 Red 255,0,0
2 Orange Red 255,69,0
3 LimeGreen 50,205,50
4 Green 0,128,0
5 DarkGreen 0,100,0
6 GreenYellow 194,225,5
7 Yellow 255,255,0
8 Aquamarine 102,205,170
9 PaleTurquoise 138,145,208
10 IndianRed 205,92,92
11 DarkSalmon 233,150,122
12 DarkKhaki 189,183,107
13 Silver 128,128,128
14 Gainsboro 192,192,192
15 White 255,255,255
barplot(seq(1:15) , col=color.table$V3 )
Error in rect(y1, x1, y2, x2, ...) :
invalid color specification "255,0,0"
即使我尝试将RGB转换为十六进制值,也有一些奇怪的地方
rgb(color.table$V3[1], 50, m=255)
Error in rgb(color.table$V3[1], 50, m = 255) :
argument "blue" is missing, with no default
如果这样做有效:
> rgb(255,0,0, 50, m=255)
[1] "#FF000032"
这里有些可疑的东西,但我摸不着。非常感谢您的建议 您需要使用
rgb
函数从V3
变量中提取颜色:
cols <- sapply(strsplit(as.character(color.table$V3), ","), function(x) {
rgb(x[1], x[2], x[3], m=255)
})
cols
# [1] "#FF0000" "#FF4500" "#32CD32" "#008000" "#006400" "#C2E105" "#FFFF00" "#66CDAA" "#8A91D0"
# [10] "#CD5C5C" "#E9967A" "#BDB76B" "#808080" "#C0C0C0" "#FFFFFF"
cols当您在上查找文档时,rgb
,您能解释一下运行rgb(color.table$V5[1,50,m=255)
时您认为您正在向哪些参数传递值吗?rgb(255,0,0,50,m=255)
嗯,color.table$V5[1]
只是一个值,而不是三个值。因此,由于位置匹配和局部参数匹配,您基本上已经完成了rgb(红色=颜色。表$V5[1],绿色=50,maxColorValues=1)
,我实际上并没有任何意思。我想亲自帮你去,但没用。完成了list=unlist(strsplit(全部$V5[1],“,”))
thenrgb(list[1],list[2],list[3],50,m=255)[1]“#FF000032”
可能不是很优雅,但我做到了。谢谢joran带我去那里。。。