Positioning text in top left corner of plot

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

Positioning text in top left corner of plot

Daniel Brewer-3
Simple question how can you position text in the top left hand corner of
a plot?  I am plotting multiple plots using par(mfrow=c(2,3)) and all I
want to do is label these plots a), b), c) etc.  I have been fiddling
around with both text and mtext but without much luck.  text is fine but
 each plot has a different scale on the axis and so this makes it
problematic.  What is the best way to do this?

Many thanks

Dan
--
**************************************************************
Daniel Brewer, Ph.D.

Institute of Cancer Research
Email: [hidden email]
**************************************************************

The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP.

This e-mail message is confidential and for use by the addre...{{dropped}}

______________________________________________
[hidden email] mailing list
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: Positioning text in top left corner of plot

Benilton Carvalho
maybe this is what you want?

plot(rnorm(10))
legend("topleft", "A)", bty="n")

?

b

On Aug 7, 2007, at 11:08 AM, Daniel Brewer wrote:

> Simple question how can you position text in the top left hand  
> corner of
> a plot?  I am plotting multiple plots using par(mfrow=c(2,3)) and  
> all I
> want to do is label these plots a), b), c) etc.  I have been fiddling
> around with both text and mtext but without much luck.  text is  
> fine but
>  each plot has a different scale on the axis and so this makes it
> problematic.  What is the best way to do this?
>
> Many thanks
>
> Dan
> --
> **************************************************************
> Daniel Brewer, Ph.D.
>
> Institute of Cancer Research
> Email: [hidden email]
> **************************************************************
>
> The Institute of Cancer Research: Royal Cancer Hospital, a  
> charitable Company Limited by Guarantee, Registered in England  
> under Company No. 534147 with its Registered Office at 123 Old  
> Brompton Road, London SW7 3RP.
>
> This e-mail message is confidential and for use by the add...{{dropped}}

______________________________________________
[hidden email] mailing list
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: Positioning text in top left corner of plot

Gregory Snow
In reply to this post by Daniel Brewer-3


You can call par('usr') to find the current coordinates, then use the
text function (the pos and offset arguments may be helpful as well)

The cnvrt.coords function from the TeachingDemos package may also be of
help for finding coordinates for the text function.

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
(801) 408-8111
 
 

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Daniel Brewer
> Sent: Tuesday, August 07, 2007 9:09 AM
> To: [hidden email]
> Subject: [R] Positioning text in top left corner of plot
>
> Simple question how can you position text in the top left
> hand corner of a plot?  I am plotting multiple plots using
> par(mfrow=c(2,3)) and all I want to do is label these plots
> a), b), c) etc.  I have been fiddling around with both text
> and mtext but without much luck.  text is fine but  each plot
> has a different scale on the axis and so this makes it
> problematic.  What is the best way to do this?
>
> Many thanks
>
> Dan
> --
> **************************************************************
> Daniel Brewer, Ph.D.
>
> Institute of Cancer Research
> Email: [hidden email]
> **************************************************************
>
> The Institute of Cancer Research: Royal Cancer Hospital, a
> charitable Company Limited by Guarantee, Registered in
> England under Company No. 534147 with its Registered Office
> at 123 Old Brompton Road, London SW7 3RP.
>
> This e-mail message is confidential and for use by the\ > ...{{dropped}}

______________________________________________
[hidden email] mailing list
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: Positioning text in top left corner of plot

Daniel Brewer-3
In reply to this post by Benilton Carvalho
Thanks for the replies, but I still cannot get what I want.  I do not
want the label inside the plot area, but in the top left of the paper, I
suppose in the margins.  When I try to use text to do this, it does not
seem to plot it outside the plot area.  I have also tried to use mtext,
but that does not really cut it, as I cannot get the label in the
correct position.  Ideally, it would be best if I could use legend but
have it outside the plot area.

Any ideas?

Thanks

Benilton Carvalho wrote:

> maybe this is what you want?
>
> plot(rnorm(10))
> legend("topleft", "A)", bty="n")
>
> ?
>
> b
>
> On Aug 7, 2007, at 11:08 AM, Daniel Brewer wrote:
>
>> Simple question how can you position text in the top left hand corner of
>> a plot?  I am plotting multiple plots using par(mfrow=c(2,3)) and all I
>> want to do is label these plots a), b), c) etc.  I have been fiddling
>> around with both text and mtext but without much luck.  text is fine but
>>  each plot has a different scale on the axis and so this makes it
>> problematic.  What is the best way to do this?
>>
>> Many thanks
>>
>> Dan
--
**************************************************************
Daniel Brewer, Ph.D.
Institute of Cancer Research
Email: [hidden email]
**************************************************************

The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP.

This e-mail message is confidential and for use by the addre...{{dropped}}

______________________________________________
[hidden email] mailing list
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: Positioning text in top left corner of plot

Jim Lemon
Daniel Brewer wrote:

> Thanks for the replies, but I still cannot get what I want.  I do not
> want the label inside the plot area, but in the top left of the paper, I
> suppose in the margins.  When I try to use text to do this, it does not
> seem to plot it outside the plot area.  I have also tried to use mtext,
> but that does not really cut it, as I cannot get the label in the
> correct position.  Ideally, it would be best if I could use legend but
> have it outside the plot area.
>
> Any ideas?
>
Hi Dan,

Try this:

plot(1:5)
par(xpd=TRUE)
text(0.5,5.5,"Outside")
par(xpd=FALSE)

Jim

______________________________________________
[hidden email] mailing list
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: Positioning text in top left corner of plot

Paul Murrell-2
In reply to this post by Daniel Brewer-3
Hi


Daniel Brewer wrote:
> Thanks for the replies, but I still cannot get what I want.  I do not
> want the label inside the plot area, but in the top left of the paper, I
> suppose in the margins.  When I try to use text to do this, it does not
> seem to plot it outside the plot area.  I have also tried to use mtext,
> but that does not really cut it, as I cannot get the label in the
> correct position.  Ideally, it would be best if I could use legend but
> have it outside the plot area.
>
> Any ideas?


plot(1:10)
library(grid)
grid.text("What do we want?  Text in the corner!\nWhere do we want it?
Here!",
           x=unit(2, "mm"), y=unit(1, "npc") - unit(2, "mm"),
           just=c("left", "top"))

Paul


> Thanks
>
> Benilton Carvalho wrote:
>> maybe this is what you want?
>>
>> plot(rnorm(10))
>> legend("topleft", "A)", bty="n")
>>
>> ?
>>
>> b
>>
>> On Aug 7, 2007, at 11:08 AM, Daniel Brewer wrote:
>>
>>> Simple question how can you position text in the top left hand corner of
>>> a plot?  I am plotting multiple plots using par(mfrow=c(2,3)) and all I
>>> want to do is label these plots a), b), c) etc.  I have been fiddling
>>> around with both text and mtext but without much luck.  text is fine but
>>>  each plot has a different scale on the axis and so this makes it
>>> problematic.  What is the best way to do this?
>>>
>>> Many thanks
>>>
>>> Dan


--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
[hidden email]
http://www.stat.auckland.ac.nz/~paul/

______________________________________________
[hidden email] mailing list
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: Positioning text in top left corner of plot

Daniel Brewer-3
In reply to this post by Daniel Brewer-3
Thanks.  That works if it is only a single plot, but if there are
multiple plots (e.g. par(mfrow=c(2,2))) it confusingly puts the label in
the absolute top left always i.e. the top left of plot one.

Dan

S Ellison wrote:

> Try something like
> mtext(side=3, line=-1, text="Here again?", adj=0, outer=T)
>
> This puts text just inside the top left corner.
>
>
>>>> Jim Lemon <[hidden email]> 10/08/2007 10:37:30 >>>
> Daniel Brewer wrote:
>> Thanks for the replies, but I still cannot get what I want.  I do not
>> want the label inside the plot area, but in the top left of the paper, I
>> suppose in the margins.  When I try to use text to do this, it does not
>> seem to plot it outside the plot area.  I have also tried to use mtext,
>> but that does not really cut it, as I cannot get the label in the
>> correct position.  Ideally, it would be best if I could use legend but
>> have it outside the plot area.
>>
>> Any ideas?
>>
> Hi Dan,
>
> Try this:
>
> plot(1:5)
> par(xpd=TRUE)
> text(0.5,5.5,"Outside")
> par(xpd=FALSE)
>
> Jim


The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP.

This e-mail message is confidential and for use by the addre...{{dropped}}

______________________________________________
[hidden email] mailing list
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: Positioning text in top left corner of plot

Daniel Brewer-3
In reply to this post by Paul Murrell-2
This works fine for one plot, but if it is a multiple plot (mfrow=c(2,2)
say) then each individual label is placed in the same position i.e.
absolute top left on the canvas.  I would like it top left of each
individual plot.

Thanks anyway.  Got any idea how to fix this?

Dan

Paul Murrell wrote:

> Hi
>
>
> Daniel Brewer wrote:
>> Thanks for the replies, but I still cannot get what I want.  I do not
>> want the label inside the plot area, but in the top left of the paper, I
>> suppose in the margins.  When I try to use text to do this, it does not
>> seem to plot it outside the plot area.  I have also tried to use mtext,
>> but that does not really cut it, as I cannot get the label in the
>> correct position.  Ideally, it would be best if I could use legend but
>> have it outside the plot area.
>>
>> Any ideas?
>
>
> plot(1:10)
> library(grid)
> grid.text("What do we want?  Text in the corner!\nWhere do we want it?
> Here!",
>           x=unit(2, "mm"), y=unit(1, "npc") - unit(2, "mm"),
>           just=c("left", "top"))
>
> Paul
>
>
>> Thanks
>>
>> Benilton Carvalho wrote:
>>> maybe this is what you want?
>>>
>>> plot(rnorm(10))
>>> legend("topleft", "A)", bty="n")
>>>
>>> ?
>>>
>>> b
>>>
>>> On Aug 7, 2007, at 11:08 AM, Daniel Brewer wrote:
>>>
>>>> Simple question how can you position text in the top left hand
>>>> corner of
>>>> a plot?  I am plotting multiple plots using par(mfrow=c(2,3)) and all I
>>>> want to do is label these plots a), b), c) etc.  I have been fiddling
>>>> around with both text and mtext but without much luck.  text is fine
>>>> but
>>>>  each plot has a different scale on the axis and so this makes it
>>>> problematic.  What is the best way to do this?
>>>>
>>>> Many thanks
>>>>
>>>> Dan
>
>


The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP.

This e-mail message is confidential and for use by the addre...{{dropped}}

______________________________________________
[hidden email] mailing list
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: Positioning text in top left corner of plot

Daniel Brewer-3
In reply to this post by Jim Lemon
Jim Lemon wrote:

> Daniel Brewer wrote:
>> Thanks for the replies, but I still cannot get what I want.  I do not
>> want the label inside the plot area, but in the top left of the paper, I
>> suppose in the margins.  When I try to use text to do this, it does not
>> seem to plot it outside the plot area.  I have also tried to use mtext,
>> but that does not really cut it, as I cannot get the label in the
>> correct position.  Ideally, it would be best if I could use legend but
>> have it outside the plot area.
>>
>> Any ideas?
>>
> Hi Dan,
>
> Try this:
>
> plot(1:5)
> par(xpd=TRUE)
> text(0.5,5.5,"Outside")
> par(xpd=FALSE)
>
> Jim

Here is what I used in the end:
par(xpd=T)
text(-0.15*(par("usr")[2]-par("usr")[1]),par("usr")[4]+0.14*(par("usr")[4]-par("usr")[3]),labels[i],cex=1.5)
par(xpd=F)

Ans that worked a treat.
Thanks

Dan

--
**************************************************************
Daniel Brewer, Ph.D.
Institute of Cancer Research
Email: [hidden email]
**************************************************************

The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP.

This e-mail message is confidential and for use by the addre...{{dropped}}

______________________________________________
[hidden email] mailing list
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: Positioning text in top left corner of plot

Manta
Dear all,

another questions related to zoo plotting. I would like to do as in the subject. Here a reproducible code:

library(zoo)
par(mfrow=c(2,1)
plot(zoo(seq(1:10),as.Date(seq(1:10),origin="1970-01-01")),xlab="",ylab="",main="Value",las=1)
mtext("EUR billions",adj=0,cex=0.7)
plot(zoo(seq(1:10),as.Date(seq(1:10),origin="1970-01-01")),xlab="",ylab="",main="Value",las=1)
par(xpd=T)
text(par("usr")[1],par("usr")[3]+10.5,"EUR billions",cex=0.7)

In the first graph I use the mtext function, which does the trick but it places the text too close too the y-axis.
A second possibility is to use the text function, prior specification of the option parameter par(xpd=T). However, this does not look consistent as, depending on the graph, I would need to add different numbers in the y specification of the axis (par("usr")[3]+XXX).

Any idea?
Reply | Threaded
Open this post in threaded view
|

Re: Positioning text in top left corner of plot

Benno Pütz
Regarding example 1:
mtext has a line parameter that should help you

example 2:
using

         text(par("usr")[1],par("usr")[4],"EUR billions",cex=0.7,adj=c(0,-1.5))

does not depend on the axis (values/range) beyond par('usr')

With

         text(par("usr")[1], …

you get something closer to your second example, will however not write outside your plot for long texts


        Benno

On Feb 7, 2012, at 12:06 PM, Manta wrote:

> Dear all,
>
> another questions related to zoo plotting. I would like to do as in the
> subject. Here a reproducible code:
>
> library(zoo)
> par(mfrow=c(2,1))
> plot(zoo(seq(1:10),as.Date(seq(1:10),origin="1970-01-01")),xlab="",ylab="",main="Value",las=1)
> mtext("EUR billions",adj=0,cex=0.7)
> plot(zoo(seq(1:10),as.Date(seq(1:10),origin="1970-01-01")),xlab="",ylab="",main="Value",las=1)
> par(xpd=T)
> text(par("usr")[1],par("usr")[3]+10.5,"EUR billions",cex=0.7)
>
> In the first graph I use the mtext function, which does the trick but it
> places the text too close too the y-axis.
> A second possibility is to use the text function, prior specification of the
> option parameter par(xpd=T). However, this does not look consistent as,
> depending on the graph, I would need to add different numbers in the y
> specification of the axis (par("usr")[3]+XXX).
>
> Any idea?
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Positioning-text-in-top-left-corner-of-plot-tp831723p4364355.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>
Benno Pütz
Statistical Genetics
MPI of Psychiatry
Kraepelinstr. 2-10
80804 Munich, Germany
T: ++49-(0)89-306 22 222
F: ++49-(0)89-306 22 601




        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
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: Positioning text in top left corner of plot

Manta
Thanks, although I still have a couple of questions:

1. What is the line parameter? I could not find it in the manual...

2. How does exactly work the adj parameter when giving two different values?
Reply | Threaded
Open this post in threaded view
|

Re: Positioning text in top left corner of plot

Benno Pütz
Both are described in the documentation (?(m)text works for me):

Usage

mtext(text, side = 3, line = 0, outer = FALSE, at = NA,
      adj = NA, padj = NA, cex = NA, col = NA, font = NA, ...)

Arguments


text a character or expression vector specifying the text to be written. Other objects are coerced by as.graphicsAnnot.
side on which side of the plot (1=bottom, 2=left, 3=top, 4=right).
line on which MARgin line, starting at 0 counting outwards.
[…]

actually, line does accept fractional values , e.g. line=.5 should work for you

and

Usage

text(x, ...)

## Default S3 method:
 text(x, y = NULL, labels = seq_along(x), adj = NULL,
     pos = NULL, offset = 0.5, vfont = NULL,
     cex = 1, col = NULL, font = NULL, ...)

Arguments

x, y numeric vectors of coordinates where the text labels should be written. If the length of x and y differs, the shorter one is recycled.
labels a character vector or expression specifying the text to be written. An attempt is made to coerce other language objects (names and calls) to expressions, and
                vectors and other classed objects to character vectors by as.character. If labels is longer than x and y, the coordinates are recycled to the length of labels.
adj one or two values in [0, 1] which specify the x (and optionally y) adjustment of the labels. On most devices values outside that interval will also work.
[…]

Hope the formatting comes out OK…

        Benno

On Feb 7, 2012, at 2:49 PM, Manta wrote:

> Thanks, although I still have a couple of questions:
>
> 1. What is the line parameter? I could not find it in the manual...
>
> 2. How does exactly work the adj parameter when giving two different values?
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Positioning-text-in-top-left-corner-of-plot-tp831723p4364757.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

Benno Pütz
Statistical Genetics
MPI of Psychiatry
Kraepelinstr. 2-10
80804 Munich, Germany
T: ++49-(0)89-306 22 222
F: ++49-(0)89-306 22 601

______________________________________________
[hidden email] mailing list
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: Positioning text in top left corner of plot

Manta
Thanks, mtext with option 'line' does the trick. I could not see that option in my version of R, but it appeared when I installed it again.
Reply | Threaded
Open this post in threaded view
|

Re: Positioning text in top left corner of plot

glsnow
In reply to this post by Manta
You might want to look at the grconvertX and grconvertY functions.
You can use them to convert a coordinate relative to the plotting
device (screen, paper) or plotting region to user coordinates to use
with text or mtext.

On Tue, Feb 7, 2012 at 4:06 AM, Manta <[hidden email]> wrote:

> Dear all,
>
> another questions related to zoo plotting. I would like to do as in the
> subject. Here a reproducible code:
>
> library(zoo)
> par(mfrow=c(2,1)
> plot(zoo(seq(1:10),as.Date(seq(1:10),origin="1970-01-01")),xlab="",ylab="",main="Value",las=1)
> mtext("EUR billions",adj=0,cex=0.7)
> plot(zoo(seq(1:10),as.Date(seq(1:10),origin="1970-01-01")),xlab="",ylab="",main="Value",las=1)
> par(xpd=T)
> text(par("usr")[1],par("usr")[3]+10.5,"EUR billions",cex=0.7)
>
> In the first graph I use the mtext function, which does the trick but it
> places the text too close too the y-axis.
> A second possibility is to use the text function, prior specification of the
> option parameter par(xpd=T). However, this does not look consistent as,
> depending on the graph, I would need to add different numbers in the y
> specification of the axis (par("usr")[3]+XXX).
>
> Any idea?
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Positioning-text-in-top-left-corner-of-plot-tp831723p4364355.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[hidden email] mailing list
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.