Skip to content

Making legend() look like my plot()

8 messages · Martin Maechler, Dan Bolser, Uwe Ligges +1 more

#
Hello,

I am using code like the following to create as simple plot...



plot(x,y,type='b')
lines(lowess(x,y),lwd=3,lty=3,col=2)

I want to add a legend which shows lines looking exactly like those used
in my plot, i.e. a thin black line with gaps taken up by circles (the
default for type='b', and a thick dashed red line with no pch at all).

I have two problems, 

1) making the pch on the first like look like type = 'b' (gaps around pch)
2) surpressing a pch on for the second line


Any help with these two problems would be greatly appreciated.

Any archive of plots and code to browse which could help me visually find
what I want and then copy the code?


An online user contributable database of 'graphics in R' would be
smashing.


How come some smart people dont just let me do something like

legend(xpos,ypos,legend=add)

to add a legend to the current plot for all the relevant points and lines
which have been added so far?
#
Is this an impossible task?

How about just problem 2 below, having one pch in one legend entry, but no
pch in the second?
On Thu, 25 Nov 2004, Dan Bolser wrote:

            
#
Dan Bolser wrote:
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)

Uwe Ligges
#
On Thu, 25 Nov 2004, Uwe Ligges wrote:

            
Ahhh... I tried pch=c(1,NULL), pt.bg='white' I couldn't work out what was
going on..

thanks very much for the info
#

        
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" !

Martin
#
On Fri, 26 Nov 2004, Martin Maechler wrote:

            
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?

Cheers,
Dan.
#
Dan Bolser wrote:

            
It is really fundamental!
[Maybe not that fundamental for lattice as for base graphics.]

Uwe Ligges
2 days later
#
Hi
Uwe Ligges wrote:
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