[R] text labels in Trellis plot

Deepayan Sarkar deepayan.sarkar at gmail.com
Sat Jan 29 19:00:34 CET 2011

On Wed, Jan 26, 2011 at 4:24 PM, Markus Damian <m.damian at bristol.ac.uk> wrote:
> 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.

You essentially want this example from the ?xyplot help page:

  states <- data.frame(state.x77,
                          state.name = dimnames(state.x77)[[1]],
                          state.region = state.region)
     xyplot(Murder ~ Population | state.region, data = states,
            groups = state.name,
            panel = function(x, y, subscripts, groups) {
                ltext(x = x, y = y, labels = groups[subscripts], cex=1,
                      fontfamily = "HersheySans")


More information about the R-help mailing list