title: two y-axes with different scales for two datasets in ggplot2
body: <p>I have two datasets (can be combined into a single one) that share common x values, while the y values are different - I want to plot the y values in one dataset and put the y-axis on the left of the plot, while plotting the y values in the other dataset and put the y-axis on the right of the same plot. Of course, the relative scales for the two y-axis values are different (actually should be "adjusted" according to the y values in the first dataset. The points in the two datasets will be in different colors in order to distinguish the two scales. </p>
  
  <p>An example is shown below:</p>
  
  <pre><code>d1 = data.frame(x=c(100, 200, 300, 400), y=seq(0.1, 0.4, by=0.1)) # 1st dataset
  d2 = data.frame(x=c(100, 200, 300, 400), y=seq(0.8, 0.5, by=-0.1)) # 2nd dataset
  p1 = ggplot(data = d1, aes(x=x, y=y)) + geom_point()
  p2 = ggplot(data = d2, aes(x=x, y=y)) + geom_point() +
    scale_y_continuous(position = "right")
  p1
  p2
  </code></pre>
  
  <p>In <code>ggplot2</code>, I cannot do <code>p1+p2</code> as it will show an error message <code>Error: Don't know how to add o to a plot</code>. Please help. Thank you!</p>
  
  <p><a href="https://i.stack.imgur.com/1TyLB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1TyLB.jpg" alt="enter image description here"></a></p>