# Gadfly - add layer caused missing column error

**URL:** <https://discourse.julialang.org/t/gadfly-add-layer-caused-missing-column-error/66578>\
**Category:** Tooling\
**Tags:** gadfly\
**Created:** [August 18, 2021, 12:34am UTC](https://discourse.julialang.org/t/gadfly-add-layer-caused-missing-column-error/66578 "2021-08-18T00:34:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [August 18, 2021, 12:34am UTC](https://discourse.julialang.org/t/gadfly-add-layer-caused-missing-column-error/66578/1 "2021-08-18T00:34:28Z")

</div>

Encountered “missing column” error when adding layer. Using example from [Gadfly - Layers](http://gadflyjl.org/stable/man/compositing/#Layers). MWE is below.

```julia
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.

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

```

Appreciate your help. Thank you.

---

<div class="post-metadata">

**Author:** ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)\
**Post date:** [August 18, 2021, 1:34am UTC](https://discourse.julialang.org/t/gadfly-add-layer-caused-missing-column-error/66578/2 "2021-08-18T01:34:08Z")

</div>

try putting iris in it’s own layer

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [August 18, 2021, 1:57am UTC](https://discourse.julialang.org/t/gadfly-add-layer-caused-missing-column-error/66578/3 "2021-08-18T01:57:57Z")

</div>

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?

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

```

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [August 18, 2021, 2:08am UTC](https://discourse.julialang.org/t/gadfly-add-layer-caused-missing-column-error/66578/4 "2021-08-18T02:08:26Z")

</div>

That is the intended behaviour: layers will inherit aesthetic mappings from the plot ([see docs](http://gadflyjl.org/stable/man/compositing/#Layers)).

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:

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

```

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [August 18, 2021, 2:41am UTC](https://discourse.julialang.org/t/gadfly-add-layer-caused-missing-column-error/66578/5 "2021-08-18T02:41:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [August 18, 2021, 2:55am UTC](https://discourse.julialang.org/t/gadfly-add-layer-caused-missing-column-error/66578/6 "2021-08-18T02:55:30Z")

</div>

What about this (what would you expect?)

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

```

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [August 18, 2021, 3:37am UTC](https://discourse.julialang.org/t/gadfly-add-layer-caused-missing-column-error/66578/7 "2021-08-18T03:37:34Z")

</div>

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.

```julia
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.

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

```

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

```
