Color formatting of correlation table

By Deependra Dhakal in R

September 19, 2020

Correlation

Correlation is a bivariate summary statistic. It basically talks of direction and magnitidue of association of two variables. Besides formatting with significance stars, color coding correlation coefficient table might be helpful to pick patterns out in a quick glimpse.

Table 1 presents correlation matrix of yield and yield component traits (a blue \(\rightarrow\) red color profile represents increasing magnitude of positive correlation between traits). Following code is helpful if somebody provides a correlation table with stars in it and tells you to prettify it. Note that lower or upper halves only cannot be used to determine the discrete color values so full column is required.

# don't use underscore with this kablestyling
# list.files(here::here("content", "post", "data"))

correlation_mat <- read_csv(here::here("content", "blog", "data", "correlation_matrix.csv")) %>% 
  select(-1) %>% 
  mutate_if(is.numeric, as.character) %>% 
  as.matrix()

# construct symmetric matrix
correlation_mat[upper.tri(correlation_mat)] <- t(correlation_mat)[upper.tri(correlation_mat)]
correlation_mat <- correlation_mat %>% as_tibble()

rb_nextgen <- function(x){
  # generates a function for palette generation
  color_fun <- colorRampPalette(c(rgb(0.7, 0.7, 0.95, 0.5), rgb(0.95, 0.7, 0.7, 0.5)), alpha = TRUE)
  # generate a palette of length equal to length of vector
  length_vec <- length(x)
  generated_colors <- color_fun(n = length_vec)[as.numeric(cut(parse_number(x), breaks = length_vec))]
}

correlation_mat <- correlation_mat %>%
  mutate_all(function(x)kableExtra::cell_spec(x, "html", bold = TRUE, background = rb_nextgen(x)))
correlation_mat[upper.tri(correlation_mat)] <- NA

correlation_mat %>% 
  knitr::kable(format = "html",
               caption = "Correlation table with color",
               escape = F,
               booktabs = T,
               linesep = "",
               align = "c", digits = 2) %>%
  kableExtra::kable_styling(position = "center",
                            latex_options = c("scale_down", "HOLD_position"),
                            full_width = FALSE) %>%
  kableExtra::row_spec(row = 0, bold = TRUE)
Table 1: Correlation table with color
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1
-0.516* 1
0.983** -0.516* 1
-0.398 -0.02 -0.44 1
0.794** -0.086 0.812** -0.530* 1
0.923** -0.377 0.918** -0.486 0.825** 1
0.853** -0.458 0.854** -0.548* 0.817** 0.819** 1
0.674** -0.573* 0.638* -0.222 0.388 0.547* 0.815** 1
0.669** -0.549* 0.638* -0.334 0.442 0.567* 0.863** 0.976** 1
0.763** -0.547* 0.760** -0.5 0.616* 0.697** 0.955** 0.926** 0.960** 1
Posted on:
September 19, 2020
Length:
2 minute read, 318 words
Categories:
R
Tags:
R correlation table
See Also:
Simulating genetic drift
Missing negative from the normal
Internals of Mixed Models