tags:

views:

122

answers:

2

Say I make a scatterplot with thousands of points:

ggplot(head(data, n=2000), aes(length, coverage))+ 
    geom_point(alpha = 0.5, color = 'navyblue')  + coord_trans(x='log', y='log')

alt text

I want to add the labels of "the 20 or so most extreme points" (in the upper right and bottom right corners). They are easy to identify visually. But getting at them programatically seems a bit of a burden. (requiring many if statements).

Is there any way I can click on R's graphic output to obtain their precise coordinates?

Thanks, yannick

A: 

Don't know with ggplot, but with base graphics you can use identify:

plot(length,coverage,type='p')
identify(length,coverage)

Now you can use your mouse to click on points and R will show which observation they correspond to. Clicking a mouse button other than the first ends the process and identify returns the observation numbers as its value.

Jyotirmoy Bhattacharya
nope, unfortunately doesn't work with ggplot!But thanks I'll keep it in mind!
Yannick Wurm
+1  A: 

The grid analogue (the ggplot2 package as well as the Lattice package are based on grid graphics) of locator() is grid.locator().

Thanks to Deepayan Sarkar Lattice Book !

Paolo
Thanks Paolo!So that returns raw click coordinates, but doesn't necessarily match that with the coordinates of real points. So I guess I'll need to make a small wrapper function to extract those. Have a great day!yannick
Yannick Wurm
You're welcome! Glad It help! :-)
Paolo