title: Plotting discrete and continuous scales in same ggplot
body: <p>I would like to plot some different data items using ggplot2, using two different colour scales (one continuous and one discrete from two different df's). I can plot either exactly how I would like individually, but I cannot make them work together. It seems like you cannot have two different colour scales operating in the same plot? I have seen similar questions <a href="https://stackoverflow.com/questions/9232331/ggplot2-multiple-color-scales-or-shift-colors-systematically-on-different-layer">here</a> and <a href="https://stackoverflow.com/questions/9916194/two-color-scales-for-geom-line-in-ggplot2">here</a>, and this had led me to believe that what I would like to achieve is simply not possible in ggplot2, but just in case I am wrong I would like to illustrate my problem to see if there is a work-around.</p>
  
  <p>I have some GIS stream data which has some categorical attributes attached to it, which I can plot (<code>p1</code> in code below) to get:
  <img src="https://i.stack.imgur.com/MoP8h.jpg" alt="enter image description here"></p>
  
  <p>I also have a set of locations which have a continuous response, which I can also plot (<code>p2</code> in code below) to get:
  <img src="https://i.stack.imgur.com/6ZREr.jpg" alt="enter image description here">
  However I can't combine the two (<code>p3</code> in code below). I get this error</p>
  
  <blockquote>
    <p>Error in scales[[prev_aes]] : attempt to select less than one element</p>
  </blockquote>
  
  <p>Commenting out the line <code>scale_colour_hue("Strahler order") +</code> changes the error to</p>
  
  <blockquote>
    <p>Error: Discrete value supplied to continuous scale</p>
  </blockquote>
  
  <p>Basically it seems that ggplot2 uses the same scale type (continuous or discrete) for the <code>geom_path</code> call and the <code>geom_point</code> calls. So when I pass the discrete variable,  <code>factor(Strahler)</code>, to the <code>scale_colour_gradientn</code> scale, the plot fails. </p>
  
  <p>Is there a way around this? It would be amazing if there was a <code>data</code> argument to a scales function to tell it where it should be mapping or setting attributes from. Is this even possible?</p>
  
  <p>Many thanks and reproducible code below:</p>
  
  <pre><code>library(ggplot2)
  
  ### Download df's   ###
  oldwd &lt;- getwd(); tmp &lt;- tempdir(); setwd(tmp)
  url &lt;- "http://dl.dropbox.com/u/44829974/Data.zip"
  f &lt;- paste(tmp,"\\tmp.zip",sep="")
  download.file(url,f)
  unzip(f)
  
  
  ### Read in data    ###
  riv_df &lt;- read.table("riv_df.csv", sep=",",h=T)
  afr_df &lt;- read.table("afr_df.csv", sep=",",h=T)
  vil_df &lt;- read.table("vil_df.csv", sep=",",h=T)
  
  
  ### Min and max for plot area   ###
  xmin &lt;- -18; xmax &lt;- 3; ymin &lt;- 4; ymax &lt;- 15
  
  
  ### Plot river data ###
  p1 &lt;-   ggplot(riv_df, aes(long, lat)) + 
      geom_map( mapping = aes( long , lat , map_id = id ) , fill = "white" , data = afr_df , map = afr_df ) +
      geom_path( colour = "grey95" , mapping = aes( long , lat , group = group , size = 1 ) , data = afr_df ) +
      geom_path( aes( group = id , alpha = I(Strahler/6) , colour = factor(Strahler) , size = Strahler/6 ) ) +
      scale_alpha( guide = "none" ) +
      scale_colour_hue("Strahler order") +
      scale_x_continuous( limits = c( xmin , xmax ) , expand = c( 0 , 0 ) ) +
      scale_y_continuous( limits = c( ymin , ymax ) , expand = c( 0 , 0 ) ) +
      coord_map()
  print(p1) # This may take a little while depending on computer speed...
  
  
  
  ### Plot response data  ###
  p2 &lt;- ggplot( NULL ) +
      geom_point( aes( X , Y , colour = Z) , size = 2 , shape = 19 , data = vil_df ) +
      scale_colour_gradientn( colours = rev(heat.colors(25)) , guide="colourbar" ) +
      coord_equal()
  print(p2)
  
  
  
  ### Plot both together  ###
  p3 &lt;-   ggplot(riv_df, aes(long, lat)) + 
      geom_map( mapping = aes( long , lat , map_id = id ) , fill = "white" , data = afr_df , map = afr_df ) +
      geom_path( colour = "grey95" , mapping = aes( long , lat , group = group , size = 1 ) , data = afr_df ) +
      geom_path( aes( group = id , alpha = I(Strahler/6) , colour = factor(Strahler) , size = Strahler/6 ) ) +
      scale_colour_hue("Strahler order") +
      scale_alpha( guide = "none" ) +
      scale_x_continuous( limits = c( xmin , xmax ) , expand = c( 0 , 0 ) ) +
      scale_y_continuous( limits = c( ymin , ymax ) , expand = c( 0 , 0 ) ) +
      geom_point( aes( X , Y , colour = Z) , size = 2 , shape = 19 , data = vil_df ) +
      scale_colour_gradientn( colours = rev(heat.colors(25)) , guide="colourbar" ) +
      coord_map()
  print(p3)
  #Error in scales[[prev_aes]] : attempt to select less than one element
  
  ### Clear-up downloaded files   ###
  unlink(tmp,recursive=T)
  setwd(oldwd)
  </code></pre>
  
  <p>Cheers,</p>
  
  <p>Simon</p>