plotjuliagadfly

Gadfly (Julia): how to add text annotation to a plot?


In Stata I can add text to a plot at given coordinates, e.g.:

clear

set obs 10

gen x = rnormal()
gen y = rnormal()

twoway scatter y x, text(0.8 0.8 "Some text")

The result is "Some text" at coordinates (0.8, 0.8):

Scatter plot with text annotation inside plot

I want to add a similar annotation in Julia with Gadfly. I found Guide.annotation that can add layers of graphs and I could not figure out how to apply it to text instead of shapes. The documentation mentions at the top:

Overlay a plot with an arbitrary Compose graphic.

but the Compose link shows a website in Chinese.

How can I add a text label (or caption, or annotation) with Gadfly?


Solution

  • You can check the guide on Compose in julia. In the example in your link they use Circle, but you just as easily can use:

    text(x, y, value)
    

    or using the linked example code:

    Pkg.add.(["Gadfly", "Compose"])
    using Gadfly, Compose;
    plot(x = rand(10),
         y = rand(10),
         Guide.annotation(compose(context(), text(0.8, 0.8, "Some text"))))
    

    in the link I provided they redirect to a source file for the comprehensive list:

    These are basic constructors for the in-built forms - see src/form.jl for more constructors.

    • polygon(points)

    • rectangle(x0, y0, width, height)

    • circle(x, y, r)

    • ellipse(x, y, x_radius, y_radius)

    • text(x, y, value)

    • line(points)

    • curve(anchor0, ctrl0, ctrl1, anchor1)

    • bitmap(mime, data, x0, y0, width, height)