Gadfly - add layer caused missing column error

Encountered “missing column” error when adding layer. Using example from Gadfly - Layers. MWE is below.

using Gadfly
using RDatasets

iris = dataset("datasets", "iris")
orchard = dataset("datasets", "OrchardSprays")

# Original example
#xdata = sort(iris.SepalWidth)
#ydata = cumsum(xdata)
#line = layer(x=xdata, y=ydata, Geom.line, color=[colorant"red"],
#    Theme(line_width=1pt))
#bars = layer(iris, x=:SepalWidth, Geom.bar)
#plot(line, bars)

p = plot(iris, x=:SepalWidth, y=:SepalWidth, color=:PetalLength, 
    Geom.point,
    layer(orchard, x=:ColPos, y=:RowPos, Geom.point)
    )

The error is as below. It seems that the added layer tried to find “PetalLength” column which only exist in original layer.

ERROR: ArgumentError: column name :PetalLength not found in the data frame
Stacktrace:
  [1] lookupname

Appreciate your help. Thank you.

try putting iris in it’s own layer

As suggested, iris as it’s own layer works. The code in first post was working some versions ago. Latest/ recent versions does not work, until your recommendation. Bugs or code/ concept change?

p = plot(
    layer(iris, x=:SepalWidth, y=:SepalWidth, color=:PetalLength, Geom.point),
    layer(orchard, x=:ColPos, y=:RowPos, Geom.point)
    )

Thanks for your help.

That is the intended behaviour: layers will inherit aesthetic mappings from the plot (see docs).

Layers also share scales with other layers (and the plot). E.g. In the following plot, the orchard points share the same numeric color scale:

plot(iris, x=:SepalWidth, y=:SepalWidth, color=:PetalLength, Geom.point,
    layer(orchard, x=:ColPos, y=:RowPos, Geom.point, color=[4])
    )

That’s interesting. For new layer, it got to replicate and define its properties (e.g. color) similar to the first layer. Why can’t it defaults to some default settings or theme?

The suggestion from @bjarthur does make sense. But again, the first layer is practically “nothing”, so doesn’t fit the inheritance concept.

What about this (what would you expect?)

plot(iris, x=:SepalWidth, y=:SepalWidth,
    layer(color=:PetalLength, Geom.point),
    layer(orchard, x=:ColPos, y=:RowPos, Geom.point),
    )

Your codes also works, as well as code below. Both are similar to @bjarthur, but not sure if they differs internally. Results from 3 versions are as expected. It is just something that caused my code (from 2020) fails to run properly now, with the missing column error.

p = plot(iris,
    layer(x=:SepalWidth, y=:SepalWidth, color=:PetalLength, Geom.point),
    layer(orchard, x=:ColPos, y=:RowPos, Geom.point),
    )

A further experiment, if the aesthetic (color or size) is not define in the layer 0, it just works fine.

p = plot(iris, x=:SepalWidth, y=:SepalWidth, Geom.point,
    layer(orchard, x=:ColPos, y=:RowPos, Geom.point)
    )
p = plot(iris, x=:SepalWidth, y=:SepalWidth, Geom.point,
    layer(orchard, x=:ColPos, y=:RowPos, color=:Treatment, Geom.point)
    )