Adding labels within lattice panels by group

3 comments
The other day I had data that showed the development of many products over time. I grouped the products into categories and visualised the data as line graphs in lattice. But instead of adding an extensive legend to the plot I wanted to add labels to each line's latest point. How do you do that? It turns out that panel.groups is there to help again.

Here is my solution:

R code



Session Info

R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lattice_0.20-24

loaded via a namespace (and not attached):
[1] grid_3.0.2

3 comments :

Song Qian said...

Cool. But you should correct a typo. The last line (panel.text) should use "dat$product", not "dat$category".

Markus Gesmann said...

Oops. Thanks for pointing this out. Fixed now.

Carlos Ortega said...

Thanks for your post.
Package "directlabels" provides additional ways to label your series in lattice plots.

http://directlabels.r-forge.r-project.org/
http://directlabels.r-forge.r-project.org/docs/index.html

Thanks,
Carlos Ortega.

Post a Comment