Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fine-grained control over plotting order in Gadfly?

Tags:

julia

I'm creating a scatterplot that looks something like this:

using DataFrames
using Gadfly
using ColorBrewer
using Distributions
colors = palette("Set1", 4)
df1 = DataFrame(rand(Normal(0, 0.5), 1000,2))
df1[:x3] = :a
df2 = DataFrame(rand(Normal(-0.25, 0.25), 500,2))
df2[:x3] = :b
df3 = DataFrame(rand(Normal(0.25, 0.25), 500,2))
df3[:x3] = :c
df4 = DataFrame(rand(Normal(0, 0.25), 500,2))
df4[:x3] = :d
df = vcat(df1, df2, df3, df4)
plot(df, x=:x1, y=:x2, color=:x3, Geom.point, Scale.color_discrete_manual(colors..., levels=[:b, :c, :d, :a]),
Theme(highlight_width=0pt))

I want the points plotted from front to back in this order [:d, :b, :c, :a] so that the larger number of points in :a are in the back. So why do I have to specify the order as levels=[:b, :c, :d, :a] do get my desired result. What's the discrepancy here?

enter image description here

Also, interestingly it seems as though the order depends on what colors are used!? as trying different colors from ColorBrewer leads to different ordering results, which is probably a bug. Relevant issue: https://github.com/dcjones/Gadfly.jl/issues/858

like image 995
tlnagy Avatar asked Jun 24 '16 18:06

tlnagy


1 Answers

FWIW, I have given up trying to fully control what layers overwrite what layers when using Gadfly.

Maybe it is even quite difficult to do, because sometimes my exact same code, run twice, produces two slightly different figures, in which the order in which a layer overwrites another layer has changed, apparently randomly. This happens at least when I send the output figure to a postscript file via Gadfly.draw(PS(file, size...), p), which is what I usually do.

Of course this may improve in the future. I presently use Gadfly 0.5.2 in Julia 0.5.0 under Windows 10, 64 bits.

like image 195
Felipe Jiménez Avatar answered Oct 15 '22 17:10

Felipe Jiménez