Skip to content

Tables in legend

3 messages · jim holtman, Vemuri, Aparna

#
I need to create a legend for a simple scatter plot in the following
format.

 This is Blah1	number1	number2
 This is Blah2	number3	number4
 .
 .
 .
 This is Blah6	number11	number12

I looked up these help pages and found the following solution. 
lStr<-c(Blah1, Blah2,....Blah6, number 1, number2, ...number12)
legend(x="topright",lStr,ncol=3)

So this creates the tabular format I am looking for. But the width of
each column is matched to the width of the longest column. So the legend
now looks like this:

 This is Blah1			number1			number2
 This is Blah2			number3			number4
 .
 .
 .
 This is Blah6			number11			number12


And thus, I have the legend occupying most part of the top half of my
plot. I was wondering if there is any way to fit in the legend compactly
or even better to format the width of columns 2 and 3 in the legend and
remove the blank spaces between them. I have tried rounding off the
number values to 3 digits with no luck.  

I hope this is not too ambitious to ask.

Thanks in advance!
Aparna
#
This will probably do what you need:

plot(1)
x <- 1:10  # numbers for the legend
blah <- LETTERS[1:5]
legendData <- character(5)
for (i in 1:5)
    legendData[i] <- sprintf("Blah-%s %d %d", blah[i], x[2*i - 1], x[2*i])
legend('topright', legend=legendData)
On Fri, Feb 6, 2009 at 6:57 PM, Vemuri, Aparna <avemuri at epri.com> wrote:

  
    
2 days later
#
Thanks Jim. That was the first thing I tried. I should have mentioned
that I was also trying to write the three columns of the legend using
three different colors. So I want the "blah" column to be black, second
column to be red and the third column to be blue.

I would appreciate if you can share any ideas on how to do this.  
	

-----Original Message-----
From: jim holtman [mailto:jholtman at gmail.com] 
Sent: Friday, February 06, 2009 4:52 PM
To: Vemuri, Aparna
Cc: r-help at r-project.org
Subject: Re: [R] Tables in legend

This will probably do what you need:

plot(1)
x <- 1:10  # numbers for the legend
blah <- LETTERS[1:5]
legendData <- character(5)
for (i in 1:5)
    legendData[i] <- sprintf("Blah-%s %d %d", blah[i], x[2*i - 1],
x[2*i])
legend('topright', legend=legendData)
On Fri, Feb 6, 2009 at 6:57 PM, Vemuri, Aparna <avemuri at epri.com> wrote:
legend
number12
compactly
and
http://www.R-project.org/posting-guide.html