Placing a label at an arbitrary place using Gadfly

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:

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

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

Also, is using


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

use a Guide.annotation: Guides · Gadfly.jlGuide.annotation

Sample …

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))


Welcome to the Julia Community!

Gadfly works with a range of units (Geom.label example in Gadfly docs). Width/height (relative) units are from Compose.jl.

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),


In answer to your other question:
colorant"..." is an example of a non-standard string literal, from Colors.jl.

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

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))