I am looking for a way to plot a matrix of type character:
我正在寻找一种绘制字符类型矩阵的方法:
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"
with a defined set of colours
使用一组定义的颜色
A="Yellow"
B="Blue"
C="Green"
Should I pass from matrix to ascii and use image() from sp package?
我应该从矩阵传递到ascii并使用sp包中的image()吗?
I am looking fro something like this:
我正在寻找这样的东西:
1 个解决方案
#1
10
It rather depends on what you meant by "plot a matrix":
它取决于你对“绘制矩阵”的意思:
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)
This plots solid diamonds of the specified color at the matrix locations determined by the row and columns of matrix m. Another way with image
:
这绘制了由矩阵m的行和列确定的矩阵位置处的指定颜色的实心菱形。图像的另一种方式:
m2[] <- match(m, c("A","B","C"))
mode(m2) <- "numeric"
m2
image(1:nrow(m2), 1:ncol(m2), m2, col=c("yellow", "blue","green"))
#1
10
It rather depends on what you meant by "plot a matrix":
它取决于你对“绘制矩阵”的意思:
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)
This plots solid diamonds of the specified color at the matrix locations determined by the row and columns of matrix m. Another way with image
:
这绘制了由矩阵m的行和列确定的矩阵位置处的指定颜色的实心菱形。图像的另一种方式:
m2[] <- match(m, c("A","B","C"))
mode(m2) <- "numeric"
m2
image(1:nrow(m2), 1:ncol(m2), m2, col=c("yellow", "blue","green"))