Image Functions in R

I am trying to do something a little complicated for a beginner in programming. I have a 16x16 matrix and I want to build the values ​​as a heat map using image() in R. How can I build a β€œ0” (zeros) in blue when the sum (row index + column index) is <= 15? Is it possible?

matrix example:

 x <- c(3045, 893, 692, 830, 617, 155, 246, 657, 105, 60, 18, 7, 7, 4, 2, 11234, 2985, 2242, 2471, 1575, 366, 503, 1283, 170, 79, 32, 6, 4, 1, 3, 19475, 4756, 3233, 3251, 1810, 409, 575, 1210, 139, 41, 11, 4, 2, 0, 0, 20830, 4739, 2990, 2531, 1346, 298, 325, 612, 60, 17, 1, 0, 1, 0, 0, 15304, 3196, 1885, 1440, 610, 117, 115, 185, 14, 2, 0, 0, 0, 0, 0, 8026, 1535, 806, 539, 223, 33, 37, 39, 0, 0, 0, 0, 0, 0, 0, 3300, 562, 286, 141, 45, 14, 5, 12, 0, 0, 0, 0, 0, 0, 0, 1067, 160, 65, 40, 14, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 277, 47, 6, 2, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 72, 6, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) xmat <- matrix(x, ncol = 12) xmat <- cbind(xmat, rep(0,16), rep(0,16), rep(0,16), rep(0,16)) xmat <- rbind(xmat, rep(0,16)) dimnames(xmat) = list(0:15, 0:15) xmat 

Thanks! Vitor

+4
source share
1 answer

Plan events matching your criteria as blue.

 xmat.new <- xmat xmat.new[!((row(xmat) + col(xmat) <= 15) & xmat==0)] <- NA image(xmat.new,col="blue") 

Plan cases that do not meet the criteria, as usual. Pay attention to add=TRUE

 xmat.new <- xmat xmat.new[((row(xmat) + col(xmat) <= 15) & xmat==0)] <- NA image(xmat.new,add=TRUE) 

Result:

enter image description here

Edited to include @Marek's suggestion for easier instructions.

+5
source

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


All Articles