title: bar and line plot in one chart with a legend under ggplot2
body: <p>I would like put a bar and a line plot of two separate but related series on the same chart with a legend (the bar plot is of quarterly growth the line plot is of annual growth).</p>
  
  <p>I currently do it with a data.frame in wide format and code like this:</p>
  
  <pre><code>p &lt;- ggplot() +
      geom_bar(df, aes(x=Date, y=quarterly), colour='blue') +
      geom_line(df, aes(x=Date, y=annual), colour='red')
  </code></pre>
  
  <p>but I cannot work out how to add a legend, which has a red line labeled 'Annual Growth'; and a blue square labeled 'Quarterly Growth'. </p>
  
  <p>Alternatively, I cannot work out how to have differnt geoms for different series with a long-form data.frame.</p>
  
  <p>UPDATE:</p>
  
  <p>The following example code gets me part of the way towards a solution, but with a really ugly duplicate legend. Still looking for a complete solution ... This approach is based on putting the data in long form and then plotting subsets of the data ...</p>
  
  <pre><code>library(ggplot2)
  library(reshape)
  library(plyr)
  library(scales)
  
  ### --- make a fake data set
  x &lt;- rep(as.Date('2012-01-01'), 24) + (1:24)*30
  ybar &lt;- 1:24
  yline &lt;- ybar + 1
  
  df &lt;- data.frame(x=x, ybar=ybar, yline=yline)
  molten &lt;- melt(df, id.vars='x', measure.vars=c('ybar', 'yline'))
  molten$line &lt;- ifelse(molten$variable=='yline', TRUE, FALSE)
  molten$bar &lt;- ifelse(molten$variable=='ybar', TRUE, FALSE)
  
  ### --- subset the data set
  df.line  &lt;- subset(molten, line==TRUE)
  df.bar   &lt;- subset(molten, bar==TRUE)
  
  ### --- plot it
  p &lt;- ggplot() +
  geom_bar(data=df.bar, mapping=aes(x=x, y=value, fill=variable, colour=variable),
      stat='identity', position='dodge') +
  geom_line(data=df.line, mapping=aes(x=x, y=value, colour=variable)) +
  
  opts(title="Test Plot", legend.position="right") 
  
  ggsave(p, width=5, height=3, filename='plot.png', dpi=150)
  </code></pre>
  
  <p>And an example plot ...</p>
  
  <p><img src="https://i.stack.imgur.com/chorh.png" alt="enter image description here"></p>