# VegaLite add line manually?

**URL:** https://discourse.julialang.org/t/vegalite-add-line-manually/36033
**Category:** General Usage
**Tags:** plotting
**Created:** [March 16, 2020, 7:55am UTC](https://discourse.julialang.org/t/vegalite-add-line-manually/36033 "2020-03-16T07:55:49Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 16, 2020, 7:55am UTC](https://discourse.julialang.org/t/vegalite-add-line-manually/36033/1 "2020-03-16T07:55:49Z")

</div>

```julia
dfc_single |> @filter(_.gender ∈ ["Male", "Female"]) |>

@vlplot(
    :bar,
    width = 700, 
    y={
        "count()",
        axis={title="Gender fraction"},
        stack=:normalize
    },
    x="state",
    color = :gender,
)

```

produces:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/b/0babbcabe9ba2f473c3cb01e3a53c0f7d0292432.png)

which is pretty nice, now, I want to just add a horizontal line in ad-hoc fashion to denote total mean fraction, it is possible in vegalite (probably with `:rule`?)

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [March 16, 2020, 8:51am UTC](https://discourse.julialang.org/t/vegalite-add-line-manually/36033/2 "2020-03-16T08:51:05Z")

</div>

Maybe:

```julia
+
@vlplot(
    mark={
        :rule,
        color="red"
    },
    x={"state:q", aggregate="average"}
)

```

But this may produce two lines, one for male and one for female.

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [March 16, 2020, 5:56pm UTC](https://discourse.julialang.org/t/vegalite-add-line-manually/36033/3 "2020-03-16T17:56:06Z")

</div>

Maybe like this:

```julia
@vlplot() +
    @vlplot(
        :bar,
        width = 700, 
        y={
            "count()",
            axis={title="Gender fraction"},
            stack=:normalize
        },
        x="state",
        color = :gender,
    ) +
    @vlplot(
        mark={:rule, color=:red},
        data={values=[{ref=0.4}]},
        y="ref:q"
    )

```

I think this will hopefully get a little easier in the future, see [here](https://github.com/vega/vega-lite/issues/1601).

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 16, 2020, 8:22pm UTC](https://discourse.julialang.org/t/vegalite-add-line-manually/36033/4 "2020-03-16T20:22:47Z")

</div>

@davidanthoff Do you know how to make it work with `column` and `row`?

```julia
dataset("cars") |>
@vlplot() +
@vlplot(:point, x = :Horsepower, y = :Miles_per_Gallon, column = :Origin,) +
@vlplot(mark = {:rule, color = :red}, data = {values = [{ref = 10}]}, y = "ref:q")

```

ignores `column`: [Vega Editor](https://vega.github.io/editor/#/url/vega-lite/N4KABGBEA2CGCeBTATpAXGA2uCZS90kQDsBjAewBMBLYgc3TxwIkgugFcBbYx4SAGbVE0So0gB5ZNTq1IAXwA0zFpAAefQcNHiAEuWQBnRAAdyAdxQLlLQvE1CRYjJACy1aIkMB9Eym8A4rDQ0OS88ioQSpFQXLDIANbiZrQALpAq0QT4qiQUNPSa9hj8jjouyIgCkIpQqfB+4gCOHLDEqdSpsB0AbogKWaqU3bCaPcEcXoyY-JXVGACMAAzyALqDBJBxiZrsBuKVYrWQ9Y0VHJ4KmTirNlDDXZocyNDiD7AA9KTxhgB0AFaGMJXeRAA)

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [March 16, 2020, 8:47pm UTC](https://discourse.julialang.org/t/vegalite-add-line-manually/36033/5 "2020-03-16T20:47:36Z")

</div>

@tkf I think you can generally not layer a spec that has a facet (which kind of makes sense). So what you need to do instead is put a facet operator into a top-level spec, which then has a child spec which is a layered spec.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 16, 2020, 9:05pm UTC](https://discourse.julialang.org/t/vegalite-add-line-manually/36033/6 "2020-03-16T21:05:57Z")

</div>

Thanks! So this [seems to work](https://vega.github.io/editor/#/url/vega-lite/N4KABGBEDOAOCmBjSAuMoIUgGwIYE94AnVMAbXEwgys0ngDtEB7AEwEsGBzUm2ugB69IAM3bxsrUpAASzItHixmAd2KQAvgBpK-LPmFiJUtJACy7bPGgB9BERsBxXNmzMGm3bW1eqkALa4RADW0sqcAC6QvmA+-Hy09ExsnDxowJAG6aLiktJE8CKQWlAR+AjSAI4ArrgMEewRuA0AbvCacXqQrM24wi0u1dakZBkFRWgAjAAMGgC6nfwBQaHZLG4kpgVSJZBlFVvVVp78Gl5zlJ3dvcLVRNjSPU0A9IhB0AB0AFbQ7po6WBEuEQ8Ci2SIqkMuRMUAA8kR2FxOMVSuV2qYGMx-JwXB0QBogA):

```julia
@vlplot(
    data = dataset("cars"),
    facet = {row = {field = :Origin, type = :nominal}},
    spec = {
        layer = [
            {
                mark = :point,
                encoding = {x = {field = :Horsepower}, y = {field = :Miles_per_Gallon}},
            },
            {
                mark = {type = :rule, color = :red},
                data = {values = [{ref = 10}]},
                encoding = {y = {field = :ref, type = :quantitative}},
            },
        ],
    }
)

```

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [March 16, 2020, 9:19pm UTC](https://discourse.julialang.org/t/vegalite-add-line-manually/36033/7 "2020-03-16T21:19:45Z")

</div>

Yes! Here is a version that uses some more shortcuts:

```julia
dataset("cars") |>
@vlplot(facet = {row = {field = :Origin, type = :nominal}}) + 
    (
        @vlplot() +
        @vlplot(:point, :Horsepower, :Miles_per_Gallon) +
        @vlplot(mark={:rule, color=:red}, data = {values = [{ref = 10}]}, y="ref:q")
    )

```

I don’t fully understand right now why this extra `@vlplot()` is needed as the first element for the layered sub-spec, but hey, it works 🙂

I think there are really two things that we could do on the Julia side to make this easier: a) add support for the shorthand parsing to the `row` field, so that `facet={row="Origin:n"}` works. b) sort out the composition story… I’ll try to find time to look over some of the proposals we had on that.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 16, 2020, 9:38pm UTC](https://discourse.julialang.org/t/vegalite-add-line-manually/36033/8 "2020-03-16T21:38:32Z")

</div>

Yeah, I thought there might be a way to write it shortly. I agree it’d be great to consolidate Julia side shortcuts more.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 16, 2020, 9:39pm UTC](https://discourse.julialang.org/t/vegalite-add-line-manually/36033/9 "2020-03-16T21:39:32Z")

</div>

thx for the discussion and examples, something like this would be useful: [horizontal and vertical lines · Issue #17 · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/issues/17)

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [March 16, 2020, 10:40pm UTC](https://discourse.julialang.org/t/vegalite-add-line-manually/36033/10 "2020-03-16T22:40:11Z")

</div>

I think once they implement this properly in the underlying Vega-Lite library, it will look like this: `@vlplot(:rule, y={datum=20})`. Until then we’ll have to live with the `@vlplot(:rule, data={values=[ref=20}]}, y="ref:q")` workaround.
