
Easy export of correlation matrix to Excel (improved version)
Source:R/cormatrix_excel2.R
cormatrix_excel2.Rd
Easily output a correlation matrix and export it to Microsoft Excel, with the first row and column frozen, and correlation coefficients colour-coded based on effect size (0.0-0.2: small (no colour); 0.2-0.4: medium (pink/light blue); 0.4-1.0: large (red/dark blue)), following Cohen's suggestions for small (.10), medium (.30), and large (.50) correlation sizes.
Based on the correlation
and openxlsx2
packages.
Usage
cormatrix_excel2(
data,
filename,
overwrite = TRUE,
p_adjust = "none",
print.mat = TRUE,
...
)
Arguments
- data
The data frame
- filename
Desired filename (path can be added before hand but no need to specify extension).
- overwrite
Whether to allow overwriting previous file.
- p_adjust
Default p-value adjustment method (default is "none", although
correlation::correlation
's default is "holm")- print.mat
Logical, whether to also print the correlation matrix to console.
- ...
Parameters to be passed to the
correlation
package (see?correlation::correlation
)
Value
A Microsoft Excel document, containing the colour-coded correlation matrix with significance stars, on the first sheet, and the colour-coded p-values on the second sheet.
Details
WARNING: This function will replace cormatrix_excel
(the
original one) as soon as openxlsx2
is available from CRAN.
In the meanwhile, it is experimental and subject to change.
Use with care.
Author
Adapted from @JanMarvin (JanMarvin/openxlsx2#286) and
the original rempsyc::cormatrix_excel
Examples
# \donttest{
.old_wd <- setwd(tempdir())
# Basic example
cormatrix_excel2(mtcars, "cormatrix1")
#> # Correlation Matrix (pearson-method)
#>
#> Parameter | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb
#> ----------------------------------------------------------------------------------------------------------------------------------
#> mpg | 1.00*** | -0.85*** | -0.85*** | -0.78*** | 0.68*** | -0.87*** | 0.42* | 0.66*** | 0.60*** | 0.48** | -0.55**
#> cyl | -0.85*** | 1.00*** | 0.90*** | 0.83*** | -0.70*** | 0.78*** | -0.59*** | -0.81*** | -0.52** | -0.49** | 0.53**
#> disp | -0.85*** | 0.90*** | 1.00*** | 0.79*** | -0.71*** | 0.89*** | -0.43* | -0.71*** | -0.59*** | -0.56*** | 0.39*
#> hp | -0.78*** | 0.83*** | 0.79*** | 1.00*** | -0.45** | 0.66*** | -0.71*** | -0.72*** | -0.24 | -0.13 | 0.75***
#> drat | 0.68*** | -0.70*** | -0.71*** | -0.45** | 1.00*** | -0.71*** | 0.09 | 0.44* | 0.71*** | 0.70*** | -0.09
#> wt | -0.87*** | 0.78*** | 0.89*** | 0.66*** | -0.71*** | 1.00*** | -0.17 | -0.55*** | -0.69*** | -0.58*** | 0.43*
#> qsec | 0.42* | -0.59*** | -0.43* | -0.71*** | 0.09 | -0.17 | 1.00*** | 0.74*** | -0.23 | -0.21 | -0.66***
#> vs | 0.66*** | -0.81*** | -0.71*** | -0.72*** | 0.44* | -0.55*** | 0.74*** | 1.00*** | 0.17 | 0.21 | -0.57***
#> am | 0.60*** | -0.52** | -0.59*** | -0.24 | 0.71*** | -0.69*** | -0.23 | 0.17 | 1.00*** | 0.79*** | 0.06
#> gear | 0.48** | -0.49** | -0.56*** | -0.13 | 0.70*** | -0.58*** | -0.21 | 0.21 | 0.79*** | 1.00*** | 0.27
#> carb | -0.55** | 0.53** | 0.39* | 0.75*** | -0.09 | 0.43* | -0.66*** | -0.57*** | 0.06 | 0.27 | 1.00***
#>
#> p-value adjustment method: none
#>
#> [Correlation matrix 'cormatrix1.xlsx' has been saved to working directory (or where specified).]
#> Warning: will not open file when not interactive
#> NULL
cormatrix_excel2(iris, p_adjust = "none", "cormatrix2")
#> # Correlation Matrix (pearson-method)
#>
#> Parameter | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width
#> ----------------------------------------------------------------------
#> Sepal.Length | 1.00*** | -0.12 | 0.87*** | 0.82***
#> Sepal.Width | -0.12 | 1.00*** | -0.43*** | -0.37***
#> Petal.Length | 0.87*** | -0.43*** | 1.00*** | 0.96***
#> Petal.Width | 0.82*** | -0.37*** | 0.96*** | 1.00***
#>
#> p-value adjustment method: none
#>
#> [Correlation matrix 'cormatrix2.xlsx' has been saved to working directory (or where specified).]
#> Warning: will not open file when not interactive
#> NULL
cormatrix_excel2(airquality, method = "spearman", "cormatrix3")
#> # Correlation Matrix (spearman-method)
#>
#> Parameter | Ozone | Solar.R | Wind | Temp | Month | Day
#> -------------------------------------------------------------------------------
#> Ozone | 1.00*** | 0.35*** | -0.59*** | 0.77*** | 0.14 | -0.06
#> Solar.R | 0.35*** | 1.00*** | -9.77e-04 | 0.21* | -0.13 | -0.15
#> Wind | -0.59*** | -9.77e-04 | 1.00*** | -0.45*** | -0.16 | 0.04
#> Temp | 0.77*** | 0.21* | -0.45*** | 1.00*** | 0.37*** | -0.16
#> Month | 0.14 | -0.13 | -0.16 | 0.37*** | 1.00*** | -7.85e-03
#> Day | -0.06 | -0.15 | 0.04 | -0.16 | -7.85e-03 | 1.00***
#>
#> p-value adjustment method: none
#>
#> [Correlation matrix 'cormatrix3.xlsx' has been saved to working directory (or where specified).]
#> Warning: will not open file when not interactive
#> NULL
setwd(.old_wd)
# }