Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R arrowed labelling of data points on a plot

Tags:

plot

r

I am looking to label data points with indices -- to identify the index number easily by visual examination.

So for instance,

x<-ts.plot(rnorm(10,0,1)) # would like to visually identify the data point indices easily through arrow labelling

Of course, if there's a better way of achieving this, please suggest

like image 931
Aditya Sihag Avatar asked Feb 15 '13 11:02

Aditya Sihag


3 Answers

You can use arrows function:

set.seed(1); ts.plot(x <-rnorm(10,0,1), ylim=c(-1.6,1.6))  # some random data
arrows(x0=1:length(x), y0=0, y1=x, code=2, col=2, length=.1) # adding arrows
text(x=1:10, y=x+.1, 0, labels=round(x,2), cex=0.65) # adding text
abline(h=0) # adding a horizontal line at y=0

enter image description here

like image 199
Jilber Urbina Avatar answered Oct 18 '22 11:10

Jilber Urbina


Use my.symbols from package TeachingDemos to get arrows pointing to the locations you want:

require(TeachingDemos)
d <- rnorm(10,0,1)
plot(d, type="l", ylim=c(min(d)-1, max(d)+1))
my.symbols(x=1:10, y=d, ms.arrows, angle=pi/2, add=T, symb.plots=TRUE, adj=1.5)

enter image description here

like image 40
Geek On Acid Avatar answered Oct 18 '22 11:10

Geek On Acid


You can use text() for this

n <- 10
d <- rnorm(n)
plot(d, type="l", ylim=c(min(d)-1, max(d)+1))
text(1:n, d+par("cxy")[2]/2,col=2) # Upside
text(1:n, d-par("cxy")[2]/2,col=3) # Downside
like image 31
Rcoster Avatar answered Oct 18 '22 11:10

Rcoster