[R] Plotting text with lattice

Gabor Grothendieck ggrothendieck at gmail.com
Thu Oct 5 19:38:45 CEST 2006


I seem to have omitted g

library(lattice)
xyplot(x ~ x | g, data = data.frame(x = 1:12, g = gl(3,4)), panel =
function(...) {
       panel.xyplot(...)
       panel.text(x=2, y=4, labels=which.packet())
})


On 10/5/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> This requires R 2.4.0.  Its the same as my earlier example except
> the labels= arg has been changed to labels=which.packet().
>
>
> xyplot(x ~ x | g, data = data.frame(x = 1:12), panel = function(...) {
>       panel.xyplot(...)
>       panel.text(x=2, y=4, labels=which.packet())
>
> })
>
>
> On 10/5/06, Ritwik Sinha <ritwik.sinha at gmail.com> wrote:
> > Hi,
> >
> > On a related note, if I wanted to add different texts to different
> > panels, should I stick to using trellis.focus() for each text in each
> > panel? I cannot figure out a way to do it using a panel function.
> >
> > Ritwik.
> >
> > On 9/29/06, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote:
> > > On 9/29/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> > > > Here are two possibilities.  The first use trellis.focus/trellis/unfocus to
> > > > add text subsequent to drawing the xyplot and the second uses a
> > > > custom panel:
> > > >
> > > > xyplot(x ~ x, data = data.frame(x = 1:10))
> > > > trellis.focus("panel", 1, 1)
> > > > panel.text(x=2, y=4, labels="Text")
> > > > trellis.unfocus()
> > > >
> > > > xyplot(x ~ x, data = data.frame(x = 1:10), panel = function(...) {
> > > >         panel.xyplot(...)
> > > >         panel.text(x=2, y=4, labels="Text")
> > > > })
> > >
> > > Right, on a more general note, this is necessary because it is not clear what
> > >
> > > ltext(x=2, y=4, labels="Text")
> > >
> > > should do for a multi-panel plot. On an even more general note, you
> > > will keep getting in trouble when using lattice if you (1) try to
> > > follow the "incremental addition" approach of standard graphics or (2)
> > > use the par() system in any way.
> > >
> > > Deepayan
> > >
> > > > On 9/29/06, McGehee, Robert <Robert.McGehee at geodecapital.com> wrote:
> > > > > Hello,
> > > > > I've decided to take the leap and try my hand at the lattice package,
> > > > > though I am getting stuck at what one might consider a trivial problem,
> > > > > plotting text at a point in a graph. Apologies in advance if (that) I'm
> > > > > missing something extremely basic.
> > > > >
> > > > > Consider in base graphics:
> > > > > > plot(1:10)
> > > > > > text(2, 4, "Text")
> > > > > In the above you will see text centered at the point (2, 4) on the
> > > > > graph.
> > > > >
> > > > > Now I would like to try to do the same thing using the lattice package:
> > > > >
> > > > > > xyplot(x ~ x, data = data.frame(x = 1:10))
> > > > > > ltext(x=2, y=4, labels="Text")
> > > > > > panel.text(x=2, y=4, labels="Text")
> > > > > > grid.text(label="Text", x=2, y=4)
> > > > > > grid.text(label="Text", x=unit(2, "native"), y=unit(4, "native"))
> > > > >
> > > > > None of the above four commands puts the "Text" at the (2, 4) point on
> > > > > the graph. Any help with this would be appreciated! Also, if I have more
> > > > > than one panel and would like to place text at different points on
> > > > > different panels how would I do this?
> > > > >
> > > > > Also, note that I'm hoping to use text to label interesting points in a
> > > > > levelplot, but am using the above xyplot as an example.
> > > > >
> > > > > Thanks,
> > > > > Robert
> > > > >
> > > > >
> > > > > Robert McGehee
> > > > > Quantitative Analyst
> > > > > Geode Capital Management, LLC
> > > > > 53 State Street, 5th Floor | Boston, MA | 02109
> > > > > Tel: 617/392-8396    Fax:617/476-6389
> > > > > mailto:robert.mcgehee at geodecapital.com
> > > > >
> > > > >
> > > > >
> > > > > This e-mail, and any attachments hereto, are intended for us...{{dropped}}
> > > > >
> > > > > ______________________________________________
> > > > > R-help at stat.math.ethz.ch mailing list
> > > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > > > > and provide commented, minimal, self-contained, reproducible code.
> > > > >
> > > >
> > > > ______________________________________________
> > > > R-help at stat.math.ethz.ch mailing list
> > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > > > and provide commented, minimal, self-contained, reproducible code.
> > > >
> > >
> > >
> > > --
> > > http://www.stat.wisc.edu/~deepayan/
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
> >
> > --
> > Ritwik Sinha
> > Graduate Student
> > Epidemiology and Biostatistics
> > Case Western Reserve University
> > ritwik.sinha at gmail.com | +12163682366 | http://darwin.cwru.edu/~rsinha
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>



More information about the R-help mailing list