R Confusion Matrix – Confusion Matrix Plot in R

confusion matrixr

I have a computed confusion matrix from google earth engine that I would like to use in r to derive a plot. how do I manually enter the confusion matrix and plot in R?. This is the confusion matrix:

1:[141,17,3,6,0,0,39,2]
2:[9,287,0,6,17,0,51,0]
3:[31,1,8,14,9,0,29,1]
4:[2,2,0,179,24,0,23,30]
5:[1,10,0,17,78,0,69,1]
6:[0,0,0,15,3,0,3,16]
7:[22,41,3,12,45,0,380,0]
8:[12,0,3,60,1,0,4,204]

Best Answer

Using the image function, you can:

# import your data values
z = c(141,17,3,6,0,0,39,2,
      9,287,0,6,17,0,51,0,
      31,1,8,14,9,0,29,1,
      2,2,0,179,24,0,23,30,
      1,10,0,17,78,0,69,1,
      0,0,0,15,3,0,3,16,
      22,41,3,12,45,0,380,0,
      12,0,3,60,1,0,4,204)

# create a matrix
z = matrix(z, ncol = 8)

# set rows and columns names
colnames(z) = c("1","2","3","4","5","6","7","8")
rownames(z) = c("1","2","3","4","5","6","7","8")

##To get the correct image plot rotation
##We need to flip the plot
image(z[,ncol(z):1], axes = FALSE)

##Add in the y-axis labels. Similar idea for x-axis.
axis(2, at = seq(0, 1, length=length(colnames(z))), labels = colnames(z))

And here is your output output