[R] Labeling lines in xyplot

John Poulsen jpoulsen at zoo.ufl.edu
Tue Aug 5 21:24:04 CEST 2008


Deepayan,

Thanks for your help, works great!

Cheers -- John

Deepayan Sarkar wrote:
> On Tue, Aug 5, 2008 at 11:15 AM, John Poulsen <jpoulsen at zoo.ufl.edu> wrote:
>> Hello,
>>
>> I apologize in advance as this example seems really elementary.  Below I
>> have created a simple scatterplot with lines.  I would like to label each
>> line with the name of the village, instead of using a legend.  Can someone
>> please let me know how to do it.
> 
> Lebeling is a call to panel.text() in the panel function; you need to
> figure out where to put the labels and make sure there's space for
> them. Here's one way:
> 
> 
> vlevs <- sort(unique(Villages))
> 
> xyplot(Pop~Year,  groups=Villages, type="b",
>        main="Population of logging villages",
>        panel = panel.superpose,
>        panel.groups = function(x, y, group.number, ...) {
>            panel.xyplot(x, y, ...)
>            panel.text(x[1], y[1], vlevs[group.number], pos = 2)
>        },
>        lwd=2, lty=c(1,2,3,4,5,6), col=c(1,8,8,1,8), pch=1:5,
>        scales = list(tick.number = 6, tck = c(1, 0),
>                    x = list(limits = c(1998.5, 2006.5),
>                             at = 2000:2006)))
> 
> -Deepayan
> 
>> Thanks in advance -- John
>>
>>
>> Pop=c(1406,1862,2333,2664,2685,2521,2726,0,0,0,0,214,289,365,900,952,1002,1205,1041,942,1004,
>>    616,806,859,991,865,810,399,7200,9136,11389,12634,13114,13417,12670)
>> Villages=rep(c("Kabo", "Loundoungou", "Ndoki 1", "Ndoki 2",
>> "Pokola"),each=7)
>> Year=rep(c(2000,2001,2002,2003,2004,2005,2006),7)
>>
>> xyplot(Pop~Year, groups=Villages,type="b",
>>                 main="Population of logging villages",
>>                  xlab=list(expression("Years"),cex=1.2),
>>                  ylab=list(expression("Number of people"),cex=1.2),
>>                  lwd=2,lty=c(1,2,3,4,5,6),col=c(1,8,8,1,8),pch=1:5,
>>                 scales=list(tick.number=6, tck=c(1,0)))
>>
>> ______________________________________________
>> R-help at r-project.org 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