[R] Computing line= for mtext

Frank Harrell f.harrell at vanderbilt.edu
Sun Mar 4 04:20:02 CET 2012


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 <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>
>>>> 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.
>>
>> ______________________________________________
>> 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.
> 
> 
> 
> -- 
> Gregory (Greg) L. Snow Ph.D.
> 538280@
> 
> ______________________________________________
> 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-tp4431554p4442874.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list