Elizabeth
Elizabeth

Reputation: 6571

Adding arrows to plots using locator() in R

Is it possible to add arrows using locator? I gave it a try but no luck....

plot(1:3)
arrows(x0=locator(1), x1=locator(1),
       y0=locator(1), y1=locator(1), code=1)#single headed arrow

Upvotes: 3

Views: 1909

Answers (2)

Tobias
Tobias

Reputation: 11

I personally prefer not to use locator in this case. A very quickly drafted alternative could be the following. To be noted is, that you can always change the way you devise 'arrow_pos'

arrow_pos = as.data.frame(matrix(1:2,2,2))
colnames(arrow_pos)<- c("x","y")
with(arrow_pos, arrows(x0=x[1],x1=x[2], y0=y[1],y1=y[2]))

alternatively, you could also do something like this:

x_s<-c(1,2)
y_s<-c(1,2)
arrow_pos <- as.data.frame(rbind(x_s,y_s))

In which case you could also fill x_s with commands taking data directly from the vector you are plotting.

V1 = 1:3

as examples you then store these in your 'arrow_pos'

length(V1); ceiling(mean(V1)); median(V1)

Upvotes: 0

Gavin Simpson
Gavin Simpson

Reputation: 174803

If you look at what locator(1) returns you'll see why this isn't even close to working:

> locator(1)
$x
[1] 1.365533

$y
[1] 2.25695

So you were passing a list of length two to each argument. I would probably (though this won't be reproducible so I wouldn't really do it in anger in any analysis) approach this via:

> locs <- locator(2)
> locs
$x
[1] 1.265845 1.587567

$y
[1] 2.604642 2.267028

The add the arrow using the data saved in locs:

with(locs, arrows(x0=x[1], x1=x[2], y0=y[1], y1= y[2], code=1))

Upvotes: 6

Related Questions