set.seed(123)
data <- matrix(runif(100, min = 0, max = 1), nrow = 10)
data[, 1] <- 0
data[, 5] <- 0
data[, 7] <- 0
data[, 8] <- 0
df <- as.data.frame(data)
correlation_matrix <- cor(df)
correlation_matrix <- (correlation_matrix + 1) / 2
correlation_matrix[is.na(correlation_matrix)] <- 0
correlation_matrix
# Identify rows and columns with non-zero values (excluding the diagonal)
ix <- rowSums(correlation_matrix- diag(diag(correlation_matrix))) != 0; correlation_matrix[ix, ix]
# Subset the correlation matrix using ix
correlation_subset <- correlation_matrix[ix, ix]
# Ensure the subset is of class "matrix" for compatibility with corrplot
correlation_subset <- as.matrix(correlation_subset)
# Plot using corrplot with coefficients displayed
library(corrplot)
corrplot(correlation_subset, method = "color",addCoef.col = "black")
