tags:

views:

110

answers:

1

The following code assigns a manual color scale of red and black to my points:

require(ggplot2)
require(directlabels)
dtest <- data.frame(x=1:20,
                  y=rnorm(20,0,5),
                  v=seq(1,2))
p <- ggplot(dtest, aes(x=x,y=y,color=as.factor(v))) + geom_point() + scale_colour_manual(values=c("red","black"))
p #this looks good; red and black as intended

direct.label(p) #this falls back on the default colors

But when I apply direct.label() to the same plot, it overrides the color scale in favor of the ggplot default. Is there a way to prevent this? If not, what's the best way to assign new colors to the default ggplot scale? Thanks, Matt

+1  A: 

This was happening because direct.label(p) operates by adding the label geom to p, then by hiding the color legend, since labeling the colors twice would be redundant. One way to hide the color legend is by adding scale_colour_discrete(legend=FALSE), and this is what I was using inside of direct.label. So applying scale_colour_discrete after scale_colour_manual will cause your manual color scale to be ignored. I have updated directlabels so that instead of overwriting the previous scale, it just looks for the color scale and turns off the legend. So installing the most recent version of directlabels should solve your problem:

install.packages("directlabels", repos="http://R-Forge.R-project.org")

Toby Dylan Hocking