0

I would like to plot a series of scatter points with their own color gradient to a scatter plot with an on-off color scheme. Using the mtcars dataset, best attempt is as follows:

library(ggplot2)

#Using the cars dataset. vs is intended to be on/off, qsec,mgt, and wt is intended to be continous
Data1=mtcars[1:15,c("mpg","wt","vs")]
Data1$vs=as.factor(Data1$vs)
Data2=mtcars[1:30,c("mpg","wt","qsec")]

#This works
graph=ggplot(Data1, aes(x=mpg, y=wt, colour=vs))  + 
geom_point(size=2) +scale_shape_manual(values=c(19,19))+ scale_colour_brewer(palette="Set1")

#This doesn't - gives an error about needed to recieve a continuous variable. In my original dataset, this isn't a problem. This syntax doesn't work.
graph= graph + layer(data = Data2, geom="point", aes(x=mpg, y=wt, colour=qsec)) + scale_colour_gradient(low="black", high="white")
graph

I can't provide the actual data because its proprietary. But that shouldn't matter. Any help is greatly appreciated. Thank you!

Duane321
  • 25
  • 5
  • The basic design in `ggplot` is one scale per `aes`thetic (e.g. one scale per `colour`. Thus, if you wish two (or more) scales, work-arounds are needed. Often they involve creation of one or more plot object, manipulation of the various components of the object, and then producing a new plot from the manipulated object(s). Perhaps my answer [**here**](http://stackoverflow.com/questions/20474465/using-different-scales-as-fill-based-on-factor/20479882#20479882) may get you started. I am sure there are more, and perhaps better, examples on such manipulations on SO. – Henrik Oct 30 '14 at 07:44
  • See e.g. [**here**](http://stackoverflow.com/questions/16129876/ggplot2-multiple-scales-legends-per-aesthetic-revisited) and @hadley's opinion [**here**](https://groups.google.com/forum/#!topic/ggplot2/lDvsd4yJ0AE) – Henrik Oct 30 '14 at 07:48

0 Answers0