79640718

Date: 2025-05-27 14:59:47
Score: 0.5
Natty:
Report link

Haven't seen upper right before, hence there may be to many revs in this unrevised full base R approach around image(). Needed: some code reviev; you are welcome to comment.

We might want to develope our own corrplot routine based on

# v0
M = cor(mtcars, use = 'pairwise.complete.obs', method = 'pearson')

local({
  ## pre-allocation 
  M[lower.tri(M)] = NA
  s = seq(nrow(M))
  v = as.vector(M[ , rev(s)]) # flip
  g = hcl.colors(8, 'Grays', rev=TRUE)
  p = g[.bincode(v, seq.int(-1, 1, length.out=9))]
  p[is.na(p)] = '#FFFFFF' # (white)

  ## correlation image 
  par(mai = c(.1, 1, 1, .1))
  layout(matrix(1:2, nrow=1), widths=c(0.8,0.2)) 
  image(s, s, M, axes=FALSE, col=NA, xlab='', ylab='')
  with(expand.grid(y=rev(s), x=rev(s)), 
       text(x, y, labels=round(v, 2), col=p, font=2)) 
  
  axis(3, s, colnames(M), tick=FALSE)
  text(rev(s)-1, s, rev(colnames(M)))
  axis(2, rownames(M)[1], at=s[length(s)], tick=FALSE, las=2)
  # TODO: combine into one

  ## legend 
  # https://stackoverflow.com/a/13389693/20002111
  par(mai = c(.1, .1, 1, .1))
  xl = 1; xr = 1.2
  yb = 1; yt = 2
  plot(NA, xlim=c(1,2), ylim=c(1,2), ann=FALSE,
       xaxt='n', yaxt='n', bty='n', type='n')
  rect(xl, head(seq(yb,yt,(yt-yb)/8),-1),
       xr, tail(seq(yb,yt,(yt-yb)/8),-1), col=g)
  axis(4, seq(1, 2, .125), seq(-1, 1, .25), las=2, 
       pos=1.1, col=NA, col.ticks=1) # or mtext
})

Notice that I have re-named car_matrix to M.

base_corrplot_upper_right

Do .00 and some grid lines add valuable information?

Reasons:
  • Blacklisted phrase (1): to comment
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • High reputation (-2):
Posted by: Friede