[R] text labels in Trellis plot

Markus Damian m.damian at bristol.ac.uk
Wed Jan 26 11:54:04 CET 2011


Dear all,

I need to generate plots in which the points of the plot are replaced
by text labels, such as "dog" and "cat". The usual way of specifying
the plotting symbol with pch works only if the labels are single
characters, as far as I know. So,

plot(runif(3), pch=c("A", "B", "C"))

will work OK, but

plot(runif(3), pch=c("dog", "cat", "mouse"))

won't - it will simply use the first character of each string as the
plotting symbol ("d", "c", and "m").

The easiest solution that I found (but there may be an easier one) is
to generate the plot but to omit the points (by specifying type="n"),
and then later adding the labels with the text() function. Hence, the
following does what I want:

a <- runif(3)
plot(a, type="n")
text(a, labels = c("dog", "cat", "mouse"))

Here is the problem -  I need to do this in a Trellis plot. So, something like

library(lattice)
stripplot(a ~ b | c)

The question is how to get the points replaced by the text labels.
Certainly the following works, and generates an empty Trellis plot:

stripplot(a ~ b | c, type="n")

but now I can't seem to figure out how to use the text() function to
add in the labels, specifically, how to implement the "by c" part.

Any help would be greatly appreciated!


M Damian



More information about the R-help mailing list