0

I have several dataframes that I use to create plots. In these plots, I would like the fill-colour to be based on the factor levels of a certain variable, but in some of the dataframes, some factors do not exist (e.g. out of 3 different potential factor levels, only 2 actually occur), which makes the colours across graphs for similar factor levels different. I would like them to be the same. So far I have assigned the desired colours as a column in the dataframe, and I have tried to pass this to ggplot, but my solution does not work very well. Let me show you what I mean:

 library(plyr); library(dplyr); library(ggplot2)
 dat <- mtcars
 dat$col <- mapvalues(dat$cyl, from=c(4,6,8), to=c("yellow", "red", "grey"))

 q <- ggplot(dat, aes(x=gear, y=carb)) + 
             geom_bar(stat="identity", position="dodge", aes(fill=factor(cyl))) + 
             scale_fill_manual(values=unique(dat$col))

 dat2 <- filter(dat, cyl>4)
 p <- ggplot(dat2, aes(x=gear, y=carb)) + 
      geom_bar(stat="identity", position="dodge", aes(fill=factor(cyl))) + 
      scale_fill_manual(values=unique(dat2$col))

Comparing both graphs reveals that not the same colours were used. The problem lies in the fact that unique(dat$col) does not necessarily give the factors in my desired order, so I am looking for a more robust solution. Most importantly, it seems that this should be easy and I feel that I am probably overlooking a really simple way to fix this, which is why I am asking here now. Does anyone have a good idea? Any hint would be appreciated!

coffeinjunky
  • 11,254
  • 39
  • 57
  • The `filter`-call throws an error. Looking at its help page it doesn't appear that the function expects a column name or logical expression as an argument. – IRTFM Mar 14 '15 at 04:37
  • My apologies. I fixed the code above. Now it should work just fine. I forgot to load `dplyr` for `filter`. – coffeinjunky Mar 14 '15 at 04:39
  • 1
    Seems related: http://stackoverflow.com/questions/6919025/how-to-assign-colors-to-categorical-variables-in-ggplot2-that-have-stable-mappin – tonytonov Mar 14 '15 at 15:11
  • 1
    Thanks @tonytonov, that is virtually the same question. Feel free to make it an answer so that I can accept it. Otherwise, any moderator can mark this question as a duplicate. – coffeinjunky Mar 14 '15 at 16:34
  • 1
    In the meanwhile, anyone else reading this and wondering about the same: tonytonov's linked question led me to realize that I can pass named values to `scale_fill_manual`, which is my preferred solution. For instance, using `scale_fill_manual(values=c("4"="yellow", "6"="orange", "8"="red" ))` keeps the colours the same even if there is no "4" in the dataset. – coffeinjunky Mar 14 '15 at 16:37
  • Thanks for the note. I'll mark it as a duplicate if you don't mind. – tonytonov Mar 14 '15 at 18:14

0 Answers0