title: R - creating a bar and line on same chart, how to add a second y axis
body: <p>I'm trying to create a ggplot2 graph showing a bar graph and a line graph overlaying each other. In excel this would be done by adding a second axis.</p>
  
  <p>The x axis represents product type, the y values of the bar graph should represent revenue, and the line graph I want to represent profit margin as a percentage. The value of the line graph and the bar chart should be independent of each other, i.e. there is no such relationship.</p>
  
  <pre><code>require(ggplot2)    
  df &lt;- data.frame(x = c(1:5), y = abs(rnorm(5)*100))
  df$y2 &lt;- abs(rnorm(5))
  
  ggplot(df, mapping= aes(x=as.factor(`x`), y = `y`)) + 
    geom_col(aes(x=as.factor(`x`), y = `y`),fill = 'blue')+
    geom_line(mapping= aes(x=as.factor(`x`), y = `y`),group=1) +
    geom_label(aes(label= round(y2,2))) +
    scale_y_continuous() +
    theme_bw() + 
    theme(axis.text.x = element_text(angle = 20,hjust=1)) 
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/j2Nzl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j2Nzl.png" alt="enter image description here"></a></p>
  
  <p>The image above produces almost what I want. However, the scaling is incorrect - I would need the 1.38 and 0.23 value to be ordered by magnitude, i.e. the point 0.23 should show below 1.38. I am also not sure how to add another axsis on the right hand side.</p>