Adding a newline in a substitute() expression

PavoDive

I'm trying to annotate a plot in ggplot with relevant data from a regression model. I've followed the suggestions in this SO post and tried to modify the function to have a couple additional items in a newline in the plot. This is my attempt at a new function:

lm_eqn = function(m){
  eq <- substitute(italic(y) == a %.% italic(x)^b*","~~italic(r)^2~"="~r2*","~~italic(n)~"="~nn*","~~italic(p-value)~"="~pv, 
                   list(a = format(exp(coef(m)[1]), digits = 3), 
                        b = format(coef(m)[2], digits = 3), 
                        r2 = format(summary(m)$r.squared, digits = 3),
                        nn=format(summary(m)$df[2]),
                        pv=format(summary(m)$coefficients[,4][2])))
  as.character(as.expression(eq));                 
}

It produces the expected output: all in one line. But I'd like to split the text in two lines, the second one starting with italic(n)=. But if I introduce a \n, it throws an error when it finds \n. If I introduce the \n inside the quotes: "\n" then it seems to be ignored and the text remains in one line. I haven't found any reference as to how to introduce a newline in such an expression. Your kind help will be much appreciated.

Thanks.

EDIT: following a coment by @Tim I present a rewriten code and adjusted question.

jentjr

\n cannot be used in plotmath expressions. You could perhaps break the expression in two parts, and use annotate to add the expressions where you want them. Or, use atop. Check out this post -> Line break in expression()?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to substitute a character or expression for a newline using sed?

From Dev

How to substitute a character or expression for a newline using sed?

From Dev

unescaped newline inside substitute pattern

From Dev

FreeBSD sed with newline and backslash in substitute

From Dev

Substitute expression by value?

From Dev

Substitute with an expression and matched pattern

From Dev

Substitute expression by value?

From Dev

Find: Substitute expression with a variable

From Dev

How to substitute part of an expression in vim using substitute

From Dev

sed: unescaped newline inside substitute pattern?

From Dev

Adding a newline in a spool file

From Dev

Adding a newline in css

From Dev

Adding newline to PHP string

From Dev

Command substitution is adding a newline?

From Dev

Adding newline to PHP string

From Dev

substitute the derivative of a function in a symbolic expression

From Dev

substitute n'th expression with sed

From Dev

How to substitute and simplify an expression in sympy?

From Dev

Substitute plotmath character string into expression

From Dev

How to substitute and simplify an expression in sympy?

From Dev

substitute the derivative of a function in a symbolic expression

From Dev

sed: regular expression,how to substitute?

From Dev

F# substitute expression in expression tree

From Dev

Adding newline between elements in XSLT

From Dev

Why is Vim adding a newline? Is this a convention?

From Dev

Adding a newline in a textarea using Javascript

From Dev

Adding a newline in CodenameOne's SpanLabel

From Dev

Adding newline between elements in XSLT

From Dev

Adding Environment.NewLine into filestream

Related Related

HotTag

Archive