The main title of the plot in two lines

I would like to have a title for the plot in two lines, but this does not work, why? and how can i make it work?

CVal<-1 SumEpsVal<-2 plot(1:10, main=bquote(paste("C=", .(CVal), " \n ", sum(xi), "=", .(SumEpsVal) ))) 

It works here:

 plot(1:10, main=paste("C=1", "\n", "SumXi=2")) 

I think bquote is doing something wrong ... (search? Bquote) I tried to change the environment in bqoute (where argument), but I don’t know which environment to accept.

BTW:

 plot(1:10, main=bquote(paste("C=", .(CVal), "bla \n ", sum(xi), "=", .(SumEpsVal) ))) 

does something crazy with blah.

+7
source share
3 answers

The root problem is that plotmath does not support newlines within the expression to output.

 Control characters (eg \n) are not interpreted in character strings in plotmath, unlike normal plotting. 

You really need to create and output each line separately.

For example:

 Lines <- list(bquote(paste("C=", .(CVal))), bquote(paste(sum(xi), "=", .(SumEpsVal)))) 

Now print each line. The text in the list is converted to do.call expressions

 mtext(do.call(expression, Lines),side=3,line=0:1) 

enter image description here

+10
source

Personally, I would use mtext, as already suggested. But if you really want it to be one liner, you can β€œtrick” bquote with atop :

 plot(1:10, main= bquote(atop(paste("C=",.(CVal)), paste(sum(xi),"=",.(SumEpsVal))))) 

It even aligns both lines neatly to the center.

+11
source

One way to achieve this is to use mtext to add an extra line under the main heading as follows:

 plot(1:10, main=bquote(paste("C=", .(CVal)))) mtext(bquote(paste(sum(xi), "=", .(SumEpsVal) )),side=3,line=0) 

There may be a prettier solution, but perhaps this is enough for your needs.

+4
source

All Articles