R: Lattice: adding text

From MathWiki

Plotting text with lattice

Through the panel function

cat("
            y  x   id sex
1   1.2831889 NA <NA>   f
2   0.7393321  1    a   m
3   2.6627737  2    a   m
4   4.2573083  3    b   f
5   6.5090686  4    b   f
6   6.4244359  5    c   m
7   6.5487169  6    c   m
8   6.4759615  7    d   m
9   8.7849752  8    d   f
10 10.0636648  9    e   f
11 12.3242820 10    e   f
12 11.7487077 11    f   f
13 14.0798296 12    f   f
14 15.4429176 13    g   f
15 14.4519283 14    g   f
16 17.5143871 15    h   f
17 14.8262911 16    h   m
18 17.6247511 17    i   f
19 18.3956455 18    i   m
20 19.5140557 19    j   f
21         NA 20    j   f
22         NA NA <NA>   m
", file = "tmp.txt")

dz <- read.table( 'tmp.txt', header=T)
dz


xyplot(y ~ x |sex, dz, 
  panel = function(x,y,subscripts,...) {
      panel.points(x,y,...,type = 'b')
      panel.text(x,y,...,labels = dz$id[subscripts])
  }
)


xyplot( y ~ x , dz, groups = sex, auto.key = T,
  panel = function( x,y, subscripts,...) panel.superpose( x, y, subscripts ,...),
  panel.groups = function( x,y , subscripts,type,...){
      panel.xyplot( x, y,..., type ='b')
      panel.text( x, y, labels = dz$id[subscripts])
      print(subscripts)
      print( dz$id[subscripts])
  }
)

With trellis.focus and panel.identify

See