# Overlay plot with second plot with second y-axis to the right?

**URL:** https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558
**Category:** Visualization
**Tags:** question, juno
**Created:** [October 19, 2017, 12:41pm UTC](https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558 "2017-10-19T12:41:53Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)
#### Post date: [October 19, 2017, 12:41pm UTC](https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558/1 "2017-10-19T12:41:53Z")

</div>

Currently I’m using RCall, to plot in R, but I’m trying to switch to plotting directly in Julia (in Juno IDE, on Linux mint). I’m trying to overlay a plot I made with Plots.jl using the GR backend with a second plot with a differently scaled y-axis on the right side of the picture. The figure I want to produce should look similar to that:  
 ![Populationsize_4_with_mutation_h_g](https://global.discourse-cdn.com/julialang/original/3X/c/b/cbce562c90b0a0d4dec03c06c4a064c6eca3c9c0.jpeg)

With GR I got as far as the following:

> display(plot(1:Generations, Npop, title=titlemain, lw=2, c= :green,  
> xlabel=“Generations”, ylabel=“Populationsize”, ylims=0:(2\*K), leg=false))

This gives me a plot similar to only the green data in the picture.  
Now, I want to add a second y axis on the right size and use that to overlay the first (green in the picture) plot with a second line. This is necessary, because the datasets are scaled so differently (up to several 1000 in the first, and 0 - 4 in the second one). Does anybody have an idea how to achieve this? I tried searching the Plots.jl documentation and the GR framework homepage, but I didn’t find info an how to set the side on which the y axis is displayed or how to overlay two plots… I’m really grateful to anybody with an idea!

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [October 19, 2017, 1:24pm UTC](https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558/2 "2017-10-19T13:24:52Z")

</div>

With Plots, you’d do `plot(vector1); plot(twinx(), vector2)`. You only need to call `display` explicitly if you’re in a context where the plot isn’t returned to console (such as inside a for loop).

---

<div class="post-metadata">

### Author: ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)
#### Post date: [October 19, 2017, 1:55pm UTC](https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558/3 "2017-10-19T13:55:52Z")

</div>

Thanks so much! This does exactly what I want! I’m using `display()` because I’m using Juno and without it, the plot isn’t shown in the plotpane. But thanks for the heads up. `twinx()` is exactly what I was looking for and it’s so much easier and faster than using RCall and plotting in R. That’s why I love Julia…

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [October 19, 2017, 3:32pm UTC](https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558/4 "2017-10-19T15:32:28Z")

</div>

Great to hear 🙂 You might want to ask on the juno gitter channel why your plot isn’t showing, it should…

---

<div class="post-metadata">

### Author: ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)
#### Post date: [October 20, 2017, 6:05am UTC](https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558/5 "2017-10-20T06:05:53Z")

</div>

Thanks! I forgot to include, that I’m using the `plot()` command in a function, so not displaying would be what I would expect, as it’s the same in loops etc. When I’m directly running `plot()` without the envelope, it shows in the plotpane. Thanks for the tip with the gitter channel though 🙂

---

<div class="post-metadata">

### Author: ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)
#### Post date: [December 1, 2017, 10:59am UTC](https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558/6 "2017-12-01T10:59:58Z")

</div>

Quick question just came to mind: How would I go about adding the label to the second (right) y-axis? If I use `ylabel="ylabel"` it appears on the left side, overlapping with the left ylabel… Is there something like `ylabelposition=:right` ? Thanks again!

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [December 1, 2017, 12:24pm UTC](https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558/7 "2017-12-01T12:24:36Z")

</div>

that’s a bug. Would you mind opening an issue?

---

<div class="post-metadata">

### Author: ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)
#### Post date: [December 1, 2017, 2:40pm UTC](https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558/8 "2017-12-01T14:40:13Z")

</div>

Never done that before, but I will try 🙂 Thanks!

---

<div class="post-metadata">

### Author: ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)
#### Post date: [December 4, 2017, 8:54am UTC](https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558/9 "2017-12-04T08:54:24Z")

</div>

I opened an issue for the problem with `twinx()`: [https://github.com/JuliaPlots/Plots.jl/issues/1276](https://github.com/JuliaPlots/Plots.jl/issues/1276)

---

<div class="post-metadata">

### Author: ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)
#### Post date: [November 5, 2019, 4:12pm UTC](https://discourse.julialang.org/t/overlay-plot-with-second-plot-with-second-y-axis-to-the-right/6558/10 "2019-11-05T16:12:57Z")

</div>

I am trying to use Plots pyplot() with twinx() but the label on the right side does not appear, neither the vertical line (only the numbers)
