m [,1]...">

Type matrix type graph

I am looking for a way to build a matrix of type:

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 specific set of colors

 A="Yellow" B="Blue" C="Green" 

Should I go from matrix to ascii and use image () from sp package ?

I look something like this:

enter image description here

+6
source share
1 answer

It rather depends on what you mean by "matrix graphics":

  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 displays solid diamonds of a given color at the locations of the matrices defined by the row and columns of the matrix m. Another way: 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")) 

enter image description here

+10
source

Source: https://habr.com/ru/post/904768/


All Articles