[R] Making legend() look like my plot()

Paul Murrell p.murrell at auckland.ac.nz
Sun Nov 28 20:46:36 CET 2004


Hi


Uwe Ligges wrote:
> Dan Bolser wrote:
> 
>> On Fri, 26 Nov 2004, Martin Maechler wrote:
>>
>>
>>>>>>>> "Dan" == Dan Bolser <dmb at mrc-dunn.cam.ac.uk>
>>>>>>>>    on Thu, 25 Nov 2004 22:35:22 +0000 (GMT) writes:
>>>>>>>
>>>
>>>   Dan> On Thu, 25 Nov 2004, Uwe Ligges wrote:
>>>   >> Dan Bolser wrote:
>>>   >>> Is this an impossible task?
>>>   >>>   >>> How about just problem 2 below, having one pch in one
>>>   >>> legend entry, but no pch in the second?
>>>   >>  Please be at least a little bit patient! This is not a
>>>   >> hotline! People are not working 24 hours a day just to
>>>   >> answer your questions at once - they are answering
>>>   >> questions on a voluntary basis!
>>>   >>   >> answer 1) is not straightforward, but you might want to
>>>   >> use one of fillable symbols mentioned in ?points,
>>>   >> e.g. number 21
>>>   >>   >> answer 2) pch = c(1, NA) should do the trick.
>>>   >>   >> legend(....., pch=c(21,NA), lwd=c(1,3), lty=c(1,3),
>>>   >>               pt.bg="white", col=1:2)
>>>
>>>   Dan> Ahhh... I tried pch=c(1,NULL), pt.bg='white'   Dan> I couldn't 
>>> work out what was going on..
>>>
>>> hmm, really,... at least you could have tried to see  what
>>> c(1,NULL) is, by just "typing it at the prompt" !
>>
>>
>>
>> Too long at the SQL prompt made me think it was immutably correct.
>>
>> That and the fact that c(1,'') was making both pch dissapear made me 
>> think
>> the whole thing was messed up.
>>
>>
>> How come legend isn't ablt to auto detect the line types in my plot and
>> add them automatically?
>>
>> Is this just an issue of 'that would be cool if I had time', or is it 
>> more
>> fundamental?
> 
> 
> It is really fundamental!
> [Maybe not that fundamental for lattice as for base graphics.]


For example, ...

     xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width |
            Species,
            data = iris, scales = "free", layout = c(2, 2),
            auto.key = TRUE)

... and even in traditional graphics there are some automated legends ...

       a <- expand.grid(1:20, 1:20)
       b <- matrix(a[,1] + a[,2], 20)
       filled.contour(x = 1:20, y = 1:20, z = b)

... but this is only really possible when an entire plot is produced 
from a single function call (so everything is known about the plot at 
once).  The general problem is that R tries to be useful by allowing you 
to add extra lines, points, ... to any part of a plot, but the price of 
this flexibility is that R has no idea what is actually a data series in 
a plot and what is just some piece of decoration.  With the grid 
graphics package, it is possible to build up a graphical object 
representing a plot, so it might then be possible to make the 
distinction between output representing data series and other stuff; 
and here we encounter your previous statement, 'that would be cool if I 
had time' :)

Paul
-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/




More information about the R-help mailing list