# Placing a label at an arbitrary place using Gadfly

**URL:** <https://discourse.julialang.org/t/placing-a-label-at-an-arbitrary-place-using-gadfly/66528>\
**Category:** New to Julia\
**Tags:** question, gadfly\
**Created:** [August 17, 2021, 2:12am UTC](https://discourse.julialang.org/t/placing-a-label-at-an-arbitrary-place-using-gadfly/66528 "2021-08-17T02:12:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![devin](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@devin](https://discourse.julialang.org/u/devin)\
**Post date:** [August 17, 2021, 2:12am UTC](https://discourse.julialang.org/t/placing-a-label-at-an-arbitrary-place-using-gadfly/66528/1 "2021-08-17T02:12:21Z")

</div>

I’m trying to place a label at any arbitrary (x,y) location in my plot, but I can’t quite figure it out.

Here’s my code:

```julia
t = 0.001:0.001:2
f = 25.0
y = cos.(2.0 * π * f .* t);
p = plot(x=t[1:100], y=y[1:100], Geom.line)
push!(p, layer(x=[0, .1], y=[0.5, 0.5], Geom.line, color=[colorant"red"]))

```

I thought I do something like this without success

```julia
push!(p, layer(x=0.05, y=0.5, Geom.label("my label")))

```

Also, is using

```julia
color=[colorant"red"]

```

the easiest way to set the color? What is “colorant” doing?

---

<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 17, 2021, 2:46am UTC](https://discourse.julialang.org/t/placing-a-label-at-an-arbitrary-place-using-gadfly/66528/2 "2021-08-17T02:46:56Z")

</div>

use a `Guide.annotation`: [Guides · Gadfly.jl](http://gadflyjl.org/stable/gallery/guides/#%5BGuide.annotation%5D(@ref))

---

<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 17, 2021, 3:54am UTC](https://discourse.julialang.org/t/placing-a-label-at-an-arbitrary-place-using-gadfly/66528/3 "2021-08-17T03:54:47Z")

</div>

Sample …

```julia
using Gadfly
using Compose

t = 0.001:0.001:2
f = 25.0
y = cos.(2.0 * π * f .* t);
p = plot(x=t[1:100], y=y[1:100], Geom.line, Theme(default_color=colorant"red"))
txt = compose(context(), Compose.text(0.05, 0.5, "my label"), stroke("blue"))
push!(p, Guide.annotation(txt))

p

```

---

<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 17, 2021, 9:39pm UTC](https://discourse.julialang.org/t/placing-a-label-at-an-arbitrary-place-using-gadfly/66528/4 "2021-08-17T21:39:23Z")

</div>

Welcome to the Julia Community!

Gadfly works with a range of units ([Geom.label example in Gadfly docs](http://gadflyjl.org/stable/gallery/geometries/#%5BGeom.label%5D(@ref))). Width/height (relative) units are from [Compose.jl](https://giovineitalia.github.io/Compose.jl/latest/).

```julia
import Gadfly: w, h

# just for brevity: 
geomlbl = Geom.label(position=:centered)

p1 = plot(x=[0., 1], y=[0.,1], Geom.blank,
    layer(x=[0.6w], y=[0.7h], label=["Relative\n units"], geomlbl, Geom.point),
    layer(x=[0.1], y=[0.5], label=["Data units"], geomlbl, Geom.point),
# Looks like there is a bug for absolute units :(
# layer(x=[16mm], y=[17mm], label=["absolute\n units"], glbl, Geom.point),
 )

```

![text_and_units](https://global.discourse-cdn.com/julialang/original/3X/8/5/853edde32c527b30d38b80afdedbeb77a57f1af1.png)

In answer to your other question:  
`colorant"..."` is an example of a [non-standard string literal](https://docs.julialang.org/en/v1/manual/strings/#non-standard-string-literals), from [Colors.jl](http://juliagraphics.github.io/Colors.jl/stable/constructionandconversion/#Color-Parsing).

Another way to set color in Gadfly is to use a [“Group label”] e.g.

```julia
y1 = [0.1, 0.26, missing, 0.5, 0.4, NaN, 0.48, 0.58, 0.83]

p2 = plot(x=1:9, y=y1, Geom.line, Geom.point, color=["Item 1"], 
        linestyle=[:dash], size=[3pt],
    layer(x=:1:10, y=rand(10), Geom.line, Geom.point, 
        color=["Item 2"], size=[5pt], shape=[Shape.square]),
    layer(x=:1:10, y=rand(10), color=[colorant"hotpink"], 
        linestyle=[[8pt, 3pt, 2pt, 3pt]], Geom.line))

```

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