joel38237
joel38237

Reputation: 169

How do I change the colours on a figure by the image function in R?

I have the following toy matrix, and I can create a visualisation, and change the colours of the tiles,

library(arules)    
m1 <- matrix(c(0,0,1,1,2,2), byrow=TRUE, nrow=3)
image(m1, col=heat.colors(3))

My question regards a sparse matrix,

library(Matrix)    
m2 <- Matrix(c(0,0,1,1,2,2), byrow=TRUE, sparse=TRUE, nrow=3)

If I attempt to create the corresponding visualisation:

image(m2, col=heat.colors(3))

I receive an error: "Error in .local(x, ...) : argument 2 matches multiple formal arguments". I believe this is because the argument 'col' is ambiguous. So, I tried to work out which other arguments that are similar to 'col' using,

args(image)

However, this provided the following output which did not include 'col' or similar agruments,

function (x, ...) 
NULL

My question is the following: How do you create an image from a sparse matrix using image() and then alter the colours of the tiles?

Upvotes: 0

Views: 652

Answers (1)

user1981275
user1981275

Reputation: 13382

One way would be to convert your sparse Matrix into a matrix:

image(as.matrix(m2), col=heat.colors(3))

Upvotes: 1

Related Questions