5

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?

David Pedack
  • 482
  • 2
  • 10
  • Have a look at: Manually Setting Group Colors - http://stackoverflow.com/questions/17180115/manually-setting-group-colors-for-ggplot2 – Ricardo Saporta Sep 24 '14 at 01:49
  • same question as [here](http://stackoverflow.com/questions/6919025/how-to-assign-colors-to-categorical-variables-in-ggplot2-that-have-stable-mappin)? – B.Mr.W. Sep 24 '14 at 01:51
  • 1
    append `+ scale_colour_discrete(drop=TRUE,limits = levels(temp$theColor))` will make it work. – B.Mr.W. Sep 24 '14 at 02:01

0 Answers0