I have a project where I create a multi-page pdf. Each page has a separate graph based on a subset of the data frame using plyr. I use a factor to control the color of the dots in each plot, but I cannot make the colors consistent.
The example below creates a two-page pdf file that reproduces what I am experiencing. You will notice that when the filtered graph No. 2 contains no entries for red. When a chart is generated, the points that I try to draw in blue are actually displayed in red.
require(plyr) require(ggplot2) graphNumber <- c(1,1,1,1,1,1,2,2,2,2,2,2) x <- c(1,2,3,4,5,6,1,2,3,4,5,6) y <- c(1,2,3,4,5,6,1,2,3,4,5,6) theColor <- c("red","red","red","blue","blue","blue","blue","blue","blue","blue","blue","blue") temp <- data.frame(graphNumber, x, y, theColor) temp$theColor = factor(temp$theColor, levels=c("red","blue")) str(temp) plotpattern <- function(dfIn) { qplot(x, y, data=dfIn, color=theColor, geom = "point") } pdf("test.pdf", width = 6, height = 4) d_ply(temp, .(graphNumber), failwith(NA, plotpattern), .print = TRUE) dev.off()
Any thoughts on what I need to change to get ggplot to consistently recognize all my factors when assigning colors? Even if certain factors do not appear in the data when filtering?