# Gadfly Layer Order (Geom.point and Geom.line)

**URL:** https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798
**Category:** Visualization
**Tags:** plotting
**Created:** [April 18, 2020, 3:41am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798 "2020-04-18T03:41:01Z")
**Posts on this page:** 20
**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: [April 18, 2020, 3:41am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/1 "2020-04-18T03:41:01Z")

</div>

In Gadfly, I have multiple layers (expensive, I know) - a combination of Geom.point and Geom.line (from function). I tried ([Compositing · Gadfly.jl](https://gadflyjl.org/stable/man/compositing/#)) to arrange/ order such that the line is on top, but unsuccessful. Any idea? Thanks.  
 ![Gadfly_layers_order](https://global.discourse-cdn.com/julialang/original/3X/f/f/ff65fac0a615d4591b3a4e00039d5f31b3e6985f.png)

---

<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: [April 18, 2020, 5:57am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/2 "2020-04-18T05:57:38Z")

</div>

```julia
using Gadfly, RDatasets
iris = dataset("datasets", "iris")
fs = [x->4.5*(1.0.-exp.(-1.5*(x-4))), x->0.3x+1, x->0.3x+1.1]
cs = unique(iris.Species)

plot(iris, 
  layer(x=:SepalLength, y=:SepalWidth, color=:Species),
  layer(y=fs, xmin=[4.4], xmax=[8], Stat.func, Geom.line, color=cs, order=2)
    )

```

![iris](https://global.discourse-cdn.com/julialang/original/3X/9/c/9c91ab9ccccd3df018e3c4b543d9c693bd38e8bb.png)  
There’s another example of `Stat.func` in the [Gadfly docs](http://gadflyjl.org/stable/gallery/statistics/#%5BStat.func%5D(@ref)-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: [April 18, 2020, 6:24am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/3 "2020-04-18T06:24:28Z")

</div>

Tested your code - it is working. When I modified your code to match mine, it doesn’t work. Do I have to follow exactly yours?

```julia
using Gadfly, RDatasets
iris = dataset("datasets", "iris")
ft(x) = 4.5*(1.0.-exp.(-1.5*(x-4)))
cs = unique(iris.Species)

plot( 
  layer(iris, x=:SepalLength, y=:SepalWidth, color=:Species, Geom.point),
  layer(ft, 4, 8, Geom.line(order=2),
  )

```

![Gadfly_layers_order2](https://global.discourse-cdn.com/julialang/original/3X/7/2/7298479d0cd56a722369456846ef8cb8db354938.png)

---

<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: [April 18, 2020, 7:23am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/4 "2020-04-18T07:23:30Z")

</div>

1. This works:

```julia
plot( 
  layer(ft, 4, 8, Geom.line),
  layer(iris, x=:SepalLength, y=:SepalWidth, color=:Species)
  )

```

1. This should work:

```julia
layer(ft, 4, 8, Geom.line, order=2)

```

but there is currently a bug, which I’ll put on the to do list.

1. About this:

```julia
layer(ft, 4, 8, Geom.line(order=2))

```

`Geoms` are like sublayers, and a layer can contain several `Geoms`.  
So `Geom.line(order=2)` controls the sublayer order, but not the layer order.

---

<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: [April 18, 2020, 7:24am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/5 "2020-04-18T07:24:16Z")

</div>

@Mattriks How to change each line color (your post #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: [April 18, 2020, 7:25am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/6 "2020-04-18T07:25:19Z")

</div>

> [@Mattriks](#):
>
> `Geoms` are like sublayers, and a layer can contain several `Geoms` .  
> So `Geom.line(order=2)` controls the sublayer order, but not the layer order.

Confirmed my suspicion.

---

<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: [April 18, 2020, 7:29am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/7 "2020-04-18T07:29:43Z")

</div>

Do you mean line color separately from point color? And how many line color groups vs point color groups? An example would help.

---

<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: [April 18, 2020, 8:07am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/8 "2020-04-18T08:07:59Z")

</div>

My objective is change the default line color (not to follow Geom.point color), since I got to go with your method, until the bug is fixed. For example, assign red color for function1, orange color for function2 and so on.

---

<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: [April 18, 2020, 9:10am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/9 "2020-04-18T09:10:24Z")

</div>

Here’s line color different to point color:

```julia
using Colors
cs = ["fn1", "0.3x+1", "0.3x+1.1"]
dpalette(n::Int) = LCHuv.(65, 100, 15:(360/n):374)
palette2 = ["deepskyblue","yellow3", "hotpink", "blue", "orange", "orchid"]

p = plot(iris,
    layer(x=:SepalLength, y=:SepalWidth, color=:Species),
    layer(y=fs, xmin=[4.4], xmax=[8], Stat.func, Geom.line, color=cs, order=2),
# Scale.color_discrete_manual(dpalette(6)...) 
   Scale.color_discrete_manual(palette2...), 
    Guide.colorkey(title="Iris")
    )

```

---

<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: [April 18, 2020, 9:49am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/10 "2020-04-18T09:49:08Z")

</div>

Tried your code. Removed dependency on Colors and simplified a bit - still works. Basically using `Scale.color_discrete_manual` to control the color. Excellent.

```julia
#using Colors
#cs = ["fn1", "0.3x+1", "0.3x+1.1"]
#dpalette(n::Int) = LCHuv.(65, 100, 15:(360/n):374)
palette2 = ["deepskyblue","yellow3", "hotpink", "blue", "orange", "orchid"]

p = plot(iris,
    layer(x=:SepalLength, y=:SepalWidth, color=:Species),
    #layer(y=fs, xmin=[4.4], xmax=[8], Stat.func, Geom.line, color=cs, order=2),
    layer(y=fs, xmin=[4.4], xmax=[8], Stat.func, Geom.line, order=2),
# Scale.color_discrete_manual(dpalette(6)...) 
   Scale.color_discrete_manual(palette2...), 
    Guide.colorkey(title="Iris")
    )

```

---

<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: [April 18, 2020, 10:25am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/11 "2020-04-18T10:25:27Z")

</div>

In your example, it’s curious that the lines get color-mapped, because `color` is not specified in that layer. Consider this example (& try removing the `color=cs`):

```julia
fs = [x->0.3x+1, x->0.3x+1.1]
cs = ["versicolor", "virginica"] # same os point color 
# cs = ["0.3x+1","0.3x+1.1"] # different to point color
p = plot(iris,
    layer(x=:SepalLength, y=:SepalWidth, color=:Species),
    layer(y=fs, xmin=[4.4], xmax=[8], Stat.func, Geom.line, color=cs, order=2),
    )

```

---

<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: [April 18, 2020, 11:21am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/12 "2020-04-18T11:21:08Z")

</div>

Sorry, I don’t think it work - after testing with various changes and restarted Julia. The color did not change, until `color=cs` present. Without this line, the color is auto-mapped to Goem.point. Just curious, what does this line `cs = ["fn1", "0.3x+1", "0.3x+1.1"]` mean, particularly `fn1`?

---

<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: [April 18, 2020, 11:50am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/13 "2020-04-18T11:50:55Z")

</div>

> [@Mattriks](#):
>
> 1. This should work:
> 
> ```julia
> layer(ft, 4, 8, Geom.line, order=2)
> 
> ```

This doesn’t work for me. Error below.

```julia
ERROR: LoadError: MethodError: no method matching layer(::var"#fn_m#11"{Array{Any,1}}, ::Float64, ::Float64, ::Type{Gadfly.Geom.LineGeometry}, ::Theme; order=2)
Closest candidates are:
  layer(::Function, ::Number, ::Number, ::Union{Function, Gadfly.Element, Theme, Type}...) at

```

---

<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: [April 18, 2020, 12:04pm UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/14 "2020-04-18T12:04:00Z")

</div>

I’ve started a PR to fix that issue ([Mapping for layers with functions by Mattriks · Pull Request #1420 · GiovineItalia/Gadfly.jl · GitHub](https://github.com/GiovineItalia/Gadfly.jl/pull/1420)).

`cs = ["fn1", "0.3x+1", "0.3x+1.1"]` is just a set of labels (you can change). If you want the line colors to match the point colors, than use matching point and line labels. If you want different colors, then use different labels.

---

<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: [April 18, 2020, 12:12pm UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/15 "2020-04-18T12:12:13Z")

</div>

OK. My plots are rather complicated, some up to 12 layers: 6 Geom.point and 6 Geom.line{ function}. As long as I can get the lines on top, that meet my purpose. Thanks for the PR. Hope it got fix and rollout soon.

---

<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: [April 19, 2020, 2:24am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/16 "2020-04-19T02:24:15Z")

</div>

It seems to work. Tried again with code below, after fresh Julia restart and clear Plot view.

```julia
using Gadfly, RDatasets
iris = dataset("datasets", "iris")

fs = [x->4.5*(1.0.-exp.(-1.5*(x-4))), x->0.3x+1, x->0.3x+1.1]
cs = ["a", "b", "c"]
dpalette(n::Int) = LCHuv.(65, 100, 15:(360/n):374)
#palette2 = ["deepskyblue","yellow3", "hotpink", "blue", "red", "green"]
palette2 = ["blue", "red", "green"]

plot(
    layer(iris, x=:SepalLength, y=:SepalWidth), #, color=:Species),
    layer(y=fs, xmin=[4.4], xmax=[8], Stat.func, Geom.line, order=2),
# Scale.color_discrete_manual(dpalette(6)...)
    Scale.color_discrete_manual(palette2...),
    )

```

![Gadfly_layers_order4](https://global.discourse-cdn.com/julialang/original/3X/d/a/dadab71e928bfc16b03eb65eea6833b2577ca9de.png)

---

<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: [April 19, 2020, 5:07am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/17 "2020-04-19T05:07:52Z")

</div>

Solved. As the plots are created over loop, got to make conditional (if-else) palette based on color count. Sample below.

 ![Gadfly_layers_order5](https://global.discourse-cdn.com/julialang/original/3X/a/f/afcea659880f8350cd63a38959dcfd24df05d11c.png)

---

<div class="post-metadata">

### Author: ![Balinus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/balinus/32/243_2.png) [@Balinus](https://discourse.julialang.org/u/Balinus)
#### Post date: [April 19, 2020, 6:07pm UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/18 "2020-04-19T18:07:29Z")

</div>

Really nice figure! I like it. Can you also provide the code? Might be useful for people. Thanks!

---

<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: [April 20, 2020, 8:10am UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/19 "2020-04-20T08:10:47Z")

</div>

@Balinus. Thanks. Actually, there is nothing fancy about the plot - just stack of plot layers. The one shown here comprises of 12 layers - six Geom.point and six Geom.line (function)). However, this is an expensive exercise for Gadfly. Luckily the data points are not that too many. The gist of this post is there is an issue with layer ordering for plot with mixed Geom.point and Geom.line, i.e. Geom.line got buried under Geom.point. @Mattriks showed a workaround, while waiting for the bug to be fixed.

The star2 are binning averages, with different color for different averaging method (Arithmetic, Geometric, Harmonic, etc.). The lines are curve fit bin averages.

---

<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: [April 20, 2020, 11:17pm UTC](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798/20 "2020-04-20T23:17:46Z")

</div>

I merged the PR into master. You can use it now by doing

```julia
]add Gadfly#master

```

The plot from my first post above can be done as:

```julia
plot(iris,
    layer(x=:SepalLength, y=:SepalWidth, color=:Species),
    layer(fs, 4.4, 8, color=cs, order=2)
  )

```

[Next page](https://discourse.julialang.org/t/gadfly-layer-order-geom-point-and-geom-line/37798.md?page=2)
