mona
mona

Reputation: 101

how to add correlation value and p-values in boxplot in R

I want to add a computed correlation coefficient and p-value to my boxplot. This is my code:

# Load needed package
library(coin)

# Simulate data
xx <- runif(10)
yy <- runif(10)

# Compute R and p-value
scor <- cor(xx, yy, method = "spearman")
ppp <- spearman_test(xx ~ yy)

# Make plot
boxplot(list(t(xx),t(yy)), main="exprment values", col = c("orange", "yellow"))
legend("topright", bty="n", legend=paste("r=0.69, p=0.0001"))

I want the R and p-value to be printed automatically so I don't have to type them out.

Upvotes: 1

Views: 2059

Answers (1)

akuiper
akuiper

Reputation: 214987

You can paste character and numeric values together as you've tried, just need to be in the right format.

legend("topright", bty="n", legend=paste("r=", scor, ", p=", ppp, sep = ""))

Upvotes: 2

Related Questions