I'm in the process of converting number matrices to color matrices; however, the colors aren't displaying correctly, e.g., the color matrix displays the incorrect color relative to the assigned number in the number matrix.
I first assigned colors to a set of numerical values.
cols <- c(
'0' = "#FFFFFF",
'1' = "#9AFC7F",
'2' = "#77F255",
'3' = "#60DF3D",
'4' = "#49C925",
'5' = "#37B215",
'6' = "#219900"
)
Then generated a couple matrices.
Map1 <- read.table(text="
1 1 1 0 0 1 1 0 2 1
1 1 1 1 0 1 1 1 2 1
0 1 1 0 2 2 1 1 1 1
0 1 1 1 1 1 2 1 2 2
1 1 2 1 1 1 1 1 2 2
1 1 1 1 1 2 2 1 2 2
1 2 0 1 1 2 1 2 1 2
0 2 2 0 1 1 1 1 1 1
0 1 1 0 0 1 2 2 1 0
0 1 1 1 1 1 1 1 1 0
")
Map2 <- read.table(text="
1 2 1 1 1 1 1 0 2 1
1 1 1 1 1 1 1 1 1 1
0 2 1 1 1 2 1 1 1 1
0 0 1 2 2 1 2 2 3 2
0 1 2 2 1 1 2 2 2 2
1 1 0 2 1 1 3 2 3 3
1 1 2 2 1 2 2 2 1 2
0 1 2 2 1 2 2 2 1 2
1 1 2 1 1 1 1 2 1 1
1 1 1 1 1 1 1 1 1 1
")
After plotting the matrices, I find the colors don't match up with the value they should have been assigned.
image(1:nrow(Map1), 1:ncol(Map1), t(apply(Map1, 2, rev)), col=cols, xaxt='n', yaxt='n', ann=FALSE, bty='n', asp = 1)
image(1:nrow(Map2), 1:ncol(Map2), t(apply(Map2, 2, rev)), col=cols, xaxt='n', yaxt='n', ann=FALSE, bty='n', asp = 1)
I've been stumped trying to figure out why the numbers and colors aren't aligning. Plotting the numbers assigned in to cols
generates the correct sequence.
d <- read.table(text="0 1 2 3 4 5 6")
image(1:nrow(d), 1:ncol(d), t(apply(d, 2, rev)), col=cols, xaxt='n', yaxt='n', ann=FALSE, bty='n', asp = 1)
There aren't any 6
values in the first number matrix, so there shouldn't be any dark squares in the first color matrix. It appears as though values of 2
are given the color assigned to 6
in the first matrix, and 3
to 6
in the second.
Adding a 3
into the first number matrix seems to shift the other values down the color scale, i.e., the largest number in the matrix is assigned the darkest color, rather than the color it was assigned in cols
.
Map1.2 <- read.table(text="
1 1 1 0 0 1 1 0 2 1
1 1 1 1 0 1 1 1 2 1
3 1 1 0 2 2 1 1 1 1
0 1 1 1 1 1 2 1 2 2
1 1 2 1 1 1 1 1 2 2
1 1 1 1 1 2 2 1 2 2
1 2 0 1 1 2 1 2 1 2
0 2 2 0 1 1 1 1 1 1
0 1 1 0 0 1 2 2 1 0
0 1 1 1 1 1 1 1 1 0
")
image(1:nrow(Map1), 1:ncol(Map1), t(apply(Map1, 2, rev)), col=cols, xaxt='n', yaxt='n', ann=FALSE, bty='n', asp = 1)
image(1:nrow(Map1.2), 1:ncol(Map1.2), t(apply(Map1.2, 2, rev)), col=cols, xaxt='n', yaxt='n', ann=FALSE, bty='n', asp = 1)
The only difference between the number matrices which generated these two color matrices is the 0
in the first column, third row was changed to a 3
.