ramnathv / rblocks

A fun and visual way to learn data structures and control flow in R.
26 stars 9 forks source link

Produce squares instead of rectangles #2

Open ramnathv opened 10 years ago

ramnathv commented 10 years ago

Suggestion from Paul Murrell

Always produce squares instead of rectangles, even when nrow != ncol. I need to tweak the basic plot function to scale the two dimensions appropriately while drawing.

ramnathv commented 10 years ago

These are the two pieces in the code that are to be tweaked. I need to scale the units across the two dimensions such that I always end up creating squares.

plot(1, 1, xlim = c(0, o + 1), ylim = c(n + 1, 0), type = "n",
   axes = FALSE, xlab = "", ylab = ""
)
...
for (i in 1:n){
    for (j in 1:o) {
      rect(j - gap, i + 0.5, j + gap, i - 0.5, 
           col = data[i, j],  xpd = TRUE, border = 'white'
      )
    }
}
ramnathv commented 10 years ago

I still have rectangles instead of squares while drawing data frames. This arises due to the gap variable I introduced to leave a gap between columns to distinguish data frames from matrices. I need to rewrite the code a bit so that I am drawing squares, but there is still a gap between columns.