specify size of box around legend

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

specify size of box around legend

Rainer Krug-3
Hi

assume the following code:

--8<---------------cut here---------------start------------->8---
plot(1,1)
legend(x="topleft", legend = LETTERS[1:10], title = "L 1")
legend(x="bottomleft", legend = paste(LETTERS[1:10], letters[1:12],LETTERS[1:10]), title = "L 2")
legend(x="topright", legend = LETTERS[1:15], title = "L 3")
--8<---------------cut here---------------end--------------->8---

The box around L 1 is less wide than the box around L 2 due to automatic
sizing of the box.

Is there a way of specifying the width of the box, so that L 1 and L 2
have the same width?

In the same sense: can I also specify the height of the legend, so that
L 1 and L 3 have the same height?

Thanks,

Rainer
--
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

signature.asc (463 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: specify size of box around legend

Duncan Murdoch-2
On 11/03/2016 9:01 AM, Rainer M Krug wrote:

> Hi
>
> assume the following code:
>
> --8<---------------cut here---------------start------------->8---
> plot(1,1)
> legend(x="topleft", legend = LETTERS[1:10], title = "L 1")
> legend(x="bottomleft", legend = paste(LETTERS[1:10], letters[1:12],LETTERS[1:10]), title = "L 2")
> legend(x="topright", legend = LETTERS[1:15], title = "L 3")
> --8<---------------cut here---------------end--------------->8---
>
> The box around L 1 is less wide than the box around L 2 due to automatic
> sizing of the box.
>
> Is there a way of specifying the width of the box, so that L 1 and L 2
> have the same width?

text.width should work.  (It is in the ?legend help page, along with
approximately one million other optional arguments.)
>
> In the same sense: can I also specify the height of the legend, so that
> L 1 and L 3 have the same height?

You can add blank entries as fillers to L 1.

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: specify size of box around legend

Rainer Krug-3
Duncan Murdoch <[hidden email]> writes:

> On 11/03/2016 9:01 AM, Rainer M Krug wrote:
>> Hi
>>
>> assume the following code:
>>
>> --8<---------------cut here---------------start------------->8---
>> plot(1,1)
>> legend(x="topleft", legend = LETTERS[1:10], title = "L 1")
>> legend(x="bottomleft", legend = paste(LETTERS[1:10], letters[1:12],LETTERS[1:10]), title = "L 2")
>> legend(x="topright", legend = LETTERS[1:15], title = "L 3")
>> --8<---------------cut here---------------end--------------->8---
>>
>> The box around L 1 is less wide than the box around L 2 due to automatic
>> sizing of the box.
>>
>> Is there a way of specifying the width of the box, so that L 1 and L 2
>> have the same width?
>
> text.width should work.  (It is in the ?legend help page, along with
> approximately one million other optional arguments.)
Thanks - that works.

May I suggest to change the documentation from:

--8<---------------cut here---------------start------------->8---
text.width: the width of the legend text in x (‘"user"’) coordinates.
          (Should be positive even for a reversed x axis.)  Defaults to
          the proper value computed by ‘strwidth(legend)’.
--8<---------------cut here---------------end--------------->8---

to

--8<---------------cut here---------------start------------->8---
text.width: the width of the legend text in x (‘"user"’) coordinates.
          (Should be positive even for a reversed x axis.)  If set,
         it is used to compute the width of the legend box. Defaults to
          the proper value computed by ‘strwidth(legend)’.
--8<---------------cut here---------------end--------------->8---

to make this clear?


>>
>> In the same sense: can I also specify the height of the legend, so that
>> L 1 and L 3 have the same height?
>
> You can add blank entries as fillers to L 1.

True - but no other parameter to achieve this? Equivalent to text.widht?


Thanks

Rainer


>
> Duncan Murdoch

--
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

signature.asc (463 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: specify size of box around legend

Duncan Murdoch-2
On 11/03/2016 9:50 AM, Rainer M Krug wrote:

> Duncan Murdoch <[hidden email]> writes:
>
> > On 11/03/2016 9:01 AM, Rainer M Krug wrote:
> >> Hi
> >>
> >> assume the following code:
> >>
> >> --8<---------------cut here---------------start------------->8---
> >> plot(1,1)
> >> legend(x="topleft", legend = LETTERS[1:10], title = "L 1")
> >> legend(x="bottomleft", legend = paste(LETTERS[1:10], letters[1:12],LETTERS[1:10]), title = "L 2")
> >> legend(x="topright", legend = LETTERS[1:15], title = "L 3")
> >> --8<---------------cut here---------------end--------------->8---
> >>
> >> The box around L 1 is less wide than the box around L 2 due to automatic
> >> sizing of the box.
> >>
> >> Is there a way of specifying the width of the box, so that L 1 and L 2
> >> have the same width?
> >
> > text.width should work.  (It is in the ?legend help page, along with
> > approximately one million other optional arguments.)
>
> Thanks - that works.
>
> May I suggest to change the documentation from:
>
> --8<---------------cut here---------------start------------->8---
> text.width: the width of the legend text in x (‘"user"’) coordinates.
>            (Should be positive even for a reversed x axis.)  Defaults to
>            the proper value computed by ‘strwidth(legend)’.
> --8<---------------cut here---------------end--------------->8---
>
> to
>
> --8<---------------cut here---------------start------------->8---
> text.width: the width of the legend text in x (‘"user"’) coordinates.
>            (Should be positive even for a reversed x axis.)  If set,
>           it is used to compute the width of the legend box. Defaults to
>            the proper value computed by ‘strwidth(legend)’.
> --8<---------------cut here---------------end--------------->8---
>
> to make this clear?

I think it's already clear, but if not, this conversation is now in the
public record, so anyone searching for "width of the legend box" on
Google should find the help they need soon enough.
>
>
> >>
> >> In the same sense: can I also specify the height of the legend, so that
> >> L 1 and L 3 have the same height?
> >
> > You can add blank entries as fillers to L 1.
>
> True - but no other parameter to achieve this? Equivalent to text.widht?

I've only read the first hundred thousand argument descriptions, and I
didn't find that.  You'll have to read the other nine hundred thousand
yourself.

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: specify size of box around legend

Duncan Mackay-4
In reply to this post by Rainer Krug-3
Hi

If you cannot get anything right in base graphics
You could plot the graph and then use
library(grid)
viewport(....)
etc
for each of the legends

or even lattice and grid

xyplot(...,
              legend =  list( ....),
)

see https://stat.ethz.ch/pipermail/r-help/2005-April/069459.html

for one way with grid package,  other ways may be using lattice and
lattice::draw.key

I' m a bit rusty on the actual workings

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: [hidden email]


-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Rainer M
Krug
Sent: Saturday, 12 March 2016 01:02
To: [hidden email]
Subject: [R] specify size of box around legend

Hi

assume the following code:

--8<---------------cut here---------------start------------->8---
plot(1,1)
legend(x="topleft", legend = LETTERS[1:10], title = "L 1")
legend(x="bottomleft", legend = paste(LETTERS[1:10],
letters[1:12],LETTERS[1:10]), title = "L 2")
legend(x="topright", legend = LETTERS[1:15], title = "L 3")
--8<---------------cut here---------------end--------------->8---

The box around L 1 is less wide than the box around L 2 due to automatic
sizing of the box.

Is there a way of specifying the width of the box, so that L 1 and L 2
have the same width?

In the same sense: can I also specify the height of the legend, so that
L 1 and L 3 have the same height?

Thanks,

Rainer
--
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: specify size of box around legend

Jim Lemon-4
In reply to this post by Rainer Krug-3
Hi Rainer,
You can use the text.width argument and override the calculated legend
text widths.

Jim


On Sat, Mar 12, 2016 at 1:01 AM, Rainer M Krug <[hidden email]> wrote:

> Hi
>
> assume the following code:
>
> --8<---------------cut here---------------start------------->8---
> plot(1,1)
> legend(x="topleft", legend = LETTERS[1:10], title = "L 1")
> legend(x="bottomleft", legend = paste(LETTERS[1:10], letters[1:12],LETTERS[1:10]), title = "L 2")
> legend(x="topright", legend = LETTERS[1:15], title = "L 3")
> --8<---------------cut here---------------end--------------->8---
>
> The box around L 1 is less wide than the box around L 2 due to automatic
> sizing of the box.
>
> Is there a way of specifying the width of the box, so that L 1 and L 2
> have the same width?
>
> In the same sense: can I also specify the height of the legend, so that
> L 1 and L 3 have the same height?
>
> Thanks,
>
> Rainer
> --
> Rainer M. Krug
> email: Rainer<at>krugs<dot>de
> PGP: 0x0F52F982
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: specify size of box around legend

David Carlson
Also you can specify the upper left/lower right coordinates of the legend box:

# Get the default positions/sizes
plot(1, 1)
a <- legend(x="topleft", legend = x, title = "L 1")
b <- legend(x="bottomleft", legend = y, title = "L 2")

# Use those to change the box size
plot(1, 1)
legend(x=c(a$rect$left, a$rect$left+b$rect$w), y=c(a$rect$top,
     a$rect$top-a$rect$h), legend = x, title = "L 1")
legend(x="bottomleft", legend = y, title = "L 2")

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Jim Lemon
Sent: Saturday, March 12, 2016 9:34 PM
To: Rainer M Krug; r-help mailing list
Subject: Re: [R] specify size of box around legend

Hi Rainer,
You can use the text.width argument and override the calculated legend
text widths.

Jim


On Sat, Mar 12, 2016 at 1:01 AM, Rainer M Krug <[hidden email]> wrote:

> Hi
>
> assume the following code:
>
> --8<---------------cut here---------------start------------->8---
> plot(1,1)
> legend(x="topleft", legend = LETTERS[1:10], title = "L 1")
> legend(x="bottomleft", legend = paste(LETTERS[1:10], letters[1:12],LETTERS[1:10]), title = "L 2")
> legend(x="topright", legend = LETTERS[1:15], title = "L 3")
> --8<---------------cut here---------------end--------------->8---
>
> The box around L 1 is less wide than the box around L 2 due to automatic
> sizing of the box.
>
> Is there a way of specifying the width of the box, so that L 1 and L 2
> have the same width?
>
> In the same sense: can I also specify the height of the legend, so that
> L 1 and L 3 have the same height?
>
> Thanks,
>
> Rainer
> --
> Rainer M. Krug
> email: Rainer<at>krugs<dot>de
> PGP: 0x0F52F982
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: specify size of box around legend

David Carlson
Forgot that I had changed the original example code:

You need this first to get the example to work:
x <- LETTERS[1:10]
y <- paste(LETTERS[1:10], letters[1:12],LETTERS[1:10])

-------
David C

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of David L Carlson
Sent: Monday, March 14, 2016 9:14 AM
To: Jim Lemon; Rainer M Krug; r-help mailing list
Subject: Re: [R] specify size of box around legend

Also you can specify the upper left/lower right coordinates of the legend box:

# Get the default positions/sizes
plot(1, 1)
a <- legend(x="topleft", legend = x, title = "L 1")
b <- legend(x="bottomleft", legend = y, title = "L 2")

# Use those to change the box size
plot(1, 1)
legend(x=c(a$rect$left, a$rect$left+b$rect$w), y=c(a$rect$top,
     a$rect$top-a$rect$h), legend = x, title = "L 1")
legend(x="bottomleft", legend = y, title = "L 2")

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Jim Lemon
Sent: Saturday, March 12, 2016 9:34 PM
To: Rainer M Krug; r-help mailing list
Subject: Re: [R] specify size of box around legend

Hi Rainer,
You can use the text.width argument and override the calculated legend
text widths.

Jim


On Sat, Mar 12, 2016 at 1:01 AM, Rainer M Krug <[hidden email]> wrote:

> Hi
>
> assume the following code:
>
> --8<---------------cut here---------------start------------->8---
> plot(1,1)
> legend(x="topleft", legend = LETTERS[1:10], title = "L 1")
> legend(x="bottomleft", legend = paste(LETTERS[1:10], letters[1:12],LETTERS[1:10]), title = "L 2")
> legend(x="topright", legend = LETTERS[1:15], title = "L 3")
> --8<---------------cut here---------------end--------------->8---
>
> The box around L 1 is less wide than the box around L 2 due to automatic
> sizing of the box.
>
> Is there a way of specifying the width of the box, so that L 1 and L 2
> have the same width?
>
> In the same sense: can I also specify the height of the legend, so that
> L 1 and L 3 have the same height?
>
> Thanks,
>
> Rainer
> --
> Rainer M. Krug
> email: Rainer<at>krugs<dot>de
> PGP: 0x0F52F982
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: specify size of box around legend

Rainer Krug-3
In reply to this post by David Carlson
David L Carlson <[hidden email]> writes:

> Also you can specify the upper left/lower right coordinates of the legend box:
>
> # Get the default positions/sizes
> plot(1, 1)
> a <- legend(x="topleft", legend = x, title = "L 1")
> b <- legend(x="bottomleft", legend = y, title = "L 2")
>
> # Use those to change the box size
> plot(1, 1)
> legend(x=c(a$rect$left, a$rect$left+b$rect$w), y=c(a$rect$top,
>      a$rect$top-a$rect$h), legend = x, title = "L 1")
> legend(x="bottomleft", legend = y, title = "L 2")
Interesting - I like it.

Thanks,

Rainer

>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
> -----Original Message-----
> From: R-help [mailto:[hidden email]] On Behalf Of Jim Lemon
> Sent: Saturday, March 12, 2016 9:34 PM
> To: Rainer M Krug; r-help mailing list
> Subject: Re: [R] specify size of box around legend
>
> Hi Rainer,
> You can use the text.width argument and override the calculated legend
> text widths.
>
> Jim
>
>
> On Sat, Mar 12, 2016 at 1:01 AM, Rainer M Krug <[hidden email]> wrote:
>> Hi
>>
>> assume the following code:
>>
>> --8<---------------cut here---------------start------------->8---
>> plot(1,1)
>> legend(x="topleft", legend = LETTERS[1:10], title = "L 1")
>> legend(x="bottomleft", legend = paste(LETTERS[1:10], letters[1:12],LETTERS[1:10]), title = "L 2")
>> legend(x="topright", legend = LETTERS[1:15], title = "L 3")
>> --8<---------------cut here---------------end--------------->8---
>>
>> The box around L 1 is less wide than the box around L 2 due to automatic
>> sizing of the box.
>>
>> Is there a way of specifying the width of the box, so that L 1 and L 2
>> have the same width?
>>
>> In the same sense: can I also specify the height of the legend, so that
>> L 1 and L 3 have the same height?
>>
>> Thanks,
>>
>> Rainer
>> --
>> Rainer M. Krug
>> email: Rainer<at>krugs<dot>de
>> PGP: 0x0F52F982
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

signature.asc (463 bytes) Download Attachment