[R] New line in caption with math symbols embedded in expression (paste(

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Thu Feb 18 19:42:09 CET 2021


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 using 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
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list