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)])