Converting a Number Matrix to a Color Matrix in R -
i have following 4x4 number matrix containing numbers 0-4:
0 1 0 3 3 2 1 4 4 1 0 2 3 3 0 1
i understand how convert number matrices color matrices using chosen colors , specific square dimensions (length x width) using r. clear, i'm defining color matrix figure using colored squares represent specific values in matrix orientation. example 4x4 program follows:
i have assign color codes numbers, example:
0 = ffffff 1 = 99ff66 2 = 66ff33 3 = 33cc00 4 = 009900
but don't know begin putting together. imagine have specify dimensions color squares well.
my goal able import data frame r 10 numerical values , create these color charts matrices large 20x20.
here's do:
d<-read.table(text=" 0 1 0 3 3 2 1 4 4 1 0 2 3 3 0 1") cols <- c( '0' = "#ffffff", '1' = "#99ff66", '2' = "#66ff33", '3' = "#33cc00", '4' = "#009900" ) # names aren't necessary here. image(1:nrow(d), 1:ncol(d), as.matrix(d), col=cols)
if you'd prefer orientation different, can rotate matrix:
image(1:nrow(d), 1:ncol(d), t(apply(d, 2, rev)), col=cols)
to rid of text , borders, might try:
image(1:nrow(d), 1:ncol(d), as.matrix(d), col=cols, xaxt="n", yaxt="n", bty="n", xlab="", ylab="")
Comments
Post a Comment