Color formatting of correlation table

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", "post", "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
V1V2V3V4V5V6V7V8V9V10
1
-0.516*1
0.983**-0.516*1
-0.398-0.02-0.441
0.794**-0.0860.812**-0.530*1
0.923**-0.3770.918**-0.4860.825**1
0.853**-0.4580.854**-0.548*0.817**0.819**1
0.674**-0.573*0.638*-0.2220.3880.547*0.815**1
0.669**-0.549*0.638*-0.3340.4420.567*0.863**0.976**1
0.763**-0.547*0.760**-0.50.616*0.697**0.955**0.926**0.960**1
comments powered by Disqus

Related