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

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

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!

1 Like

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).

4 Likes

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…

2 Likes

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

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 :slight_smile:

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!

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

Never done that before, but I will try :slight_smile: Thanks!

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

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)