0

I have the following code constructing a confusion matrix image:

truth <- c(0,1,0,0,0,1,0,1,0,1)
pred  <- c(0,0,0,0,0,1,0,1,0,0)
FWConf <- mlearning::confusion(factor(pred,labels=c("Loyal","Churn")), factor(truth,labels=c("Loyal","Churn")),vars=c("a","b"), labels = c("Actual","Predicted"))
opar <- par(mar=c(14, 4, 7, 2))
x <- x.orig <- unclass(FWConf)
x <- log(x + 0.5) * 2.33
x[x < 0] <- NA
x[x > 10] <- 10
diag(x) <- -diag(x)

graphics::image(1:ncol(x), 1:ncol(x), -(x[, nrow(x):1]), xlab='', ylab='',col=colorRampPalette(c("red","yellow","green"))(41), xaxt='n', yaxt='n', zlim=c(-10, 10))

axis(1, at=1:ncol(x), labels=colnames(x), cex.axis=0.8)
axis(2, at=ncol(x):1, labels=colnames(x), las=1, cex.axis=0.8)
title(ylab='Predicted', line=0.5, xlab = 'Actual') 
abline(h = 0:ncol(x) + 0.5, col = 'gray')
abline(v = 0:ncol(x) + 0.5, col = 'gray')
text(1:6, rep(6:1, each=6), 
     labels = sub('^0$', '', round(c(x.orig), 0)))
box(lwd=2)
title(sub = paste("\n\n\n +-------------------------------------------------------+",
                  "\n\t[\tCosto Mancato Allarme:\t", 90      ,"\t euro\t]", 
                  "\n\t[\tCosto Falso Allarme  :\t", 10      ,"\t euro\t]", 
                  "\n\t[\tIndice di Salvataggio:\t", 0.4*100 ,"\t  %  \t]",
                  "\n+-------------------------------------------------------+"), 
      main = paste(" +-------------------------+","\n | Costo  = " ,10, " euro |", "\n+-------------------------+"))
par(opar)

The resulting image is: enter image description here

In order to have the subtitle separated from the image I have changed the title part in the following way.

title(main = paste(" +-------------------------+","\n | Costo  = " ,costo, " euro |", "\n+-------------------------+"))
title(sub = paste("\n\n\n +-------------------------------------------------------+",
                      "\n[\tCosto Mancato Allarme:\t", z2            ,"\t euro\t]", 
                      "\n[\tCosto Falso Allarme  :\t", z1            ,"\t euro\t]", 
                      "\n[\tIndice di Salvataggio:\t", savingRate*100,"\t  %  \t]",
                      "\n+-------------------------------------------------------+"), 
        line = 7
)

enter image description here

Is there a better way to show the information contained in the title and subtitle and the image? Something like boxes, rectangles, and so on?

Andrea Ianni
  • 829
  • 12
  • 24
  • Do you just mean that you want to get rid of the overlap? – Hack-R Nov 02 '16 at 16:53
  • I'd like to avoid the overlapping obviously... in addition I'd like to know if there exists a better way (mine is not that "charming") to plot the image with the information I have inserted (es: boxes containing the same information about costs, etc.)... – Andrea Ianni Nov 02 '16 at 17:01
  • 1
    For the spacing - don't use `title` use `mtext` and you can adjust the `line` argument to alter how close it is to the plot. – Gregor Thomas Nov 02 '16 at 17:56
  • 1
    Or [maybe `plotrix::textbox` as suggested here?](http://stackoverflow.com/a/7730492/903061) – Gregor Thomas Nov 02 '16 at 18:01
  • I've tried to use the plotrix::textbox but, maybe for the way I have used to draw the image, it doesn't modify the result..! – Andrea Ianni Nov 02 '16 at 20:03

0 Answers0