title: Second Y-Axis in ggplot (R)
body: <p>I am trying to make a plot with ggplot in a Shiny app in R and I need to set a second Y-axis in it.  This plot has two types of graphics: lines and bars.  I would like to represent the bars (depth of precipitation) on the left, and the lines (flows) on the right.</p>
  
  <p>My current code is:</p>
  
  <pre><code>output$plotRout &lt;- renderPlot({                                     
      ggplot(totalRR(),aes(x=time)) +
        geom_bar(aes(y=mm), stat = "identity",fill = "dodgerblue",color = "black") +
        geom_bar(aes(y=NetRain), stat = "identity",fill = "Cyan",color = "black") +
        geom_line(aes(y=DirRun, colour = "Direct Runoff"), stat = "identity",color = "Red") +
        geom_line(aes(y=BF, colour = "Baseflow"), stat = "identity",color = "Darkorange", linetype = "longdash") +
        scale_y_continuous("Rainfall (mm)", sec.axis = sec_axis(~.*10, name = "Flow (m3/s)")) +
        xlab("Time (h)")
    })
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/8Tybf.png" rel="nofollow noreferrer">The result is:</a></p>
  
  <p><a href="https://i.stack.imgur.com/8Tybf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8Tybf.png" alt="enter image description here"></a></p>
  
  <p>This plot has on the left the values of the flows, the values that should be on the right, whereas the values of rainfall (the bars) are not displayed on the plot.</p>
  
  <p>How could I make this plot putting the values of the bars (rainfall) on the left and the second y-axis on the right showing the values of the lines (flows)?</p>
  
  <p>Many thanks in advance.</p>
  
  <p>Victor</p>