sjt.corr {sjPlot}

This document shows examples for using the sjt.corr function of the sjPlot package.

Ressources:

(back to table of content)

Basics of the sjt-functions

Please refer to this document

Data initialization

Please refer to this document.

library(sjPlot)
library(sjmisc)
data(efc)

Printing correlations

First, we need to prepare a data frame with variables where the correlation should be computed with. We randomly select variables from the sample dataset.

df <- data.frame(efc$c12hour, 
                 efc$e17age, 
                 efc$e42dep, 
                 efc$c161sex, 
                 efc$c172code)

The sjt.corr function either accepts a data frame with variables or a computed cor-object. In our example, we use the above created data frame.

sjt.corr(df)
  average number of hours of care per week elder’ age elder’s dependency carer’s gender carer’s level of education
average number of hours of care per week   0.095** 0.556*** 0.103** -0.103**
elder’ age 0.095**   0.209*** 0.065 0.038
elder’s dependency 0.556*** 0.209***   0.044 -0.009
carer’s gender 0.103** 0.065 0.044   -0.045
carer’s level of education -0.103** 0.038 -0.009 -0.045  
Computed correlation used spearman-method with listwise-deletion.

Change appearance of p-values

As you can see, non-significant values are faded by default. You can change this behaviour with the fade.ns parameter. Also, significant levels are indicated by asterisks. You can either suppress p-levels with show.p = FALSE or you can print the p-values as numbers with p.numeric = TRUE.

sjt.corr(df, fade.ns = FALSE, p.numeric = TRUE)
  average number of hours of care per week elder’ age elder’s dependency carer’s gender carer’s level of education
average number of hours of care per week   0.095
(.006)
0.556
(<.001)
0.103
(.003)
-0.103
(.003)
elder’ age 0.095
(.006)
  0.209
(<.001)
0.065
(.062)
0.038
(.269)
elder’s dependency 0.556
(<.001)
0.209
(<.001)
  0.044
(.202)
-0.009
(.793)
carer’s gender 0.103
(.003)
0.065
(.062)
0.044
(.202)
  -0.045
(.193)
carer’s level of education -0.103
(.003)
0.038
(.269)
-0.009
(.793)
-0.045
(.193)
 
Computed correlation used spearman-method with listwise-deletion.

Suppressing weak correlations

With the val.rm you can specify a value where correlation values are not printed to the table, if they are smaller than this specific limit. E.g., if val.rm = 0.4, all correlations lower than 0.4 are omitted from the table output.

sjt.corr(df, val.rm = 0.1)
  average number of hours of care per week elder’ age elder’s dependency carer’s gender carer’s level of education
average number of hours of care per week   0.095** 0.556*** 0.103** -0.103**
elder’ age 0.095**   0.209*** 0.065 0.038
elder’s dependency 0.556*** 0.209***   0.044 -0.009
carer’s gender 0.103** 0.065 0.044   -0.045
carer’s level of education -0.103** 0.038 -0.009 -0.045  
Computed correlation used spearman-method with listwise-deletion.

De facto, the omitted values are just formatted with white foreground color, thus they still are printed to the table. By this, you can also specify a certain color for those values, using the CSS parameter (see this page).

sjt.corr(df, val.rm = 0.1, 
         CSS = list(css.valueremove = "color: #99ccff;"))
  average number of hours of care per week elder’ age elder’s dependency carer’s gender carer’s level of education
average number of hours of care per week   0.095** 0.556*** 0.103** -0.103**
elder’ age 0.095**   0.209*** 0.065 0.038
elder’s dependency 0.556*** 0.209***   0.044 -0.009
carer’s gender 0.103** 0.065 0.044   -0.045
carer’s level of education -0.103** 0.038 -0.009 -0.045  
Computed correlation used spearman-method with listwise-deletion.

Printing just upper or lower triangle

Use the triangle parameter to just print the values in the upper or lower triangle of the table.

sjt.corr(df, triangle = "lower")
  average number of hours of care per week elder’ age elder’s dependency carer’s gender carer’s level of education
average number of hours of care per week          
elder’ age 0.095**        
elder’s dependency 0.556*** 0.209***      
carer’s gender 0.103** 0.065 0.044    
carer’s level of education -0.103** 0.038 -0.009 -0.045  
Computed correlation used spearman-method with listwise-deletion.

Adding information to the table diagonal

If you want to compute a correlation matrix of scales or index scores, you might want to add the Cronbach’s Alpha values in the table diagonal. In such a case (or any other usecase, of course), you can use the string.diag parameter.

sjt.corr(df, triangle = "lower", 
         string.diag = c("You", "may", "add", "anything", "here"))
  average number of hours of care per week elder’ age elder’s dependency carer’s gender carer’s level of education
average number of hours of care per week You        
elder’ age 0.095** may      
elder’s dependency 0.556*** 0.209*** add    
carer’s gender 0.103** 0.065 0.044 anything  
carer’s level of education -0.103** 0.038 -0.009 -0.045 here
Computed correlation used spearman-method with listwise-deletion.