Search code examples
rggplot2correlationscatter-plot

spearman's rho plot


I have done a Spearman correlation and need a scatterplot with a line matching the Spearman's rho. How can I do this?

> db
   id Var_1  Var_2  Var_3 Var_4
1   5  8.17  83.08  10.28 19.81
2   6  4.06  42.30   2.72 28.92
3  17  6.06  62.43   4.55 23.10
4  18 10.88  61.12  15.96 23.50
5  27 10.67  49.29   1.99 19.50
6  30  2.85  57.22  13.05 19.90
7  37  7.26  53.79  13.91 22.30
8  40 28.77 146.45 558.33 33.10
9  41  5.26  42.87   7.03 23.40
10 42 21.38  70.32  12.60 20.00
> cor.test(db$Var_4, db$Var_2, method=c("spearman"))

    Spearman's rank correlation rho

data:  db$Var_4 and db$Var_2
S = 168, p-value = 0.9728
alternative hypothesis: true rho is not equal to 0
sample estimates:
        rho 
-0.01818182 

enter image description here


Solution

  • Use ‘abline’ to manually add lines to your plot.

    db <- read.table(header=TRUE, text="
       id Var_1  Var_2  Var_3 Var_4
    5  8.17  83.08  10.28 19.81
    6  4.06  42.30   2.72 28.92
    17  6.06  62.43   4.55 23.10
    18 10.88  61.12  15.96 23.50
    27 10.67  49.29   1.99 19.50
    30  2.85  57.22  13.05 19.90
    37  7.26  53.79  13.91 22.30
    40 28.77 146.45 558.33 33.10
    41  5.26  42.87   7.03 23.40
    42 21.38  70.32  12.60 20.00")
    
    rho <- cor(db$Var_4, db$Var_2, method=c("spearman"))
    

    Since Spearman's correlation coefficient is based on the ranks, we transform the data to ranks before plotting.

    db.rank <- dplyr::mutate(db, across(starts_with("Var"), rank))
    
    a <- mean(db.rank$Var_2)
    
    plot(db.rank$Var_4, db.rank$Var_2,
         main="Scatter plot of ranks\nSpearman's correlation: -0.15"))
    abline(a=a, b=rho, col="red")
    

    enter image description here