Working with levelplot in lattice, I have figured out how to display the corresponding value of each cell. For a matrix m:
myPanel <- function(x,y,z, ...){
panel.levelplot(x,y,z,...)
panel.text(x,y, round(m,2),col=bw[col.m])
}
levelplot(m, col.regions=col.range, colorkey=NULL, xlab=NULL, ylab=NULL,
scales = list(x = list(draw = FALSE), y = list(draw = FALSE)),
panel= myPanel)
The rounded matrix values are
round(m,2)
13 14 15 16 17 18
GDcsp -0.44 -0.34 -0.39 -0.35 -0.53 -0.60
GDsor 0.14 0.07 0.03 0.01 0.06 0.09
GDdup 0.43 0.36 0.34 0.36 0.46 0.52
GDhsw 0.22 0.05 0.11 0.00 0.20 0.26
Gdwpa 0.17 0.25 0.32 0.37 0.46 0.47
The problem is that -0.60 and 0.00 are displayed in the corresponding cell as 0.6 and 0, respectively, while I would like to have all numbers with two decimals. Any idea to solve this would be most welcome.
myPanel <- function(x,y,z, ...){
panel.levelplot(x,y,z,...)
panel.text(x,y, sprintf("%.2f", m))
}
levelplot(m, colorkey=NULL, xlab=NULL, ylab=NULL,
scales = list(x = list(draw = FALSE), y = list(draw = FALSE)),
panel= myPanel)
You can use sprintf
to force the output to be 2 decimal places.