I am trying to work out the best way of approaching this visualisation problem.
I have a correlation plot that has been constructed using the following code:
library(dplyr)
library(tidyr)
library(corrplot)
library(RColorBrewer)
data <- mtcars
# Data preparation and correlation matrix calculation
df_cor <- na.omit(data) %>%
mutate(across(where(is.character), as.factor)) %>%
mutate(across(where(is.factor), as.numeric))
# Calculate the correlation matrix
cor_matrix <- cor(df_cor, method = c("spearman"))
cor_matrix
# Calculate the p values
res1 <- cor.mtest(df_cor, conf.level = 0.95)
p_values <- as.data.frame(res1) %>%
rownames_to_column("rowname") %>%
gather("var", "p", -rowname)
# Remove insignificant correlations
cor_with_p_values <- cor_matrix %>%
as.data.frame() %>%
rownames_to_column("rowname") %>%
gather("var", "value", -rowname) %>%
left_join(p_values, by = c("rowname", "var")) %>%
mutate(value = ifelse(p > 0.05, NA, value)) %>%
select(-p) %>%
spread(var, value)
cor_with_p_values_only <- cor_matrix %>%
as.data.frame() %>%
rownames_to_column("rowname") %>%
gather("var", "value", -rowname) %>%
left_join(p_values, by = c("rowname", "var")) %>%
mutate(value = ifelse(p > 0.05, NA, value)) %>%
select(-value) %>%
spread(var, p)
# correlation values
filtered_cor_matrix <- cor_with_p_values %>%
column_to_rownames("rowname") %>%
as.matrix()
# P values
filtered_cor_matrix_p <- cor_with_p_values_only %>%
column_to_rownames("rowname") %>%
as.matrix()
#ordered_cor_matrix <- filtered_cor_matrix[order_vector[order_vector %in% rownames(filtered_cor_matrix)],
# order_vector[order_vector%in% colnames(filtered_cor_matrix)]]
#ordered_cor_matrix_p <- filtered_cor_matrix_p[order_vector[order_vector %in% rownames(filtered_cor_matrix_p)],
# order_vector[order_vector%in% colnames(filtered_cor_matrix_p)]]
# Plot the correlation matrix
corrplot(filtered_cor_matrix , is.corr = FALSE, tl.col = "black", na.label = " ",
col = rev(brewer.pal(n = 11, name = "RdBu")),
order = "original",
insig = "label_sig",
p.mat = filtered_cor_matrix_p ,
sig.level = c(.001, .01, .05),
pch.cex = 0.8,
pch.col = "black",
method = "color",
type = "upper",
diag = FALSE)
However, what I need to do is add an additional legend to the x axis to visually help the reader identify what category certain variables fall into.
Similar to adding coloured squares in this diagram:
Any guidance would be much appreciated!!!

