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

question
juno

#1

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!


Plot(twinx(), ...) creates ylabel on the wrong side of the plot (left instead of ríght)
#2

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


#3

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…


#4

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


#5

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:


#6

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!


#7

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


#8

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


#9

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