title: How to overlay geom_bar and geom_line plots with different number of elements using ggplot2?
body: <p>Assuming I have two data.frames with different data but in the same range of x-values</p>
  
  <pre><code>a &lt;-data.frame(x=c(1,1,1,2,2,2,3,3,3),
                 y=c(0.3,0.4,0.3,0.2,0.5,0.3,0.4,0.4,0.2), 
                 z=c("do","re","mi","do","re","mi","do","re","mi"))
  
  b &lt;- data.frame(x=c(1,2,3),y=c(10,15,8))
  </code></pre>
  
  <p>Both, a and b have the same range of X values (1,2,3) but while a is a data.frame with 9 rows, b is a data.frame with 3 rows.</p>
  
  <p>I use geom_bar in order to plot the distribution of values of a, like this:</p>
  
  <pre><code>ggplot(a, aes(x=x, y=y, fill=z)) +
      geom_bar(position="stack",stat="identity") + 
      ylab("") + 
      xlab("x")
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ycXHj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ycXHj.png" alt="enter image description here"></a></p>
  
  <p>And I use geom_line to plot b data, like this:</p>
  
  <pre><code>ggplot(b, aes(x=x, y=y)) + 
      geom_line(stat="identity") + 
      ylab("") + xlab("x") + ylim(0,15)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/uf3mX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uf3mX.png" alt="enter image description here"></a></p>
  
  <p>Now I would like to overlay this geom_line plot to the previous geom_bar plot. My first try was to do the following:</p>
  
  <pre><code>ggplot(a, aes(x=x, y=y, fill=z)) +
      geom_bar(position="stack",stat="identity") + 
      ylab("") + xlab("x") +
      ggplot(b, aes(x=x, y=y)) + 
      geom_line(stat="identity") + 
      ylab("") + xlab("x") + ylim(0,15)
  </code></pre>
  
  <p>With no success.</p>
  
  <p>How can I overlay a geom_line plot to a geom_bar plot?</p>