简体   繁体   中英

Add an equation into scatter plots in R

I have a little trouble to add the equations into scatter plots using "legend"

A simple example is as follow:

plot(1:100)

# The below code can work if I add "= 0.1234" directly.
legend(locator(1), expression(paste("Linear model: ", R^2, "= 0.1234",sep="")),
        text.col= "black",cex=1,bty="n")

# The below code cannot work if I add the "ps".
ps = "= 0.1234"

legend(locator(1), expression(paste("Linear model: ", R^2, ps, sep="")), 
       text.col= "red",cex=1,bty="n")

The real issue I have is a little complex with this example.

So how should I revise this code?

The "ps"-object is being handled as an expression, ie not being evaluated. To get around this use bquote and .()

 legend(locator(1), legend= bquote("Linear model: "* R^2*.(ps)), 
        text.col= "red",cex=1,bty="n")

BTW the first version would be more compactly represented without the paste :

legend(locator(1), expression(Linear~model*":"~ R^2 == 0.1234),
    text.col= "black",cex=1,bty="n")

The only thing needing to be quoted is the semi-colon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM