3

How to make drop=TRUE work (so legend contains only categories that exist in the subset) within scale_colour_discrete when using ggplot and trying to have stable colour mapping for categories in different plots?

This question is linked to this one and especially this comment.

Reproducible code borrowed from one of the answers in the linked question:

set.seed(2014)
library(ggplot2)
dataset <- data.frame(category = rep(LETTERS[1:5], 100),
                      x = rnorm(500, mean = rep(1:5, 100)),
                      y = rnorm(500, mean = rep(1:5, 100)))
dataset$fCategory <- factor(dataset$category)
subdata <- subset(dataset, category %in% c("A", "D", "E"))
ggplot(dataset, aes(x = x, y = y, colour = fCategory)) + geom_point()
ggplot(subdata, aes(x = x, y = y, colour = fCategory)) + geom_point() + 
       scale_colour_discrete(drop=TRUE,limits = levels(dataset$fCategory))

Why does the drop=TRUE not work in the second plot? The legend still contains all categories.

Output from sessionInfo():

R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United     Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_1.0.0

loaded via a namespace (and not attached):
[1] colorspace_1.2-4 digest_0.6.8     grid_3.1.2       gtable_0.1.2         labeling_0.3    
[6] MASS_7.3-35      munsell_0.4.2    plyr_1.8.1       proto_0.3-10         Rcpp_0.11.3     
[11] reshape2_1.4.1   scales_0.2.4     stringr_0.6.2    tools_3.1.2     
Community
  • 1
  • 1
Sirvydas
  • 154
  • 7
  • I tried your example and I have stable mapping (i.e. categories A-E have the same colors in both plots) either with or without `drop=TRUE`. Could it be a version-related issue? I have ggplot2_1.0.0 – Marat Talipov Feb 02 '15 at 21:20
  • @MaratTalipov Stable mapping works, but my question is concerned about drop=TRUE parameter not working. The legend in the second plot still contains all categories. I've edited my question to make it more clear and also added the session info. – Sirvydas Feb 03 '15 at 06:42

1 Answers1

2

This is either a misconception of what drop does (the help entry does not give much detail, unfortunately) or a bug. However, I'd recommend dropping drop altogether (pun intended) and setting both limits and breaks:

ggplot(subdata, aes(x = x, y = y, colour = fCategory)) + geom_point() + 
  scale_colour_discrete(limits = levels(dataset$fCategory), 
                        breaks = unique(subdata$fCategory))

enter image description here

The colour set is consistent, the legend is fine.

tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • Thanks for suggesting breaks. I suggest you edit your answer to `breaks = unique(subdata$fCategory)` because we don't know which levels are present in advance. – Sirvydas Feb 04 '15 at 10:12