[R] Counting question

Thomas Lumley tlumley at u.washington.edu
Fri Jul 30 17:34:23 CEST 2004

On Fri, 30 Jul 2004, Adair, Laurence wrote:

> Hi All,
> Here is something that sounds simple, but I'm having trouble getting it.  I
> have a data frame with two columns, the first is date and the second is
> employee ID.  I'd like to plot date on the horizontal axis, employee ID on
> the vertical axis, and the number of times the employee appears for the
> given date as a color.  I've kluged something where I make a table
> (table(date, id)) and add points to a plot by looping through the rownames
> (employee ids) of the table.  But certainly there is a better way of doing
> this??

The function below takes a correlation matrix and plots circles whose
radius is proportional to the absolute value of the correlation.
Something along these lines would presumably work for your problem.


 shadedcorr <- function(mat, labels = colnames(mat)) {
  n <- NCOL(mat)
  diag(mat) <- NA
  mat <- mat[, n:1]
  mat <- as.vector(mat)
  pos <- mat >= 0
  par(pty = "s", las = 2, mar = c(7, 7, 4, 4))
  xy <- expand.grid(1:n, 1:n)
  plot(xy, type = "n", axes = FALSE, xlab = "", ylab = "",
  xlim = c(0.5, n + 0.5), ylim = c(0.5, n + 0.5))
  if (any(pos %in% TRUE))
  symbols(xy[, 1][pos], xy[, 2][pos], mat[pos]/2, bg = grey(0.8),
  inches = FALSE, add = TRUE)
  if (any(pos %in% FALSE))
  symbols(xy[, 1][!pos], xy[, 2][!pos], -mat[!pos]/2, bg = grey(0.2),
  inches = FALSE, add = TRUE)
  axis(2, n:1, labels)
  axis(1, 1:n, labels)

More information about the R-help mailing list