Correlation matrix graph with calculated result

Dear sir/madam,
I need to make a corrplot by manually calculated results.
For example,
A B C
A 0.00 0.43 0.32
B 0.43 0.00 0.23
C 0.32 0.23 0.00

Actually, this is my exact result. I want the graph as like attached image

Welcome to the community!

First of all, why are the diagonals zero instead of being one?

Second, you can plot using heatmap.

Example:

text_data <- "A B C
A 0.00 0.43 0.32
B 0.43 0.00 0.23
C 0.32 0.23 0.00"

correlation_matrix <- as.matrix(x = read.table(text = text_data,
                                               header = TRUE))

# full plot
heatmap(x = correlation_matrix,
        Rowv = NA,
        Colv = NA,
        distfun = NA,
        hclustfun = NA,
        symm = TRUE,
        revC = TRUE,
        scale = "none")

# lower triangular
lower_correlation_matrix = correlation_matrix
lower_correlation_matrix[upper.tri(x = lower_correlation_matrix)] = NA
heatmap(x = lower_correlation_matrix,
        Rowv = NA,
        Colv = NA,
        distfun = NA,
        hclustfun = NA,
        symm = FALSE,
        revC = TRUE,
        scale = "none")

Created on 2021-03-22 by the reprex package (v1.0.0)

Hope this helps.

1 Like

Thank you so much, it was a huge confusion to me. now cleared. Thank you so much again

greeting. how can I show the value on the graph sir?

And gradient scale sir

Unfortunately, the gradient may not be possible with heatmap. At least I couldn't find a way.

You can use ggplot2 to do what you want:

text_data <- "A B C
A 0.00 0.43 0.32
B 0.43 0.00 0.23
C 0.32 0.23 0.00"

correlation_matrix <- read.table(text = text_data,
                                 header = TRUE)

library(ggplot2)
library(tibble)
library(tidyr)

correlation_matrix %>%
        rownames_to_column(var = "row_names") %>%
        pivot_longer(cols = -row_names,
                     names_to = "column_names",
                     values_to = "correlation_values") %>%
        ggplot(mapping = aes(x = row_names,
                             y = column_names)) +
        geom_tile(mapping = aes(fill = correlation_values)) +
        geom_label(mapping = aes(label = correlation_values))

Created on 2021-03-23 by the reprex package (v1.0.0)

Hope this helps!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.