Bryan Hanson
2008-Jun-21 17:40 UTC
[R] Coloring Stripchart Points, or Better, Lattice Equivalent
Hi All. I have the commands below to create a stripchart/plot. I was hoping to color the points in the plot by yr, and use a symbol that varied with resp. However, the outcome makes it appear as though the point by point col and pch data is not being passed properly. Any suggestions? And truthfully, I?d rather be doing this with Lattice, but I?ve tried several variations of stripplot and can?t even get something with the general layout of the stripchart version. Thanks, Bryan ************* Bryan Hanson Professor of Chemistry & Biochemistry DePauw University, Greencastle IN USA index <- round(runif(100, 1, 100)) resp <- rbinom(100, 1, 0.5) yr <- rep(c("2005", "2006"), 50) all <- data.frame(index, resp, yr) for (n in 1:length(all$index)) { if (all$yr[n] == 2005) {all$col[n] <- "red"} else {all$col[n] <- "blue"} } for (n in 1:length(all$index)) { if (all$resp[n] == 1) {all$sym[n] <- 1} else {all$sym[n] <- 3} } stripchart(all$index, method = "stack", ylim = c(0,10), col = all$col, pch all$sym)
Bryan Hanson
2008-Jun-22 14:43 UTC
[R] Coloring Stripchart Points, or Better, Lattice Equivalent
Below is a revised set of code that demonstrates my question a little more clearly, I hope. When plotting all the data (5th panel), col & sym don't seem to be passed correctly, as the (random) first value for col & sym are used for all points (run the code, then run it again, you'll see how the 5th panel changes depending upon col & sym for the first data point). The 5th panel should ideally be the "sum" of the 4 panels above, keeping col & sym intact. Also, I would rather have this in lattice or ggplot2, if anyone sees how to convert it. Thanks once again, several of you have made very useful suggestions off list. Bryan samples <- 100 # must be even index <- round(runif(samples, 1, 100)) # set up data resp <- rbinom(samples, 1, 0.5) yr <- rep(c("2005", "2006"), samples/2) all <- data.frame(index, resp, yr) all$sym <- ifelse(all$resp == 1, 1, 3) all$col <- ifelse(all$yr == 2005, "red", "blue") all$count <- rep(1, length(all$index)) all <- all[order(all$index, all$yr, all$resp),] # for easier inspection row.names(all) <- c(1:samples) # for easier inspection one <- all[(all$yr == 2005 & all$resp == 0),] # First 2005/0 at top two <- all[(all$yr == 2005 & all$resp == 1),] # Then 2005/1 three <- all[(all$yr == 2006 & all$resp == 0),] # Now 2006/0 four <- all[(all$yr == 2006 & all$resp == 1),] # Finally 2006/1 par(mfrow = c(5, 1)) par(plt = c(0.1, 0.9, 0.25, 0.75)) stripchart(one$index, method = "stack", ylim = c(0,10), xlim = c(1,100), col = one$col, pch = one$sym) mtext("2005/0", side = 3) stripchart(two$index, method = "stack", ylim = c(0,10), xlim = c(1,100), col = two$col, pch = two$sym) mtext("2005/1", side = 3) stripchart(three$index, method = "stack", ylim = c(0,10), xlim = c(1,100), col = three$col, pch = three$sym) mtext("2006/0", side = 3) stripchart(four$index, method = "stack", ylim = c(0,10), xlim = c(1,100), col = four$col, pch = four$sym) mtext("2006/1", side = 3) stripchart(all$index, method = "stack", ylim = c(0,10), xlim = c(1,100), col = all$col, pch = all$sym) mtext("col & sym always taken from 1st data point when all data is plotted!", side = 3)