Skip to content

how to plot a nice legend?

8 messages · Jonas Stein, Duncan Murdoch, Sarah Goslee +2 more

#
i'd like to plot a legend in my diagram. The diagram will be included 
in a TikZ LaTeX document later.

I tried the legend() function, but 
- it can not find a good place it self where the legend fits
  and playing around with coordinates and scaling consumes a lot time

- standard settings for the text need adjustment 
  (linespacing is quite large and so on)

Is there an alternative to legend()?

Is it possible to place the legend() outside of the plot area?

Kind regards,
#
On 12-02-10 3:45 PM, Jonas Stein wrote:
There are various alternatives available; you can also write your own, 
by modifying the standard one.

Generally there are lots of possibilities for customizing within the 
standard one; e.g. y.intersp will affect the line spacing, using a 
negative value for inset (together with xpd=NA) will allow the legend to 
be moved outside the plot.

Duncan Murdoch
#
i tried without success:

plot(1:10)
legend(1,3, legend=c("one", "two"), inset=-1, xpd=NA)

The legend is still placed inside the plot on point (1,3)

What could i have done wrong?

Can i include a legend like this in a standard plot like 
plot(1:10) too?
http://www.r-bloggers.com/wp-content/uploads/2011/03/heatmap.png

kind regards,
#
Hi,
On Sat, Feb 11, 2012 at 8:07 PM, Jonas Stein <news at jonasstein.de> wrote:
Wrong? Nothing. You told R to put the legend at c(1,3)
so it did. If you want it elsewhere you need to specify that.
legend(-1,3, legend=c("one", "two"), inset=-1, xpd=NA)
maybe, or some other location?
Yes.

What part of that do you want to duplicate? You can specify colors,
symbols, labels, etc. in legend().

Also, please link to the original blog post, not just the figure, so that
the author gets some credit and we can see the code used.

Sarah
#
ok that works fine. Now i understand how to use it.
If i create several plots it would be nice if all legends would 
have the same distance to plots with different scaling.

Can the legend be placed vertically centered, 1cm right to the plot aera?
The coloured squares.
for the reader who got to this article and had the same question:
I have just found another nice solution for a colour legend a minute ago
http://www.r-bloggers.com/rethinking-loess-for-binomial-response-pitch-fx-strike-zone-maps/
can i even invent my own symbols?
sure 
http://www.r-bloggers.com/ggheat-a-ggplot2-style-heatmap-function/

kind regards,
#
Have a lookt at the package ggplot2

I believe it will give you something like the image you link to.

Simple example:
----------------------------------------------------------------------------------
library(ggplot2)
mydata <- data.frame(a=1:10, b=1:10, c=letters[1:10])
ggplot(mydata, aes(a,b, colour=c)) + geom_point()
----------------------------------------------------------------------------------


John Kane
Kingston ON Canada
____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
#
On 12-02-11 9:07 PM, Jonas Stein wrote:
I'm not sure what position you mean, but legend("top", ...) puts it on 
the top edge of the plot, and the inset argument gives detailed 
positioning.  The units aren't cm, but the grDevices package (or is it 
grid?) has functions to convert between units.

Duncan Murdoch