# How to make this plot in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065>\
**Category:** Visualization\
**Tags:** gadfly, plots, statsplots, algebraofgraphics, vegalite\
**Created:** [January 23, 2022, 1:10pm UTC](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065 "2022-01-23T13:10:07Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [September 29, 2022, 7:49pm UTC](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065/21 "2022-09-29T19:49:21Z")

</div>

Ahh, now I got it:

```julia
using AlgebraOfGraphics, GLMakie
using Random, DataFrames
Random.seed!(123)
d = DataFrame(name = repeat(["A","B","C","D","E","F"], inner=4), 
      time=repeat([0,1,3,6], outer=6), value = rand(24));
plt = data(d)*( 
       (visual(Scatter) + visual(Lines)) * mapping(:time, :value, color = :name) + 
        visual(Makie.Text) * mapping(:time, :value, color = :name, text = :name => verbatim) 
       );
AlgebraOfGraphics.draw(plt)

```

 ![billede](https://global.discourse-cdn.com/julialang/original/3X/3/8/3801435bcbbe530836ecb4ab4ba1007f0fb3f99a.png)

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [October 2, 2022, 10:26am UTC](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065/22 "2022-10-02T10:26:25Z")

</div>

Here’s an example using [`Bokeh.jl`](https://github.com/cjdoris/Bokeh.jl). It’s quite long, because you need to be explicit about a lot of things, but I’ve been thinking about an AoG-style wrapper for it.

```julia
using DataFrames, Random, Bokeh
# OP's data
Random.seed!(123)
d = DataFrame(name = repeat(["A","B","C","D","E","F"], inner=4), 
      time=repeat([0,1,3,6], outer=6), value = rand(24));
# (optional) displays plots in the browser - omit if you are in a notebook
Bokeh.settings!(display=:browser)
# make the figure
fig = figure()
# set the axis labels
fig.x_axis.axis_label = "time"
fig.y_axis.axis_label = "value"
# (optional) by default the legend is placed inside the plot
plot!(fig, Legend; location="right")
# defines the mapping from names to colors
color = factor_cmap("name", "Set2_8", sort(unique(d.name)))
# defines the data source
source = ColumnDataSource(data=d)
# add scatter plot
plot!(fig, Scatter; x="time", y="value", source, color, size=10, legend_field="name")
# add text
plot!(fig, Bokeh.Text; x="time", y="value", text="name", source, color, text_baseline="middle", x_offset=10)
# add lines - note bokeh will not group lines for you, so you must
# group the data yourself and either make multiple `Line` plots or
# one `MultiLine` plot
w = combine(groupby(d, :name), d->(time=[d.time], value=[d.value]))
plot!(fig, MultiLine; xs="time", ys="value", source=w, color)
# show the result
display(fig)

```

Edit: here is the result.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/6/46c644d5fdbb5f025d111d63d83f2446a2e7cc98.png)

---

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [October 2, 2022, 10:51am UTC](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065/23 "2022-10-02T10:51:49Z")

</div>

Thank you @cjdoris  
I’ve heard a lot of good things about Bokeh in python, but never tried it. Great that we also have it in Julia.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [October 2, 2022, 11:46am UTC](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065/24 "2022-10-02T11:46:27Z")

</div>

I just dug out my AoG-style wrapper for Bokeh: [GitHub - cjdoris/Algebrokeh.jl: Statistical plotting with Bokeh](https://github.com/cjdoris/Algebrokeh.jl).

The gist is:

```julia
# You can't use Line glyph with color mapping, so need to explicitly
# group and use the MultiLine glyph (currently).
gd = combine(groupby(d, :name), d->(time=[d.time], value=[d.value]))
# Should look familiar to AoG users!
((data(d) * (visual(Scatter) + visual(Bokeh.Text)) + data(gd) * visual(MultiLine))
 * mapping(x="time", y="value", color="name", text="name")
) |> draw |> display

```

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

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 2, 2022, 11:57am UTC](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065/25 "2022-10-02T11:57:14Z")

</div>

> [@cjdoris](#):
>
> I just dug out my AoG-style wrapper for Bokeh

Did you write this 300-LOC package in less then 1h?

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [October 2, 2022, 11:59am UTC](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065/26 "2022-10-02T11:59:49Z")

</div>

Lol no I wrote the code months ago and this post prompted me to put it on GitHub 🙂

---

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [October 2, 2022, 3:18pm UTC](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065/27 "2022-10-02T15:18:14Z")

</div>

> AoG-style wrapper for Bokeh  
> Wow! This is looking great!

---

<div class="post-metadata">

**Author:** ![jerlich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerlich/32/5205_2.png) [@jerlich](https://discourse.julialang.org/u/jerlich)\
**Post date:** [October 13, 2022, 8:03am UTC](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065/28 "2022-10-13T08:03:30Z")

</div>

It would be great for this example to be in the AofG gallery.

---

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [October 15, 2022, 1:09pm UTC](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065/29 "2022-10-15T13:09:50Z")

</div>

The `Makie.Text` part is covered by this example: [https://aog.makie.org/stable/gallery/gallery/scales/prescaled\_data/#Pre-scaled-data](https://aog.makie.org/stable/gallery/gallery/scales/prescaled_data/#Pre-scaled-data)  
Not sure how typical this plot actually is. What would be a good name for it?

---

<div class="post-metadata">

**Author:** ![fabgrei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabgrei/32/4214_2.png) [@fabgrei](https://discourse.julialang.org/u/fabgrei)\
**Post date:** [October 17, 2022, 8:15pm UTC](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065/30 "2022-10-17T20:15:54Z")

</div>

You can make your code a little more concise by using `visual(ScatterLines)`. You can also use the distributive property of `mapping`s (that’s a matter of taste, though)

```julia
	# Version 1
	plt = data(d) * 
		mapping(:time, :value, color = :name) * (
			visual(ScatterLines) + 
			visual(Makie.Text) * mapping(text = :name => verbatim)
		)

	# Version 2 (works because ScatterLines ignores unused kwarg `text`)
	plt = data(d) * 
		mapping(:time, :value, color = :name, text = :name => verbatim) *
		(visual(ScatterLines) + visual(Makie.Text))

```

[Previous page](https://discourse.julialang.org/t/how-to-make-this-plot-in-julia/75065.md?page=1)
