[R] formatting expressoion(paste with line shift

Duncan Murdoch murdoch.duncan at gmail.com
Fri Feb 26 13:47:59 CET 2016


On 26/02/2016 7:37 AM, Duncan Murdoch wrote:
> On 26/02/2016 7:08 AM, Troels Ring wrote:
>> (pHi <- seq(1,8))
>> #This gets formatted well but I want subscript for 2 in pCO2
>>     plot(pHi,type="s",axes=FALSE,xlab="",lwd=4,
>> main=paste("Theoretical experiment using SID = 0.13 M\n"," ATOT = 0.2 M,
>> pKa = 6.8, and pCO[2] = 40"))
>>
>> #but this gets a very unhelpful format
>>
>> plot(pHi,type="s",axes=FALSE,xlab="",lwd=4,
>> main=expression(paste("Theoretical experiment using ", pCO[2], "= 40,
>> SID = 0.13 M\n"," ATOT = 0.2 M, and pKa = 6.8",
>>     )))
>
> As the docs say, control chars like \n are ignored in plotmath.  You can
> probably put something together using atop(), though the resizing will
> be problematic.  I'd suggest that you just use mtext() to write each of
> the three lines of output:
>
> plot(pHi,type="s",axes=FALSE,xlab="",lwd=4,
> main="")
> mtext(expression(paste("Theoretical experiment using ", pCO[2], "=
> 40")), 3, line = 3)
> mtext("SID = 0.13 M", 3, line = 2)
> mtext("ATOT = 0.2 M and pKa = 6.8", 3, line = 1)
>
> You can move things around by changing the "line = " args, and add cex
> if you want bigger text, etc.
>

One thing I forgot to add:  if you want to match the look to other 
plots, see ?title.  The default settings for the main title are 
par(c("font.main", "cex.main", "col.main")).

Duncan Murdoch



More information about the R-help mailing list