Computing line= for mtext

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Computing line= for mtext

Frank Harrell
I want to right-justify a vector of numbers in the right margin of a low-level plot.  For this I need to compute the line parameter to give to mtext.  Is this the correct scalable calculation?

par(mar=c(4,3,1,5)); plot(1:20)
s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
 
Thanks
Frank
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: Computing line= for mtext

Richard M. Heiberger
Frank,

This can be done directly with a variant of the panel.axis function.
See function panel.axis.right in the HH package.  This was provided for me
by David Winsemius in response to my query on this list in October 2011
https://stat.ethz.ch/pipermail/r-help/2011-October/292806.html

The email thread also includes comments by Deepayan Sarkar and Paul Murrell.

Rich

On Wed, Feb 29, 2012 at 8:48 AM, Frank Harrell <[hidden email]>wrote:

> I want to right-justify a vector of numbers in the right margin of a
> low-level plot.  For this I need to compute the line parameter to give to
> mtext.  Is this the correct scalable calculation?
>
> par(mar=c(4,3,1,5)); plot(1:20)
> s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
> mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
> mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
>
> Thanks
> Frank
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4431554.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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[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: Computing line= for mtext

Frank Harrell
Rich's pointers deals with lattice/grid graphics.  Does anyone have a solution for base graphics?
Thanks
Frank
Richard M. Heiberger wrote
Frank,

This can be done directly with a variant of the panel.axis function.
See function panel.axis.right in the HH package.  This was provided for me
by David Winsemius in response to my query on this list in October 2011
https://stat.ethz.ch/pipermail/r-help/2011-October/292806.html

The email thread also includes comments by Deepayan Sarkar and Paul Murrell.

Rich

On Wed, Feb 29, 2012 at 8:48 AM, Frank Harrell <[hidden email]>wrote:

> I want to right-justify a vector of numbers in the right margin of a
> low-level plot.  For this I need to compute the line parameter to give to
> mtext.  Is this the correct scalable calculation?
>
> par(mar=c(4,3,1,5)); plot(1:20)
> s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
> mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
> mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
>
> Thanks
> Frank
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4431554.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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[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.
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: Computing line= for mtext

plangfelder
Frank,

maybe I'm not understanding something right... you say right-justify
in the right margin, meaning justify against the very right edge of
the plot margin area? Since you're setting your right margin to 5, why
not just

mtext(s, side=4, las=1, at=5, adj=1, line = 5, cex=1)
mtext(s, side=4, las=1, at=7, adj=1, line = 5,  cex=2)

i.e. set the line argument to the right margin?

Peter

On Thu, Mar 1, 2012 at 3:52 PM, Frank Harrell <[hidden email]> wrote:

> Rich's pointers deals with lattice/grid graphics.  Does anyone have a
> solution for base graphics?
> Thanks
> Frank
>
> Richard M. Heiberger wrote
>>
>> Frank,
>>
>> This can be done directly with a variant of the panel.axis function.
>> See function panel.axis.right in the HH package.  This was provided for me
>> by David Winsemius in response to my query on this list in October 2011
>> https://stat.ethz.ch/pipermail/r-help/2011-October/292806.html
>>
>> The email thread also includes comments by Deepayan Sarkar and Paul
>> Murrell.
>>
>> Rich
>>
>> On Wed, Feb 29, 2012 at 8:48 AM, Frank Harrell &lt;f.harrell@&gt;wrote:
>>
>>> I want to right-justify a vector of numbers in the right margin of a
>>> low-level plot.  For this I need to compute the line parameter to give to
>>> mtext.  Is this the correct scalable calculation?
>>>
>>> par(mar=c(4,3,1,5)); plot(1:20)
>>> s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
>>> mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
>>> mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
>>>
>>> Thanks
>>> Frank
>>>
>>> -----
>>> Frank Harrell
>>> Department of Biostatistics, Vanderbilt University
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4431554.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help@ mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt;
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@ 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.
>>
>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context: http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4436923.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.

______________________________________________
[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: Computing line= for mtext

Richard M. Heiberger
In reply to this post by Frank Harrell
Frank,

this is it.  It uses Peter's idea.

 plot(1:10)
 axis(side=2, 1:10, las=1, line=-31.5, lwd=0)
 axis(side=4, 1:10, las=1, labels=FALSE)

Rich


On Thu, Mar 1, 2012 at 6:52 PM, Frank Harrell <[hidden email]>wrote:

> Rich's pointers deals with lattice/grid graphics.  Does anyone have a
> solution for base graphics?
> Thanks
> Frank
>
> Richard M. Heiberger wrote
> >
> > Frank,
> >
> > This can be done directly with a variant of the panel.axis function.
> > See function panel.axis.right in the HH package.  This was provided for
> me
> > by David Winsemius in response to my query on this list in October 2011
> > https://stat.ethz.ch/pipermail/r-help/2011-October/292806.html
> >
> > The email thread also includes comments by Deepayan Sarkar and Paul
> > Murrell.
> >
> > Rich
> >
> > On Wed, Feb 29, 2012 at 8:48 AM, Frank Harrell &lt;f.harrell@&gt;wrote:
> >
> >> I want to right-justify a vector of numbers in the right margin of a
> >> low-level plot.  For this I need to compute the line parameter to give
> to
> >> mtext.  Is this the correct scalable calculation?
> >>
> >> par(mar=c(4,3,1,5)); plot(1:20)
> >> s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
> >> mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
> >> mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
> >>
> >> Thanks
> >> Frank
> >>
> >> -----
> >> Frank Harrell
> >> Department of Biostatistics, Vanderbilt University
> >> --
> >> View this message in context:
> >>
> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4431554.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> R-help@ mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >>
> http://www.R-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt<http://www.r-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt>
> ;
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@ mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4436923.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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[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: Computing line= for mtext

Frank Harrell
Hi Rich and Peter,

What I am trying to do is the right-justify a vector of numbers to the right of the y-axis so that the leftmost digit of all of the numbers is one character to the right of the axis line.  axis() plots tick marks and left-justifies the numbers.

Peter's idea:
---------
Since you're setting your right margin to 5, why
not just

mtext(s, side=4, las=1, at=5, adj=1, line = 5, cex=1)
mtext(s, side=4, las=1, at=7, adj=1, line = 5,  cex=2)

i.e. set the line argument to the right margin?
---------
comes close to what I'm trying to do but I haven't found out how to compute "line" from the maximum width over all the strings in the vector being plotted vertically, and I'm not sure it scales properly for different cex=.  strwidth(s, units='inches')/par('cin')[1] doesn't seem to be a complete solution.

Thanks
Frank

Richard M. Heiberger wrote
Frank,

this is it.  It uses Peter's idea.

 plot(1:10)
 axis(side=2, 1:10, las=1, line=-31.5, lwd=0)
 axis(side=4, 1:10, las=1, labels=FALSE)

Rich


On Thu, Mar 1, 2012 at 6:52 PM, Frank Harrell <[hidden email]>wrote:

> Rich's pointers deals with lattice/grid graphics.  Does anyone have a
> solution for base graphics?
> Thanks
> Frank
>
> Richard M. Heiberger wrote
> >
> > Frank,
> >
> > This can be done directly with a variant of the panel.axis function.
> > See function panel.axis.right in the HH package.  This was provided for
> me
> > by David Winsemius in response to my query on this list in October 2011
> > https://stat.ethz.ch/pipermail/r-help/2011-October/292806.html
> >
> > The email thread also includes comments by Deepayan Sarkar and Paul
> > Murrell.
> >
> > Rich
> >
> > On Wed, Feb 29, 2012 at 8:48 AM, Frank Harrell <f.harrell@>wrote:
> >
> >> I want to right-justify a vector of numbers in the right margin of a
> >> low-level plot.  For this I need to compute the line parameter to give
> to
> >> mtext.  Is this the correct scalable calculation?
> >>
> >> par(mar=c(4,3,1,5)); plot(1:20)
> >> s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
> >> mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
> >> mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
> >>
> >> Thanks
> >> Frank
> >>
> >> -----
> >> Frank Harrell
> >> Department of Biostatistics, Vanderbilt University
> >> --
> >> View this message in context:
> >>
> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4431554.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> R-help@ mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >>
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html><http://www.r-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt>
> ;
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@ mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4436923.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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[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.
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: Computing line= for mtext

ilai-2
On Fri, Mar 2, 2012 at 1:17 PM, Frank Harrell <[hidden email]> wrote:
> Hi Rich and Peter,
>
> What I am trying to do is the right-justify a vector of numbers to the right
> of the y-axis so that the leftmost digit of all of the numbers is one
> character to the right of the axis line.  axis() plots tick marks and

No it doesn't (not always)

> left-justifies the numbers.

For axis(4). If you really want right justify on the right hand side,
you could use axis(2) with negative line numbers (device size
dependent). Or you could try something like

plot(1:20)
axis(2,at=seq(1,20,4),labels=T,tick=F,las=T,pos=c(22.25,1))

Hope this is getting there (after only 6 messages...)

Elai

>
> Peter's idea:
> ---------
> Since you're setting your right margin to 5, why
> not just
>
> mtext(s, side=4, las=1, at=5, adj=1, line = 5, cex=1)
> mtext(s, side=4, las=1, at=7, adj=1, line = 5,  cex=2)
>
> i.e. set the line argument to the right margin?
> ---------
> comes close to what I'm trying to do but I haven't found out how to compute
> "line" from the maximum width over all the strings in the vector being
> plotted vertically, and I'm not sure it scales properly for different cex=.
> strwidth(s, units='inches')/par('cin')[1] doesn't seem to be a complete
> solution.
>
> Thanks
> Frank
>
>
> Richard M. Heiberger wrote
>>
>> Frank,
>>
>> this is it.  It uses Peter's idea.
>>
>>  plot(1:10)
>>  axis(side=2, 1:10, las=1, line=-31.5, lwd=0)
>>  axis(side=4, 1:10, las=1, labels=FALSE)
>>
>> Rich
>>
>>
>> On Thu, Mar 1, 2012 at 6:52 PM, Frank Harrell &lt;f.harrell@&gt;wrote:
>>
>>> Rich's pointers deals with lattice/grid graphics.  Does anyone have a
>>> solution for base graphics?
>>> Thanks
>>> Frank
>>>
>>> Richard M. Heiberger wrote
>>> >
>>> > Frank,
>>> >
>>> > This can be done directly with a variant of the panel.axis function.
>>> > See function panel.axis.right in the HH package.  This was provided for
>>> me
>>> > by David Winsemius in response to my query on this list in October 2011
>>> > https://stat.ethz.ch/pipermail/r-help/2011-October/292806.html
>>> >
>>> > The email thread also includes comments by Deepayan Sarkar and Paul
>>> > Murrell.
>>> >
>>> > Rich
>>> >
>>> > On Wed, Feb 29, 2012 at 8:48 AM, Frank Harrell &lt;f.harrell@&gt;wrote:
>>> >
>>> >> I want to right-justify a vector of numbers in the right margin of a
>>> >> low-level plot.  For this I need to compute the line parameter to give
>>> to
>>> >> mtext.  Is this the correct scalable calculation?
>>> >>
>>> >> par(mar=c(4,3,1,5)); plot(1:20)
>>> >> s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
>>> >> mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
>>> >> mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
>>> >>
>>> >> Thanks
>>> >> Frank
>>> >>
>>> >> -----
>>> >> Frank Harrell
>>> >> Department of Biostatistics, Vanderbilt University
>>> >> --
>>> >> View this message in context:
>>> >>
>>> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4431554.html
>>> >> Sent from the R help mailing list archive at Nabble.com.
>>> >>
>>> >> ______________________________________________
>>> >> R-help@ mailing list
>>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>>> >> PLEASE do read the posting guide
>>> >>
>>> http://www.R-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt&lt;http://www.r-project.org/posting-guide.html&amp;lt;http://www.r-project.org/posting-guide.html&amp;gt&gt;
>>> ;
>>> >> and provide commented, minimal, self-contained, reproducible code.
>>> >>
>>> >
>>> >       [[alternative HTML version deleted]]
>>> >
>>> > ______________________________________________
>>> > R-help@ mailing list
>>> > https://stat.ethz.ch/mailman/listinfo/r-help
>>> > PLEASE do read the posting guide
>>> >
>>> http://www.R-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt;
>>> > and provide commented, minimal, self-contained, reproducible code.
>>> >
>>>
>>>
>>> -----
>>> Frank Harrell
>>> Department of Biostatistics, Vanderbilt University
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4436923.html
>>>  Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help@ mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt;
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@ 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.
>>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context: http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4439703.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.

______________________________________________
[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: Computing line= for mtext

Frank Harrell
Thanks Elai.  axis(2) looks like a good approach.   I think the way to solve for the pos= argument is to use:

usr <- par('usr'); plt <- par('plt')
usr[2] + (usr[2] - usr[1])/(plt[2] - plt[1]) *  (1 - plt[2])

I think pos should have only one element.

Thanks for your help,
Frank

ilai-2 wrote
On Fri, Mar 2, 2012 at 1:17 PM, Frank Harrell <[hidden email]> wrote:
> Hi Rich and Peter,
>
> What I am trying to do is the right-justify a vector of numbers to the right
> of the y-axis so that the leftmost digit of all of the numbers is one
> character to the right of the axis line.  axis() plots tick marks and

No it doesn't (not always)

> left-justifies the numbers.

For axis(4). If you really want right justify on the right hand side,
you could use axis(2) with negative line numbers (device size
dependent). Or you could try something like

plot(1:20)
axis(2,at=seq(1,20,4),labels=T,tick=F,las=T,pos=c(22.25,1))

Hope this is getting there (after only 6 messages...)

Elai

>
> Peter's idea:
> ---------
> Since you're setting your right margin to 5, why
> not just
>
> mtext(s, side=4, las=1, at=5, adj=1, line = 5, cex=1)
> mtext(s, side=4, las=1, at=7, adj=1, line = 5,  cex=2)
>
> i.e. set the line argument to the right margin?
> ---------
> comes close to what I'm trying to do but I haven't found out how to compute
> "line" from the maximum width over all the strings in the vector being
> plotted vertically, and I'm not sure it scales properly for different cex=.
> strwidth(s, units='inches')/par('cin')[1] doesn't seem to be a complete
> solution.
>
> Thanks
> Frank
>
>
> Richard M. Heiberger wrote
>>
>> Frank,
>>
>> this is it.  It uses Peter's idea.
>>
>>  plot(1:10)
>>  axis(side=2, 1:10, las=1, line=-31.5, lwd=0)
>>  axis(side=4, 1:10, las=1, labels=FALSE)
>>
>> Rich
>>
>>
>> On Thu, Mar 1, 2012 at 6:52 PM, Frank Harrell <f.harrell@>wrote:
>>
>>> Rich's pointers deals with lattice/grid graphics.  Does anyone have a
>>> solution for base graphics?
>>> Thanks
>>> Frank
>>>
>>> Richard M. Heiberger wrote
>>> >
>>> > Frank,
>>> >
>>> > This can be done directly with a variant of the panel.axis function.
>>> > See function panel.axis.right in the HH package.  This was provided for
>>> me
>>> > by David Winsemius in response to my query on this list in October 2011
>>> > https://stat.ethz.ch/pipermail/r-help/2011-October/292806.html
>>> >
>>> > The email thread also includes comments by Deepayan Sarkar and Paul
>>> > Murrell.
>>> >
>>> > Rich
>>> >
>>> > On Wed, Feb 29, 2012 at 8:48 AM, Frank Harrell <f.harrell@>wrote:
>>> >
>>> >> I want to right-justify a vector of numbers in the right margin of a
>>> >> low-level plot.  For this I need to compute the line parameter to give
>>> to
>>> >> mtext.  Is this the correct scalable calculation?
>>> >>
>>> >> par(mar=c(4,3,1,5)); plot(1:20)
>>> >> s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
>>> >> mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
>>> >> mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
>>> >>
>>> >> Thanks
>>> >> Frank
>>> >>
>>> >> -----
>>> >> Frank Harrell
>>> >> Department of Biostatistics, Vanderbilt University
>>> >> --
>>> >> View this message in context:
>>> >>
>>> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4431554.html
>>> >> Sent from the R help mailing list archive at Nabble.com.
>>> >>
>>> >> ______________________________________________
>>> >> R-help@ mailing list
>>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>>> >> PLEASE do read the posting guide
>>> >>
>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html><http://www.r-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt>
>>> ;
>>> >> and provide commented, minimal, self-contained, reproducible code.
>>> >>
>>> >
>>> >       [[alternative HTML version deleted]]
>>> >
>>> > ______________________________________________
>>> > R-help@ mailing list
>>> > https://stat.ethz.ch/mailman/listinfo/r-help
>>> > PLEASE do read the posting guide
>>> >
>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>>> > and provide commented, minimal, self-contained, reproducible code.
>>> >
>>>
>>>
>>> -----
>>> Frank Harrell
>>> Department of Biostatistics, Vanderbilt University
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4436923.html
>>>  Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help@ mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@ 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.
>>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context: http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4439703.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.

______________________________________________
[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.
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: Computing line= for mtext

baptiste auguie-5
In reply to this post by Frank Harrell
Hi,

If you're going to use different text sizes and convert between units,
it might be easier to do the calculations with grid.

par(mar=c(1,1,1,5))
plot(1:10)
labels = c(1, 2, 10, 123, 3.141592653589, 1.2, 2)
sizes = c(1, 1, 2, 1, 0.4, 1, 3) # cex of individual labels

## pure base graphics
max_width_base = do.call(max, mapply(function(l, size) strwidth(l,
cex=size, units = "inches"), l=labels, size=sizes, SIMPLIFY=FALSE))

## calculations with grid graphics
max_width_grid = grid::convertUnit(do.call(max, mapply(function(l,
size) grid::grobWidth(grid::textGrob(l, gp=grid::gpar(cex=size))),
l=labels, size=sizes, SIMPLIFY=FALSE)), "in", valueOnly=TRUE)

all.equal(max_width_base, max_width_grid)

## add one line
final_width = grid::convertUnit( grid::unit(max_width_base,"in") +
                                         grid::unit(1,"lines"),
"lines", valueOnly=TRUE)

mapply(function(l, size, ii) mtext(l, side=4, at=ii, las=1, adj=1,
line=final_width, cex=size), l=labels, size=sizes,
ii=seq_along(labels)) -> b.quiet


HTH,

b.

On 3 March 2012 09:17, Frank Harrell <[hidden email]> wrote:

> Hi Rich and Peter,
>
> What I am trying to do is the right-justify a vector of numbers to the right
> of the y-axis so that the leftmost digit of all of the numbers is one
> character to the right of the axis line.  axis() plots tick marks and
> left-justifies the numbers.
>
> Peter's idea:
> ---------
> Since you're setting your right margin to 5, why
> not just
>
> mtext(s, side=4, las=1, at=5, adj=1, line = 5, cex=1)
> mtext(s, side=4, las=1, at=7, adj=1, line = 5,  cex=2)
>
> i.e. set the line argument to the right margin?
> ---------
> comes close to what I'm trying to do but I haven't found out how to compute
> "line" from the maximum width over all the strings in the vector being
> plotted vertically, and I'm not sure it scales properly for different cex=.
> strwidth(s, units='inches')/par('cin')[1] doesn't seem to be a complete
> solution.
>
> Thanks
> Frank
>
>
> Richard M. Heiberger wrote
>>
>> Frank,
>>
>> this is it.  It uses Peter's idea.
>>
>>  plot(1:10)
>>  axis(side=2, 1:10, las=1, line=-31.5, lwd=0)
>>  axis(side=4, 1:10, las=1, labels=FALSE)
>>
>> Rich
>>
>>
>> On Thu, Mar 1, 2012 at 6:52 PM, Frank Harrell &lt;f.harrell@&gt;wrote:
>>
>>> Rich's pointers deals with lattice/grid graphics.  Does anyone have a
>>> solution for base graphics?
>>> Thanks
>>> Frank
>>>
>>> Richard M. Heiberger wrote
>>> >
>>> > Frank,
>>> >
>>> > This can be done directly with a variant of the panel.axis function.
>>> > See function panel.axis.right in the HH package.  This was provided for
>>> me
>>> > by David Winsemius in response to my query on this list in October 2011
>>> > https://stat.ethz.ch/pipermail/r-help/2011-October/292806.html
>>> >
>>> > The email thread also includes comments by Deepayan Sarkar and Paul
>>> > Murrell.
>>> >
>>> > Rich
>>> >
>>> > On Wed, Feb 29, 2012 at 8:48 AM, Frank Harrell &lt;f.harrell@&gt;wrote:
>>> >
>>> >> I want to right-justify a vector of numbers in the right margin of a
>>> >> low-level plot.  For this I need to compute the line parameter to give
>>> to
>>> >> mtext.  Is this the correct scalable calculation?
>>> >>
>>> >> par(mar=c(4,3,1,5)); plot(1:20)
>>> >> s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
>>> >> mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
>>> >> mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
>>> >>
>>> >> Thanks
>>> >> Frank
>>> >>
>>> >> -----
>>> >> Frank Harrell
>>> >> Department of Biostatistics, Vanderbilt University
>>> >> --
>>> >> View this message in context:
>>> >>
>>> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4431554.html
>>> >> Sent from the R help mailing list archive at Nabble.com.
>>> >>
>>> >> ______________________________________________
>>> >> R-help@ mailing list
>>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>>> >> PLEASE do read the posting guide
>>> >>
>>> http://www.R-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt&lt;http://www.r-project.org/posting-guide.html&amp;lt;http://www.r-project.org/posting-guide.html&amp;gt&gt;
>>> ;
>>> >> and provide commented, minimal, self-contained, reproducible code.
>>> >>
>>> >
>>> >       [[alternative HTML version deleted]]
>>> >
>>> > ______________________________________________
>>> > R-help@ mailing list
>>> > https://stat.ethz.ch/mailman/listinfo/r-help
>>> > PLEASE do read the posting guide
>>> >
>>> http://www.R-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt;
>>> > and provide commented, minimal, self-contained, reproducible code.
>>> >
>>>
>>>
>>> -----
>>> Frank Harrell
>>> Department of Biostatistics, Vanderbilt University
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4436923.html
>>>  Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help@ mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt;
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@ 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.
>>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context: http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4439703.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.

______________________________________________
[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: Computing line= for mtext

glsnow
In reply to this post by Frank Harrell
I would use the regular text function instead of mtext (remembering to
set par(xpd=...)), then use the grconvertX and grconvertY functions to
find the location to plot at (possibly adding in the results from
strwidth or stheight).

On Thu, Mar 1, 2012 at 4:52 PM, Frank Harrell <[hidden email]> wrote:

> Rich's pointers deals with lattice/grid graphics.  Does anyone have a
> solution for base graphics?
> Thanks
> Frank
>
> Richard M. Heiberger wrote
>>
>> Frank,
>>
>> This can be done directly with a variant of the panel.axis function.
>> See function panel.axis.right in the HH package.  This was provided for me
>> by David Winsemius in response to my query on this list in October 2011
>> https://stat.ethz.ch/pipermail/r-help/2011-October/292806.html
>>
>> The email thread also includes comments by Deepayan Sarkar and Paul
>> Murrell.
>>
>> Rich
>>
>> On Wed, Feb 29, 2012 at 8:48 AM, Frank Harrell &lt;f.harrell@&gt;wrote:
>>
>>> I want to right-justify a vector of numbers in the right margin of a
>>> low-level plot.  For this I need to compute the line parameter to give to
>>> mtext.  Is this the correct scalable calculation?
>>>
>>> par(mar=c(4,3,1,5)); plot(1:20)
>>> s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
>>> mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
>>> mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
>>>
>>> Thanks
>>> Frank
>>>
>>> -----
>>> Frank Harrell
>>> Department of Biostatistics, Vanderbilt University
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4431554.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help@ mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt;
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@ 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.
>>
>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context: http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4436923.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.
Reply | Threaded
Open this post in threaded view
|

Re: Computing line= for mtext

Frank Harrell
Thanks very much for the ideas Baptiste and Greg.  I think this is a way to go:

# Right justifies (if adj=1) a vector of strings against the right margin (side=4) or against the y-axis (side=2)
outerText <-
  function(string, y, side=4, cex=par('cex'), adj=1, ...) {
    if(side %nin% c(2,4)) stop('only works for side=2 or 4')
    x <- if(side==4) grconvertX(1, from='nfc', to='user') else par('usr')[1]
    text(x, y, paste(string,''), cex=cex, adj=adj, xpd=NA)
}    

Frank


Greg Snow wrote
I would use the regular text function instead of mtext (remembering to
set par(xpd=...)), then use the grconvertX and grconvertY functions to
find the location to plot at (possibly adding in the results from
strwidth or stheight).

On Thu, Mar 1, 2012 at 4:52 PM, Frank Harrell <[hidden email]> wrote:
> Rich's pointers deals with lattice/grid graphics.  Does anyone have a
> solution for base graphics?
> Thanks
> Frank
>
> Richard M. Heiberger wrote
>>
>> Frank,
>>
>> This can be done directly with a variant of the panel.axis function.
>> See function panel.axis.right in the HH package.  This was provided for me
>> by David Winsemius in response to my query on this list in October 2011
>> https://stat.ethz.ch/pipermail/r-help/2011-October/292806.html
>>
>> The email thread also includes comments by Deepayan Sarkar and Paul
>> Murrell.
>>
>> Rich
>>
>> On Wed, Feb 29, 2012 at 8:48 AM, Frank Harrell <f.harrell@>wrote:
>>
>>> I want to right-justify a vector of numbers in the right margin of a
>>> low-level plot.  For this I need to compute the line parameter to give to
>>> mtext.  Is this the correct scalable calculation?
>>>
>>> par(mar=c(4,3,1,5)); plot(1:20)
>>> s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
>>> mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
>>> mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
>>>
>>> Thanks
>>> Frank
>>>
>>> -----
>>> Frank Harrell
>>> Department of Biostatistics, Vanderbilt University
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4431554.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help@ mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@ 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.
>>
>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context: http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4436923.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.
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: Computing line= for mtext

Frank Harrell
Whoops.  I spoke too soon.  That last approach does not survive shrinking the graphics window.  I'll go back to the axis and par approach.
Frank

Frank Harrell wrote
Thanks very much for the ideas Baptiste and Greg.  I think this is a way to go:

# Right justifies (if adj=1) a vector of strings against the right margin (side=4) or against the y-axis (side=2)
outerText <-
  function(string, y, side=4, cex=par('cex'), adj=1, ...) {
    if(side %nin% c(2,4)) stop('only works for side=2 or 4')
    x <- if(side==4) grconvertX(1, from='nfc', to='user') else par('usr')[1]
    text(x, y, paste(string,''), cex=cex, adj=adj, xpd=NA)
}    

Frank


Greg Snow wrote
I would use the regular text function instead of mtext (remembering to
set par(xpd=...)), then use the grconvertX and grconvertY functions to
find the location to plot at (possibly adding in the results from
strwidth or stheight).

On Thu, Mar 1, 2012 at 4:52 PM, Frank Harrell <[hidden email]> wrote:
> Rich's pointers deals with lattice/grid graphics.  Does anyone have a
> solution for base graphics?
> Thanks
> Frank
>
> Richard M. Heiberger wrote
>>
>> Frank,
>>
>> This can be done directly with a variant of the panel.axis function.
>> See function panel.axis.right in the HH package.  This was provided for me
>> by David Winsemius in response to my query on this list in October 2011
>> https://stat.ethz.ch/pipermail/r-help/2011-October/292806.html
>>
>> The email thread also includes comments by Deepayan Sarkar and Paul
>> Murrell.
>>
>> Rich
>>
>> On Wed, Feb 29, 2012 at 8:48 AM, Frank Harrell <f.harrell@>wrote:
>>
>>> I want to right-justify a vector of numbers in the right margin of a
>>> low-level plot.  For this I need to compute the line parameter to give to
>>> mtext.  Is this the correct scalable calculation?
>>>
>>> par(mar=c(4,3,1,5)); plot(1:20)
>>> s <- 'abcde'; w=strwidth(s, units='inches')/par('cin')[1]
>>> mtext(s, side=4, las=1, at=5, adj=1, line=w-.5, cex=1)
>>> mtext(s, side=4, las=1, at=7, adj=1, line=2*(w-.5), cex=2)
>>>
>>> Thanks
>>> Frank
>>>
>>> -----
>>> Frank Harrell
>>> Department of Biostatistics, Vanderbilt University
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4431554.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help@ mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@ 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.
>>
>
>
> -----
> Frank Harrell
> Department of Biostatistics, Vanderbilt University
> --
> View this message in context: http://r.789695.n4.nabble.com/Computing-line-for-mtext-tp4431554p4436923.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.
Frank Harrell
Department of Biostatistics, Vanderbilt University