GiovineItalia / Gadfly.jl

Crafty statistical graphics for Julia.
http://gadflyjl.org/stable/
Other
1.9k stars 251 forks source link

Color facetting fails when combined with a contour layer #1098

Open phipsgabler opened 6 years ago

phipsgabler commented 6 years ago

I was trying to plot two traces of points over a function contour:

df = vcat(DataFrame(x = rand(100), y = rand(100), v = :one),
          DataFrame(x = rand(100), y = rand(100), v = :two))
plot(layer(x = linspace(0, 1, 100), y = linspace(0, 1, 100), z = (x,y) -> x + y, Geom.contour),
     layer(df, x = :x, y = :y, color = :v, Geom.point))

But doing so results in an error, which seems to have to do with the color argument:

Error showing value of type Gadfly.Plot:
ERROR: MethodError: Cannot `convert` an object of type Symbol to an object of type Float64

This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] apply_scale(::Gadfly.Scale.ContinuousColorScale, ::Array{Gadfly.Aesthetics,1}, ::Gadfly.Data, ::Vararg{Gadfly.Data,N} where N) at /home/philipp/.julia/v0.6/Gadfly/src/scale.jl:585
 [2] apply_scales(::IterTools.Distinct{Base.ValueIterator{Dict{Symbol,Gadfly.ScaleElement}},Gadfly.ScaleElement}, ::Array{Gadfly.Aesthetics,1}, ::Gadfly.Data, ::Vararg{Gadfly.Data,N} where N) at /home/philipp/.julia/v0.6/Gadfly/src/scale.jl:33
 [3] apply_scales(::IterTools.Distinct{Base.ValueIterator{Dict{Symbol,Gadfly.ScaleElement}},Gadfly.ScaleElement}, ::Gadfly.Data, ::Gadfly.Data, ::Vararg{Gadfly.Data,N} where N) at /home/philipp/.julia/v0.6/Gadfly/src/scale.jl:52
 [4] render_prepare(::Gadfly.Plot) at /home/philipp/.julia/v0.6/Gadfly/src/Gadfly.jl:670
 [5] render(::Gadfly.Plot) at /home/philipp/.julia/v0.6/Gadfly/src/Gadfly.jl:748
 [6] display(::Base.REPL.REPLDisplay{Base.REPL.LineEditREPL}, ::MIME{Symbol("text/html")}, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Gadfly/src/Gadfly.jl:1062
 [7] display(::Base.REPL.REPLDisplay{Base.REPL.LineEditREPL}, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Gadfly/src/Gadfly.jl:1007
 [8] display(::Gadfly.Plot) at ./multimedia.jl:194
 [9] hookless(::LastMain.LastMain.LastMain.LastMain.LastMain.Media.##7#8{Gadfly.Plot}) at 
/home/philipp/.julia/v0.6/Media/src/compat.jl:14                                         
 [10] render(::LastMain.LastMain.LastMain.LastMain.LastMain.Media.NoDisplay, ::Gadfly.Plot
) at /home/philipp/.julia/v0.6/Media/src/compat.jl:27                                    
 [11] display(::LastMain.LastMain.LastMain.LastMain.LastMain.Media.DisplayHook, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Media/src/compat.jl:9
 [12] display(::Gadfly.Plot) at ./multimedia.jl:194
 [13] hookless(::LastMain.LastMain.LastMain.LastMain.Media.##7#8{Gadfly.Plot}) at /home/philipp/.julia/v0.6/Media/src/compat.jl:14
 [14] render(::LastMain.LastMain.LastMain.LastMain.Media.NoDisplay, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Media/src/compat.jl:27
 [15] display(::LastMain.LastMain.LastMain.LastMain.Media.DisplayHook, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Media/src/compat.jl:9
 [16] display(::Gadfly.Plot) at ./multimedia.jl:194
 [17] hookless(::LastMain.LastMain.LastMain.Media.##7#8{Gadfly.Plot}) at /home/philipp/.julia/v0.6/Media/src/compat.jl:14
 [18] render(::LastMain.LastMain.LastMain.Media.NoDisplay, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Media/src/compat.jl:27
 [19] display(::LastMain.LastMain.LastMain.Media.DisplayHook, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Media/src/compat.jl:9
 [20] display(::Gadfly.Plot) at ./multimedia.jl:194
 [21] hookless(::LastMain.LastMain.Media.##7#8{Gadfly.Plot}) at /home/philipp/.julia/v0.6/Media/src/compat.jl:14
 [22] render(::LastMain.LastMain.Media.NoDisplay, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Media/src/compat.jl:27
 [23] display(::LastMain.LastMain.Media.DisplayHook, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Media/src/compat.jl:9
 [24] display(::Gadfly.Plot) at ./multimedia.jl:194
 [25] hookless(::LastMain.Media.##7#8{Gadfly.Plot}) at /home/philipp/.julia/v0.6/Media/src/compat.jl:14
 [26] render(::LastMain.Media.NoDisplay, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Media/src/compat.jl:27
 [27] display(::LastMain.Media.DisplayHook, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Media/src/compat.jl:9
 [28] display(::Gadfly.Plot) at ./multimedia.jl:194
 [29] hookless(::Media.##7#8{Gadfly.Plot}) at /home/philipp/.julia/v0.6/Media/src/compat.jl:14
 [30] render(::Media.NoDisplay, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Media/src/compat.jl:27
 [31] display(::Media.DisplayHook, ::Gadfly.Plot) at /home/philipp/.julia/v0.6/Media/src/compat.jl:9
 [32] display(::Gadfly.Plot) at ./multimedia.jl:194

The following, leaving out the contour plot or the color specification, both work without problems:

plot(layer(x = linspace(0, 1, 100), y = linspace(0, 1, 100), z = (x,y) -> x + y, Geom.contour),
     layer(df, x = :x, y = :y, Geom.point))
plot(layer(df, x = :x, y = :y, color = :v, Geom.point))

I use version 0.6.4.

Mattriks commented 6 years ago

The issue here is that the user is wanting a continuous color scale for Geom.contour, and a discrete color scale for Geom.point. Currently Gadfly only allows one automatic color scale. The above plot could be done like this:

theme(x) = style(default_color=parse(Colors.Colorant,x))
g = [:one, :two]
Da = [DataFrame(x=rand(20), y=rand(20), v=g[i]) for i in 1:2]
discrete_colors = ["deepskyblue", "lightgreen"]

p = plot(
      layer(x = linspace(0, 1, 100), y = linspace(0, 1, 100), z = (x,y) -> x + y, Geom.contour),
      layer(Da[1], x = :x, y = :y, theme(discrete_colors[1]), Geom.point),
      layer(Da[2], x = :x, y = :y, theme(discrete_colors[2]), Geom.point),
    Coord.cartesian(fixed=true)
)

issue1098a

But, if you want to map Geom.point to the same color scale as Geom.contour, that is possible:

 Db = vcat([DataFrame(x=rand(20), y=rand(20), v=i) for i in [0.5,1.5]]...)
p = plot(
      layer(x = linspace(0, 1, 100), y = linspace(0, 1, 100), z = (x,y) -> x + y, Geom.contour),
      layer(Db, x=:x, y=:y, color=:v, Geom.point),
      Scale.color_continuous(minvalue=0.0, maxvalue=2.0),
      Coord.cartesian(fixed=true)
)

issue1098b

phipsgabler commented 6 years ago

Ah, I didn't realize that this is a clash of scales. I'll figure out something to work around that based on you examples. Thanks!