Skip to content

New line in caption with math symbols embedded in expression (paste(

8 messages · Bert Gunter, Rui Barradas, Izmirlian, Grant (NIH/NCI) [E]

#
## I am using ggplot and trying to produce a caption containing math symbols. I need to
## add a second line. I did a fair amount of googling for answers. This one seemed like
## it would answer my question as it is nearly exactly my problem, except there is only
## one argument to the paste function. Note that my example is a complete minimal
## example, just a scatterplot with a line, and the caption content seems to not have
## anything to do with the plot. That is of course, intentional.
##
## https://stackoverflow.com/questions/13223846/ggplot2-two-line-label-with-expression

library(ggplot2)
X <- 10*runif(100)
Y <- 2*X + rnorm(100, sd=2)
fit <- lm(Y~X)
Y.p <- predict(fit, newdata=data.frame(X=X))
DAT <- data.frame(X=X)

## without a newline
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
                        "for 'FDR' and 'Auto' FDP control method, vs '", m,
                        "' at levels of 'eff size' (col) and '", p[1], "' (row)")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## newline, method 1, just add a new component of paste containing "\n" somewhere in the middle
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
                        "for 'FDR' and 'Auto' FDP control method, vs '", m,
                        "' at levels of 'eff size' (col) and '", p[1], "' (row)",
                        "and \n a new line")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## doesn't work because the newline affects only the last component of paste. It looks like new line
## works if the line is long enough, but the newline character is being parsed until the individual
## arguments to paste are parsed for math symbols but prior to pasting the components together. prior
## to pasting all arguments. I can't imagine why this would be the desired behavior. When the value of a
## caption argument is expression(paste(s1, s2, s3, ...)) then parsing for a newline character should
## occur after the components are pasted together, right?

## newline, method 2, enclose paste in another paste and add the new line as the second argument of the
## outer paste
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(paste(P,"(",FDP,">", alpha,") ",
                        "for 'FDR' and 'Auto' FDP control method, vs '", m,
                        "' at levels of 'eff size' (col) and '", p[1], "' (row)"),
                        "and \n a new line")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## which has the same behavior
#
Note, from ?plotmath:

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

For this reason, as best I can tell, you need to fool with plotmath's
"atop" command or do separate "labs" calls.  This post seems to confirm
that opinion:

https://stackoverflow.com/questions/29112697/adding-a-newline-in-a-substitute-expression

I certainly would welcome a better alternative.

Cheers,
Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Feb 18, 2021 at 9:37 AM Izmirlian, Grant (NIH/NCI) [E] via R-help <
r-help at r-project.org> wrote:

            

  
  
#
See also this:

https://cran.r-project.org/web/packages/latex2exp/vignettes/using-latex2exp.html


Bert
On Thu, Feb 18, 2021 at 10:42 AM Bert Gunter <bgunter.4567 at gmail.com> wrote:

            

  
  
#
Thank you for your suggestions. So you?re suggesting I bypass the ggplot symbol parsing by passing a character string to caption which has latex2exp in it. Good idea. So in this approach, I should break the string into new lines via ?atop??
Thanks


From: Bert Gunter <bgunter.4567 at gmail.com>
Sent: Thursday, February 18, 2021 1:47 PM
To: Izmirlian, Grant (NIH/NCI) [E] <izmirlig at mail.nih.gov>
Cc: r-help at r-project.org
Subject: Re: [R] New line in caption with math symbols embedded in expression (paste(

See also this:

https://cran.r-project.org/web/packages/latex2exp/vignettes/using-latex2exp.html


Bert
On Thu, Feb 18, 2021 at 10:42 AM Bert Gunter <bgunter.4567 at gmail.com<mailto:bgunter.4567 at gmail.com>> wrote:
Note, from ?plotmath:

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

For this reason, as best I can tell, you need to fool with plotmath's "atop" command or do separate "labs" calls.  This post seems to confirm that opinion:

https://stackoverflow.com/questions/29112697/adding-a-newline-in-a-substitute-expression

I certainly would welcome a better alternative.

Cheers,
Bert Gunter

"The trouble with having an open mind is that people keep coming along and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Thu, Feb 18, 2021 at 9:37 AM Izmirlian, Grant (NIH/NCI) [E] via R-help <r-help at r-project.org<mailto:r-help at r-project.org>> wrote:
## I am using ggplot and trying to produce a caption containing math symbols. I need to
## add a second line. I did a fair amount of googling for answers. This one seemed like
## it would answer my question as it is nearly exactly my problem, except there is only
## one argument to the paste function. Note that my example is a complete minimal
## example, just a scatterplot with a line, and the caption content seems to not have
## anything to do with the plot. That is of course, intentional.
##
## https://stackoverflow.com/questions/13223846/ggplot2-two-line-label-with-expression

library(ggplot2)
X <- 10*runif(100)
Y <- 2*X + rnorm(100, sd=2)
fit <- lm(Y~X)
Y.p <- predict(fit, newdata=data.frame(X=X))
DAT <- data.frame(X=X)

## without a newline
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
                        "for 'FDR' and 'Auto' FDP control method, vs '", m,
                        "' at levels of 'eff size' (col) and '", p[1], "' (row)")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## newline, method 1, just add a new component of paste containing "\n" somewhere in the middle
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
                        "for 'FDR' and 'Auto' FDP control method, vs '", m,
                        "' at levels of 'eff size' (col) and '", p[1], "' (row)",
                        "and \n a new line")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## doesn't work because the newline affects only the last component of paste. It looks like new line
## works if the line is long enough, but the newline character is being parsed until the individual
## arguments to paste are parsed for math symbols but prior to pasting the components together. prior
## to pasting all arguments. I can't imagine why this would be the desired behavior. When the value of a
## caption argument is expression(paste(s1, s2, s3, ...)) then parsing for a newline character should
## occur after the components are pasted together, right?

## newline, method 2, enclose paste in another paste and add the new line as the second argument of the
## outer paste
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(paste(P,"(",FDP,">", alpha,") ",
                        "for 'FDR' and 'Auto' FDP control method, vs '", m,
                        "' at levels of 'eff size' (col) and '", p[1], "' (row)"),
                        "and \n a new line")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## which has the same behavior



______________________________________________
R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see
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.
#
To be clear: I have tried none of this, and so cannot offer detailed useful
advice. Just passing along what I found (though I have done it with "atop"
in the dim, dark past). Let us know what works best for you if you find
something that does.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Feb 18, 2021 at 10:51 AM Izmirlian, Grant (NIH/NCI) [E] <
izmirlig at mail.nih.gov> wrote:

            

  
  
#
Hello,

First of all the plotmath in your code doesn't need paste, expression 
alone will do it.

I am not sure that the following is what you want. I create the caption 
beforehand, in order to make the plotting code simpler.
The asterisks/tildes make less/more space between the text line's elements.


e <- expression(
   atop(
     P * "(" * FDP ~ ">" ~ alpha * ") " ~
       "for 'FDR' and 'Auto' FDP control method, vs '" *
       m * "' at levels of 'eff size' (col) and '" *
       p[1] * "' (row)" ~ "and",
     "a new line" ~
       bar(x) == sum(frac(x[i], n), i==1, n)
   )
)

p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + 
geom_line(aes(y=Y.p))
p <- p + labs(caption = e)
p <- p + theme(plot.caption = element_text(hjust = 0))
p


Hope this helps,

Rui Barradas
?s 17:37 de 18/02/21, Izmirlian, Grant (NIH/NCI) [E] via R-help escreveu:
#
This is beautiful. Thanks!
G

-----Original Message-----
From: Rui Barradas <ruipbarradas at sapo.pt> 
Sent: Thursday, February 18, 2021 2:10 PM
To: Izmirlian, Grant (NIH/NCI) [E] <izmirlig at mail.nih.gov>; r-help at r-project.org
Subject: Re: [R] New line in caption with math symbols embedded in expression (paste(

Hello,

First of all the plotmath in your code doesn't need paste, expression alone will do it.

I am not sure that the following is what you want. I create the caption beforehand, in order to make the plotting code simpler.
The asterisks/tildes make less/more space between the text line's elements.


e <- expression(
   atop(
     P * "(" * FDP ~ ">" ~ alpha * ") " ~
       "for 'FDR' and 'Auto' FDP control method, vs '" *
       m * "' at levels of 'eff size' (col) and '" *
       p[1] * "' (row)" ~ "and",
     "a new line" ~
       bar(x) == sum(frac(x[i], n), i==1, n)
   )
)

p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + 
geom_line(aes(y=Y.p))
p <- p + labs(caption = e)
p <- p + theme(plot.caption = element_text(hjust = 0))
p


Hope this helps,

Rui Barradas
?s 17:37 de 18/02/21, Izmirlian, Grant (NIH/NCI) [E] via R-help escreveu:
#
You th' barradas!!!


-----Original Message-----
From: Rui Barradas <ruipbarradas at sapo.pt> 
Sent: Thursday, February 18, 2021 2:10 PM
To: Izmirlian, Grant (NIH/NCI) [E] <izmirlig at mail.nih.gov>; r-help at r-project.org
Subject: Re: [R] New line in caption with math symbols embedded in expression (paste(

Hello,

First of all the plotmath in your code doesn't need paste, expression alone will do it.

I am not sure that the following is what you want. I create the caption beforehand, in order to make the plotting code simpler.
The asterisks/tildes make less/more space between the text line's elements.


e <- expression(
   atop(
     P * "(" * FDP ~ ">" ~ alpha * ") " ~
       "for 'FDR' and 'Auto' FDP control method, vs '" *
       m * "' at levels of 'eff size' (col) and '" *
       p[1] * "' (row)" ~ "and",
     "a new line" ~
       bar(x) == sum(frac(x[i], n), i==1, n)
   )
)

p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + 
geom_line(aes(y=Y.p))
p <- p + labs(caption = e)
p <- p + theme(plot.caption = element_text(hjust = 0))
p


Hope this helps,

Rui Barradas
?s 17:37 de 18/02/21, Izmirlian, Grant (NIH/NCI) [E] via R-help escreveu: