ali srn
ali srn

Reputation: 573

Plotting a table and colouring minimum values in columns

I have a data frame as below.

                              RMSE      MAE        MPE     MAPE
ARIMA(1, 1, 0)(1, 1, 1)23 540.4890 362.6894 -1.2617083 8.273222
ARIMA(1, 0, 0)(1, 1, 1)23 550.7088 373.4725 -0.9303624 8.523295
ARIMA(1, 1, 0)(2, 1, 1)23 539.1094 360.4556 -1.2885436 8.220159
ARIMA(2, 0, 0)(1, 1, 1)23 547.4179 366.4258 -0.9816881 8.374072
ARIMA(1, 0, 0)(2, 1, 1)23 548.6216 372.1675 -0.9249135 8.494618

I want to plot it as a table and want to colour that has minimum value in each row. So that I can see which model gives better results in terms of Error values. I couldnt find a easy way of doing this. This table will be uploaded to a website via R.Net. I tried this example but I didn't even pass the first part.

# load Systematic Investor Toolbox
setInternet2(TRUE)
source(gzcon(url('https://github.com/systematicinvestor

data:

structure(list(RMSE = c(540.489, 550.7088, 539.1094, 547.4179, 
548.6216), MAE = c(362.6894, 373.4725, 360.4556, 366.4258, 372.1675
), MPE = c(-1.2617083, -0.9303624, -1.2885436, -0.9816881, -0.9249135
), MAPE = c(8.273222, 8.523295, 8.220159, 8.374072, 8.494618)), .Names = 
c("RMSE", 
"MAE", "MPE", "MAPE"), class = "data.frame", row.names = c("ARIMA(1,1,0)(1,1,1)23", 
"ARIMA(1,0,0)(1,1,1)23", "ARIMA(1,1,0)(2,1,1)23", "ARIMA(2,0,0)(1,1,1)23", 
"ARIMA(1,0,0)(2,1,1)23"))

Upvotes: 1

Views: 76

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73405

Let dat be your data frame, you can do:

## colour setting for each row / model
col_set <- 1:5

## do matrix plot; extend `xlim` to the right to reserve space for legend
matplot(y = t(dat), xaxt = "n", pch = 19, col = col_set,
        xlab = "", ylab = "Error", xlim = c(1, ncol(dat)+2L))
axis(1L, seq_len(ncol(dat)), colnames(dat))

## add legend at top right corner
legend("topright", legend = rownames(dat), pch = 19, col = col_set)

## highlight the column minimum
points(seq_len(ncol(dat)), unlist(lapply(dat, min)), cex = 2, pch = 19,
       col = col_set[unlist(lapply(dat, which.min))])

enter image description here


As you observe, without rescaling, the graphical presentation is poor. We now scale all columns onto [0, 1] and make the plot.

dat <- "rownames<-"(do.call(cbind.data.frame, lapply(dat, function (u) (u - min(u)) / diff(range(u)))),
                    rownames(dat))
#                           RMSE       MAE        MPE      MAPE
#ARIMA(1,1,0)(1,1,1)23 0.1189372 0.1716077 0.07379835 0.1750468
#ARIMA(1,0,0)(1,1,1)23 1.0000000 1.0000000 0.98501527 1.0000000
#ARIMA(1,1,0)(2,1,1)23 0.0000000 0.0000000 0.00000000 0.0000000
#ARIMA(2,0,0)(1,1,1)23 0.7162870 0.4586499 0.84386716 0.5077358
#ARIMA(1,0,0)(2,1,1)23 0.8200597 0.8997457 1.00000000 0.9053989

Then using previous code, we have

enter image description here

Upvotes: 1

Related Questions