[R] Annotating trellis graphics

Deepayan Sarkar deepayan.sarkar at gmail.com
Thu Jun 14 20:45:35 CEST 2007


On 6/13/07, Alan S Barnett <asb at mail.nih.gov> wrote:
> I'm using xyplot to generate a trellis plot with each panel containing a
> scatterplot and a best fit line.  Is it possible to write the slope of
> the best fit line in each panel?

Sure. The only question is, where (inside the panel) do you want it written?
Here are a couple of possibilities:

## writes the slope at a location that happens to be empty in both
## panels in this example

xyplot(len ~ dose | supp, ToothGrowth,
       panel = function(x, y, ...) {
           panel.xyplot(x, y, ...)
           fm <- lm(y ~ x)
           panel.abline(reg = fm)
           slope <- round(coef(fm)[2], 3)
           panel.text(1.5, 5, lab = slope)
       })


## needs the user to click on a suitable position for each panel

library(grid)

xyplot(len ~ dose | supp, ToothGrowth,
       panel = function(x, y, ...) {
           panel.xyplot(x, y, ...)
           fm <- lm(y ~ x)
           panel.abline(reg = fm)
           slope <- round(coef(fm)[2], 3)
           message("Click on desired location")
           panel.text(grid.locator("native"), lab = slope)
       })

-Deepayan



More information about the R-help mailing list