1

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?

miguelmorin
  • 5,025
  • 4
  • 29
  • 64

1 Answers1

2

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)

Community
  • 1
  • 1
kabanus
  • 24,623
  • 6
  • 41
  • 74
  • Yes, this works in normal scale: `using Gadfly, Compose; plot(x = rand(10), y = rand(10), Guide.annotation(compose(context(), text(0.8, 0.8, "Some text"))))`. But it fails in log-scale: `using Gadfly, Compose; plot(x = rand(10), y = rand(10), Scale.y_log10, Guide.annotation(compose(context(), text(0.8, 0.8, "Some text"))))`. It looks like a bug. Do you have a workaround? – miguelmorin Jun 19 '18 at 10:14
  • @mmorin Can you specify what is is the bug? If it's too complex to work around t may be worth accepting this answer and asking another question on SO about that. – kabanus Jun 19 '18 at 10:42
  • I posted this [on GitHub](https://github.com/GiovineItalia/Gadfly.jl/issues/1165) and the bug is that "Guide.annotation does not auto-adjust to Scale.y_log10" so the solution is "to use log10(0.8) in text()". Yes, I accepted the answer. – miguelmorin Jun 19 '18 at 11:26
  • @mmorin Ahh, I see the bug. Awesome you get a workaround so quickly. – kabanus Jun 19 '18 at 11:27