Packages

library(shiny)
library(RColorBrewer)
library(RCurl)
## Warning: package 'RCurl' was built under R version 3.2.4
## Loading required package: bitops
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.2.4
library(reshape2)
library(grid)
library(lattice)
library(corrplot)
library(Hmisc)
## Loading required package: survival
## Loading required package: Formula
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, round.POSIXt, trunc.POSIXt, units
library(PerformanceAnalytics)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
library(ggcorrplot)

Database

Summary

mydata1 <-  mydata [ , c ( 3 , 4 , 5 , 6 , 7 ) ]
str(mydata1)
## 'data.frame':    20 obs. of  5 variables:
##  $ pH_S30: num  6.3 5.4 5.4 6 5.9 5.6 5.8 5.8 5.9 5.9 ...
##  $ P_S30 : num  5 2 1 3 16 28 13 24 15 9 ...
##  $ Mg_S30: num  19 17 17 23 18 16 16 17 20 18 ...
##  $ k_S30 : num  13 13 10 15 26 41 32 34 19 26 ...
##  $ Ca_S30: num  294 84 68 133 101 72 71 74 131 105 ...
summary(mydata1)
##      pH_S30         P_S30           Mg_S30          k_S30     
##  Min.   :5.40   Min.   : 1.00   Min.   :14.00   Min.   :10.0  
##  1st Qu.:5.60   1st Qu.: 8.00   1st Qu.:16.00   1st Qu.:18.5  
##  Median :5.70   Median :16.00   Median :17.50   Median :26.0  
##  Mean   :5.76   Mean   :17.25   Mean   :17.65   Mean   :26.8  
##  3rd Qu.:5.90   3rd Qu.:25.25   3rd Qu.:18.00   3rd Qu.:34.5  
##  Max.   :6.30   Max.   :44.00   Max.   :23.00   Max.   :47.0  
##      Ca_S30      
##  Min.   : 56.00  
##  1st Qu.: 71.75  
##  Median : 90.00  
##  Mean   :102.45  
##  3rd Qu.:114.50  
##  Max.   :294.00

Boxplot

boxplot(mydata1, main="Range of pH on Melrose soil",
        xlab="pH",
        ylab="Range",
        col="grey",
        border="black",
        horizontal=FALSE,
        notch=FALSE)

Computing of the correlation matrix

The rcorr() function from Hmisc package can be used to perform Pearson or Spearman correlation test.

mcor <- cor(mydata1)
mcor
##             pH_S30       P_S30      Mg_S30       k_S30     Ca_S30
## pH_S30  1.00000000 -0.08742630  0.20759517  0.04959296  0.6187745
## P_S30  -0.08742630  1.00000000 -0.03430656  0.44994529 -0.1795933
## Mg_S30  0.20759517 -0.03430656  1.00000000 -0.63188167  0.5487586
## k_S30   0.04959296  0.44994529 -0.63188167  1.00000000 -0.5361706
## Ca_S30  0.61877447 -0.17959332  0.54875858 -0.53617064  1.0000000
symnum(mcor)
##        p P M k C
## pH_S30 1        
## P_S30    1      
## Mg_S30     1    
## k_S30    . , 1  
## Ca_S30 ,   . . 1
## attr(,"legend")
## [1] 0 ' ' 0.3 '.' 0.6 ',' 0.8 '+' 0.9 '*' 0.95 'B' 1
res<-rcorr(as.matrix(mydata1))

As an output, the rcorr() function returns a list including the following elements :

r : the correlation matrix.

P : the p-values corresponding to the significance levels of the correlations.

Printing the correlation matrix

signif(res$r, 2)
##        pH_S30  P_S30 Mg_S30 k_S30 Ca_S30
## pH_S30  1.000 -0.087  0.210  0.05   0.62
## P_S30  -0.087  1.000 -0.034  0.45  -0.18
## Mg_S30  0.210 -0.034  1.000 -0.63   0.55
## k_S30   0.050  0.450 -0.630  1.00  -0.54
## Ca_S30  0.620 -0.180  0.550 -0.54   1.00

Printing the p-values of the correlations

signif(res$P,2)
##        pH_S30 P_S30 Mg_S30  k_S30 Ca_S30
## pH_S30     NA 0.710 0.3800 0.8400 0.0036
## P_S30  0.7100    NA 0.8900 0.0470 0.4500
## Mg_S30 0.3800 0.890     NA 0.0028 0.0120
## k_S30  0.8400 0.047 0.0028     NA 0.0150
## Ca_S30 0.0036 0.450 0.0120 0.0150     NA

Formatting the correlation matrix in 4 column tables

cormat : matrix of the correlation coefficients

pmat : matrix of the correlation p-values

flattenCorrMatrix <- function(cormat, pmat) {
  ut <- upper.tri(cormat)
  data.frame(
    row = rownames(cormat)[row(cormat)[ut]],
    column = rownames(cormat)[col(cormat)[ut]],
    cor  =(cormat)[ut],
    p = pmat[ut]
    )
}

Visualize a correlation matrix

flattenCorrMatrix(res$r, res$P)
##       row column         cor           p
## 1  pH_S30  P_S30 -0.08742657 0.713983575
## 2  pH_S30 Mg_S30  0.20759511 0.379814114
## 3   P_S30 Mg_S30 -0.03430656 0.885827122
## 4  pH_S30  k_S30  0.04959274 0.835516351
## 5   P_S30  k_S30  0.44994530 0.046526090
## 6  Mg_S30  k_S30 -0.63188165 0.002801105
## 7  pH_S30 Ca_S30  0.61877447 0.003629899
## 8   P_S30 Ca_S30 -0.17959332 0.448664866
## 9  Mg_S30 Ca_S30  0.54875857 0.012223786
## 10  k_S30 Ca_S30 -0.53617066 0.014811609

Visualize a correlation matrix using a correlogram

corrplot(mcor, type="upper", order="hclust", tl.col="black", tl.srt=0)