Skip to content

Legend placement in barplot?

7 messages · Marc Schwartz, Gabor Grothendieck, Dan Bolser +1 more

#
This has been asked before, but all the answers are hidiously complex.

The

legend.text=TRUE 

option of barplot is almost exactly what I need, except I need a

legend.placement='tl'

(top left) option. This option would be in contrast to the default
placement which we could call 'tr' (top right).

Anyone know how to edit the barplot code to make this change? Could
someone like me work out how to do this?

Cheers,
Dan.
#
On Wed, 2004-11-03 at 09:55, Dan Bolser wrote:
Dan,

Do not edit the barplot() code. Use the legend() function instead, which
enables you to specify the x,y coordinates of the upper left hand corner
of the legend box. See ?legend

A fair number of the questions that you have had regarding graphics are
covered in Chapter 12 "Graphical Procedures" in "An Introduction to R":

http://cran.r-project.org/doc/manuals/R-intro.pdf

which is included with the R installation.

Another online resource for some graphics assistance would be R News
Volume 3 Number 2 for October 2003, which has an article on R's base
graphics in the R Help Desk section:

http://cran.r-project.org/doc/Rnews/Rnews_2003-2.pdf

Notwithstanding all of that, searching the r-help archives is yet
another terrific online (and free) resource that you _should_ avail
yourself of.

HTH,

Marc Schwartz
#
Dan Bolser <dmb <at> mrc-dunn.cam.ac.uk> writes:

: 
: This has been asked before, but all the answers are hidiously complex.
: 
: The
: 
: legend.text=TRUE 
: 
: option of barplot is almost exactly what I need, except I need a
: 
: legend.placement='tl'
: 
: (top left) option. This option would be in contrast to the default
: placement which we could call 'tr' (top right).
: 
: Anyone know how to edit the barplot code to make this change? Could
: someone like me work out how to do this?
: 


In package gplots (in bundle gregmisc in R 2.0.0) there is smartlegend.
#
Dan Bolser <dmb <at> mrc-dunn.cam.ac.uk> writes:

: 
: This has been asked before, but all the answers are hidiously complex.
: 
: The
: 
: legend.text=TRUE 
: 
: option of barplot is almost exactly what I need, except I need a
: 
: legend.placement='tl'
: 
: (top left) option. This option would be in contrast to the default
: placement which we could call 'tr' (top right).
: 
: Anyone know how to edit the barplot code to make this change? Could
: someone like me work out how to do this?
: 
: Cheers,
: Dan.


Check out gplots::smartlegend (in the R 2.0.0 gregmisc bundle).
#
On Wed, 3 Nov 2004, Marc Schwartz wrote:

            
Thing is I need to pass legend the correct groups and correct plotting
colors and correct XY position relative to my data. All these things are
already known by the barplot function, and used to draw a beautiful
legend.

The fact that this legend can only appear in the upper right hand corner
is surly a bug worthy of changing the code for?
Thanks for the links
Quoting me... "This has been asked before, but all the answers are
hidiously complex."
#
On Wed, 3 Nov 2004, Gabor Grothendieck wrote:

            
This works great, but like the (smart)legend function, fill=true appears
to be giving me only black boxes.

Here is what I add..

smartlegend(x="left",y="top",
            c("PDB","MSD"),
            fill=TRUE,
            col=c("red","blue")
           )

The result is two black boxes! I tried swapping the order of the color and
fill options, but to the same effect.

I got round the problem by using...

smartlegend(x="left",y="top",
            c("PDB","MSD"),
            col=c("red","blue"),
            lwd=5
            )

Not quite the same, but good enough.


One other thing (while I am generally complaining), the legend dosn't
scale correctly as I change the image size with the mouse. All the other
aspects of the barplot scale correctly. If I redraw the legend after
changing the size it is scaled correctly, suggesting that this problem
isn't fundamental, but is a bug in the implementation of legend.
#
At 13:24 2004-11-04 +0000, Dan Bolser wrote:

            
For coloured boxes, use

smartlegend(x="left",
                    y="top",
                    c("PDB","MSD"),
                  fill=c("red","blue"))

HTH,
Henric