Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I create a labelled scatter plot?

I would like to draw a labelled scatterplot, like shown below, in Gadfly.

Like this example (Source: http://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/images/renda.png)

How can I do this?

The scatter plot is easy:

using Gadfly
X = [1, 2, 2, 3, 3, 3, 4]
Y = [4, 4, 7, 7, 9, 1, 8]
Labels = ["bill", "susan", "megan", "eric", "fran", "alex", "fred"]

plot(x=X, y=Y)

On Option is to use colours, but that isn't so great, cos the legend becomes huge (particularly in less trivial examples than this).

plot(x=X,y=Y, color=Labels)

colors

like image 241
Lyndon White Avatar asked Feb 23 '15 01:02

Lyndon White


2 Answers

While @Oxinabox's answer works, the Gadfly way would be to use Geom.label, e.g.

using Gadfly
X = [1, 2, 2, 3, 3, 3, 4]
Y = [4, 4, 7, 7, 9, 1, 8]
Labels = ["bill", "susan", "megan", "eric", "fran", "alex", "fred"]

plot(x=X, y=Y, label=Labels, Geom.point, Geom.label)

Gadfly Plot

There are a lot of benefits to this, including smart label placement to avoid label overlap, or you can pick from some easy-to-use rules like :centered or below. Plus it'll pick up font/font size from the theme.

like image 171
IainDunning Avatar answered Oct 05 '22 22:10

IainDunning


You can do this with Compose annotations.

The Gadfly documentation on that is here. You might have missed its true power from the simple example shown.

Using Compose
plot(x=X, y=Y, Guide.annotation(compose(context(), text(X, Y, Labels))))

With labels

like image 31
Lyndon White Avatar answered Oct 05 '22 23:10

Lyndon White