[R] lattice key (legend) with both points and lines

Peter McMahan peter.mcmahan at gmail.com
Wed Mar 21 04:00:47 CET 2007


On Mar 20, 2007, at 5:53 PM, Sundar Dorai-Raj wrote:
>
> Peter McMahan said the following on 3/20/2007 3:16 PM:
>> Hello,
>> I'm running into a frustrating problem with the legend on a  
>> lattice  plot I'm working with. The plot is a stripplot with a  
>> panel.linejoin () line running through the mean of each of the  
>> categories. Thus  there are both points and lines in the plot.
>> In trying to make a key for the plot, I can't figure out how to  
>> make  a legend for both the points and the lines. What I'd like  
>> is  something like:
>> prices    *
>> means   -----
>> in which there are two "rows" in the legend, one with the point  
>> and  its label and one with the line and its label. By supplying  
>> this type  of argument to stripplot:
>> key=list(text=list(lab="prices"),
>>           points=list(...),
>>           text=list(lab="means"),
>>           lines=list(...))
>> I can get a legend that looks like:
>> prices    *     means   -----
>> But this looks awkward with my plot
>> Is there any way to have the plot elements and their labels be in  
>> the  same column?
>> Thanks,
>> Peter
>> ______________________________________________
>> 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.
>
>
> You can try:
>
> library(lattice)
> xyplot(1:10 ~ 1:10,
>        key = list(text = list(c("prices", "means")),
>                  lines = list(
>                    pch = c("*", ""), type = c("p", "l"),
>                    cex = 2, col = "red", lwd = 3)))
>
> HTH,
>
> --sundar

That does the trick nicely, thanks! Didn't think of just telling the  
line to be a point. Here's the call that ended up working out for me:

stripplot(MedPrice~State,prices,
           ylab="Rate Premium (¢/kWh)",
           panel=function(x,y,...){
             panel.grid(h=0,v=34)
             panel.stripplot(x,y,jitter=T,factor=1,pch=20,...)
             panel.linejoin(x,y,horizontal=F,col.line="black",lty=2)
           },
           key=list(
             x=.72,y=.9,
             text=list(lab=c("observations","state mean")),
             lines=list(
                 pch=c(20,NA),
                 col="black",
                 type=c("p","l"),
                 lty=1:2,
                 cex=.8),
             transparent=T,border=F,rep=F
           ),col="black"
          )



More information about the R-help mailing list