user2669497
user2669497

Reputation: 157

plot color matrix with numbers in R

I am using the plotrix in R plot the color matrix with the numbers, here is the example code :

set.seed(1)

x<-matrix(rnorm(64),nrow=8)

# simulate a correlation matrix with values -0.5 to 0.5
x<-rescale(x,c(-0.5,0.5))

cellcol<-color.scale(cbind(x,c(-1,rep(1,7))),c(0,1),0,c(1,0))[,1:7]

color2D.matplot(x,cellcolors=cellcol,main="Blue to red correlations")

cellcol<-color.scale(cbind(x,c(-1,rep(1,7))),c(0,1),0,c(1,0))[,1:7]

color2D.matplot(x,cellcolors=cellcol,main="Blue to red correlations",show.values=2)

I want the numbers to be shown as two decimals, e.g.as 0.12. I used the show.values=2, which basically will shown the value with two decimals. This should work!!!! But in the plot, for 0.500000, it still showed 0.5, why could this happen? any suggestions?

Upvotes: 0

Views: 808

Answers (1)

qdread
qdread

Reputation: 3943

One workaround is to edit the function definition of color2D.matplot().

Call fix(color2D.matplot)

Lines 97-99 should read:

 text(sort(rep((1:xdim[2]) - 0.5, xdim[1])), 
      texty, round(x, show.values), col = vcol, 
      cex = vcex)

Replace them with the following:

 text(sort(rep((1:xdim[2]) - 0.5, xdim[1])), 
      texty, sprintf("%.2f", round(x, show.values)), col = vcol, 
      cex = vcex)

The first argument to sprintf() is a format string indicating that the string should be formatted as a floating point number with two trailing zeroes.

Upvotes: 1

Related Questions