Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot of a matrix of type character

Tags:

r

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?

I am looking fro something like this:

enter image description here

like image 608
Gago-Silva Avatar asked Dec 30 '11 17:12

Gago-Silva


People also ask

What is a matrix plot?

A matrix plot is an array of scatterplots. There are two types of matrix plots: Matrix of plots and Each Y versus each X. Matrix of plots. This type of matrix plot accepts up to 20 variables and creates a plot for every possible combination.

What is Matplot in R?

R Language Base Plotting Matplot matplot is useful for quickly plotting multiple sets of observations from the same object, particularly from a matrix, on the same graph. Here is an example of a matrix containing four sets of random draws, each with a different mean.

How do you represent a matrix in R?

To create a matrix in R you need to use the function called matrix(). The arguments to this matrix() are the set of elements in the vector. You have to pass how many numbers of rows and how many numbers of columns you want to have in your matrix. Note: By default, matrices are in column-wise order.


1 Answers

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:

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

like image 113
IRTFM Avatar answered Sep 18 '22 11:09

IRTFM