Adding labels within lattice panels by group

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

Citation

For attribution, please cite this work as:

Markus Gesmann (Feb 18, 2014) Adding labels within lattice panels by group. Retrieved from https://magesblog.com/post/2014-02-18-adding-labels-within-lattice-panels-by/

BibTeX citation:

@misc{ 2014-adding-labels-within-lattice-panels-by-group,
 author = { Markus Gesmann },
 title = { Adding labels within lattice panels by group },
 url = { https://magesblog.com/post/2014-02-18-adding-labels-within-lattice-panels-by/ },
 year = { 2014 }
 updated = { Feb 18, 2014 }
}

Related