我正在寻找一种绘制字符类型矩阵的方法:
m=matrix(data=c("A","A","B","B","B","C","C","B"),nrow=4,ncol=2)
> m
[,1] [,2]
[1,] "A" "B"
[2,] "A" "C"
[3,] "B" "C"
[4,] "B" "B"
带有一组定义的颜色
A="Yellow"
B="Blue"
C="Green"
我应该从矩阵传递到ascii并使用图像()来自 sp package 吗?
我正在寻找这样的事情:
答案 0 :(得分:10)
这取决于“绘制矩阵”的含义:
m2 <- m
m2[] <- c("yellow", "blue","green")[match(m, c("A","B","C"))]
m2
#------------
[,1] [,2]
[1,] "yellow" "blue"
[2,] "yellow" "green"
[3,] "blue" "green"
[4,] "blue" "blue"
#------------
plot(row(m2), col(m2), col=m2, pch=18, cex=4)
这绘制了由矩阵m的行和列确定的矩阵位置处的指定颜色的实心菱形。 image
的另一种方式:
m2[] <- match(m, c("A","B","C"))
mode(m2) <- "numeric"
m2
image(1:nrow(m2), 1:ncol(m2), m2, col=c("yellow", "blue","green"))