tags:

views:

33

answers:

2

I'd like to include the glyph used in a scatter plot directly in a text annotation.

E.g., Suppose I have:

g <- g + geom_text(aes(x=14, y = 17, label="GOOD")) 

where g is a scatter plot w/ a linear trend line, labeled by "GOOD" at (14,17). Instead of "GOOD", I'd like to have "X = GOOD" where X is a plotting glyph (e.g., a hollow triangle, circle etc.) used in the scatter plot. This way I can dispense w/ the legend.

+1  A: 

You could probably do it by plotting a single point of the right type using annotate(), with the text you have above next to it as a separate annotate().

Personally, though, it sounds like you're trying to make what they used to call a camera-ready image. In that case, I'd recommend exporting to PDF and using something like Inkspace to custom-create your in-graph labels. It'll be simpler and you have a lot more flexibility, plus a WYSIWYG interface.

Harlan
Thanks Harlan and Richie - that's what I ended up doing and it worked quite well.
John Horton
+1  A: 

To expand upon Harlan's answer, here's a plot of the car's dataset

p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()

You need to add two annotations, and the trick is to set the horizontal justification of the text to make the positions line up.

x <- 4
y <- 30
p + annotate("point", x, y) + annotate("text", x, y, label = " = GOOD", hjust = 0)

The alternative is to use opts(legend.position = ??) to place your legend inside the plot, which has much the same effect.

Richie Cotton