This is beautiful. Thanks! G -----Original Message----- From: Rui Barradas <ruipbarra...@sapo.pt> Sent: Thursday, February 18, 2021 2:10 PM To: Izmirlian, Grant (NIH/NCI) [E] <izmir...@mail.nih.gov>; r-help@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: > ## 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@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. > ______________________________________________ R-help@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.