# [R] adding text to the top corner of a lattice plot

David Winsemius dwinsemius at comcast.net
Fri Dec 17 07:24:09 CET 2010

```On Dec 17, 2010, at 12:07 AM, Rajarshi Guha wrote:

> On Thu, Dec 16, 2010 at 11:26 PM, David Winsemius
> <dwinsemius at comcast.net> wrote:
>>
>> On Dec 16, 2010, at 11:12 PM, Rajarshi Guha wrote:
>>
>>> Hi, I have a series of lattice plots which I am arranging in a 2x2
>>> grid via print:
>>>
>>> print(p.preds, split=c(1,1, 2,2), more=TRUE)
>>> print(p.comp, split=c(2,1,2,2), more=TRUE)
>>> print(p.bw, split=c(1,2,2,2), more=FALSE)
>>>
>>> What I'd like to have is a letter (A, B, ...) in the top corner of
>>> each plot. While panel.text lets me add text anywhere within a
>>> plot, I
>>> can't seem to workout how I could put some text in the top left
>>> corner, say, of the whole plotting region.
>> ########>>>>>>>>>
>>>
>>> and provide commented, minimal, self-contained, reproducible code.
>
> Apologies for an incomplete post. Example code, based on Gabors
> suggestion is
>
> library(gridExtra)
> p1 <- xyplot(demand ~ Time, BOD)
> p2 <- xyplot(demand ~ Time, BOD)
> p3 <- xyplot(demand ~ Time, BOD)
> print(p1, split=c(1,1,2,2), more=TRUE)
> print(p2, split=c(2,1,2,2), more=TRUE)
> print(p3, split=c(1,2,2,2), more=FALSE)
>
> However, Gabors approach places the mark within the plot itself. What
> I'd ideally like is to have the mark be located in the margins, in the
> top right corner. (I am not sure of the correct terminology here). An
> example of the desired output can be seen at
> http://rguha.net/plot-annot.png
>

Something along these lines?

vp <- viewport(w = 1, h = 1, gp = gpar(col="blue")) # or "black"
grid.text("A",
y = .95, x=.05,
gp = gpar(fontsize=20), vp = vp)
grid.text("B",
y = .95, x=.55,
gp = gpar(fontsize=20), vp = vp)
grid.text("C",
y = .45, x=.05,
gp = gpar(fontsize=20), vp = vp)
--

David Winsemius, MD
West Hartford, CT

```