Medical physicist
Medical physicist

Reputation: 2594

Concatenate strings and results in R plotmath

In R, I want to include a legend in a plot with the result of the Spearman's rho. I use this:

expression(paste(rho, " = ", cor(v$V1, v$V2, method = 'spearman')))

But in the legend I get:

p = cor($(v,V1), $(v,V2), spearman)

How can I get the result of cor?

Upvotes: 0

Views: 366

Answers (1)

blmoore
blmoore

Reputation: 1487

Here's an example that should help:

set.seed(42)
x <- 1:10
y <- rnorm(10)
plot(x,y)
text(7,1, bquote(rho==.(cor(x,y, method="spearman"))))

enter image description here

?bquote explains that it'll evaluate calls within .(). You generally don't need to use paste within plotmath.

Upvotes: 2

Related Questions