I have a project where I am generating a multi-page pdf. Each page has a separate plot based on a subset of a dataframe using plyr. I am using a factor to control the color of the points in each plot but I can't seem to make the colors consistent.
The example below generates a two page pdf file that replicates what I am experiencing. You'll notice that when filtered graph #2 does not contain an entry for red. When the graph generates, the points I am trying to make in blue actually show up 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 might need to change to get ggplot to consistently recognize all my factors when assigning colors? Even if a particular factors does not appear in the data when filtered?