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

Gabor Grothendieck ggrothendieck at gmail.com
Fri Dec 17 12:57:14 CET 2010


On Fri, Dec 17, 2010 at 12:07 AM, Rajarshi Guha <rajarshi.guha at gmail.com> 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

In that case use page= rather than panel=.   Unfortunately page only
passes the page number but we can wrap it in a proto object from the
proto package to get the desired effect:

library(gridExtra)
library(proto)
pg <- function(., n) grid.text(label = mark,
	just = c("left", "center"),
    x = unit(0.1, "npc"), y = unit(0.9, "npc"))
fo <- demand ~ Time
grid.arrange(nrow = 2,
	xyplot(fo, BOD, page = proto(mark = "A", pg = pg)$pg),
	xyplot(fo, BOD, page = proto(mark = "B", pg = pg)$pg),
	xyplot(fo, BOD, page = proto(mark = "C", pg = pg)$pg)
)


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list