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?? Thanks and regards, Laurence Adair [[alternative HTML version deleted]]
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. -thomas 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) invisible(NULL) }
On Friday 30 July 2004 10:13, 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??as.data.frame(table(date, id)) would have been useful, except that it coerces date and id to be factors. To work around that, you could do: u.date <- sort(unique(date)) u.id <- sort(unique(id)) mydf <- as.data.frame(table(date = factor(date, levels = u.date), id = factor(id, levels = u.id))) mydf <- subset(mydf, Freq > 0) plot(u.date[mydf$date], u.id[mydf$id], col = mydf$Freq) Hope that helps, Deepayan
Adair, Laurence <Laurence.Adair <at> parsons.com> writes:> 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??In the example below the first three lines create a random test data frame with columns date and id. The fourth line calculates the counts using table, converts it back to a data frame (which has the effect of adding a Freq column) and removes any row with a Freq of 0. The last two lines load package gregmisc and then plot the data using balloonplot from that package. set.seed(1) r <- 100 + sample(5, 25, replace = TRUE) DF <- data.frame(date = structure(rev(r), class = "Date"), id = r) DF2 <- subset(as.data.frame(table(DF)),Freq > 0) require(gregmisc) with(DF2, balloonplot(date, id, Freq))