Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何确定矩阵中数字的颜色_R_Colormap - Fatal编程技术网

R 如何确定矩阵中数字的颜色

R 如何确定矩阵中数字的颜色,r,colormap,R,Colormap,目前,我尝试进行多种可视化,其中矩阵中的数字必须在图像中获得某种(固定)颜色 由于我找不到一种方法将颜色分配给一个固定的数字,这给我带来了比我想象的更多的麻烦 以下示例显示了该问题: 假设我们定义以下颜色与以下数字关联 cols <- c( '0' = "#FFFFFF", '1' = "#99FF66", '2' = "#66FF33", '3' = "#33CC00", '4' = "#009900" ) image(as.matrix(d), col=cols) col

目前,我尝试进行多种可视化,其中矩阵中的数字必须在图像中获得某种(固定)颜色

由于我找不到一种方法将颜色分配给一个固定的数字,这给我带来了比我想象的更多的麻烦

以下示例显示了该问题:

假设我们定义以下颜色与以下数字关联

cols <- c(
 '0' = "#FFFFFF",
 '1' = "#99FF66",
 '2' = "#66FF33",
 '3' = "#33CC00",
 '4' = "#009900" 
)
image(as.matrix(d), col=cols)

cols多亏了安德烈的建议,我可以很巧妙地解决这个问题

d<-as.matrix(read.table(text="
1  1  1  3
3  2  1  4
4  1  1  2
3  3  1  1"))

cols <- c(
'0' = "#FFFFFF",
'1' = "#99FF66",
'2' = "#66FF33",
'3' = "#33CC00",
'4' = "#009900"
)

image(as.matrix(d), col= cols[ names(cols) %in% d ])

d删除矩阵中不突出的颜色值:

image(as.matrix(d), col=cols[names(cols)%in%unlist(d)])
unlist
仅适用于名称所示的列表。
如果
d
已经是一个矩阵,只需使用
c(d)

您是否尝试过
plotrix
软件包中的
color2D.matplot
功能?这是否是秒矩阵的一种可能解决方法<代码>图像(如.matrix(d),col=cols[名称(cols)%in%unlist(d)])
d<-as.matrix(read.table(text="
1  1  1  3
3  2  1  4
4  1  1  2
3  3  1  1"))

cols <- c(
'0' = "#FFFFFF",
'1' = "#99FF66",
'2' = "#66FF33",
'3' = "#33CC00",
'4' = "#009900"
)

image(as.matrix(d), col= cols[ names(cols) %in% d ])
image(as.matrix(d), col=cols[names(cols)%in%unlist(d)])