Search code examples
rcorrelationpearson-correlationr-corrplotggcorrplot

Customized correlation plot color


Is there any way to customize the correlation plot in R for example in the below plot enter image description here

I want to have different color value for example if value lie between 0.5 to 0.7 then make it green and below that keep it to blue and above 0.7 keep it to red

I used below code for correlation plot

library(corrplot)
data_matrix<-as.matrix(data)
corr_mat=cor(data_matrix,method="pearson")
corrplot(corr_mat,method = "number")


Solution

  • Better a ggplot. It's easier to customize.

    Adjust the size of the text with the additional argument size inside geom_text if you need to.

    # given a correlation matrix
    corr_matrix <- cor(mtcars)
    
    library(dplyr)
    library(tidyr)
    library(ggplot2)
    
    corr_matrix %>% 
      as_tibble(rownames = "var1") %>% 
      gather(var2, value, -var1) %>% 
      
      ggplot(aes(x = var1, y = var2, fill = value)) +
      geom_tile() +
      geom_text(aes(label = round(value, digits = 2))) +
      labs(x = "", y = "", fill = "Corr", title = "Correlation Matrix") +
      coord_fixed() +
      theme_minimal() +
      scale_fill_gradientn(
            limits = c(-1,1),
    
            # here choose the colours you want
            colours = c("blue", "green", "red"), 
    
            # here choose the intervals you want (must be inside rescale!)
            values = scales::rescale(c(-1, 0.5, 0.7, 1)))
    

    enter image description here

    Just with numbers [personally I'd go with the first one]

    corr_matrix %>% 
      as_tibble(rownames = "var1") %>% 
      gather(var2, value, -var1) %>% 
      
      ggplot(aes(x = var1, y = var2, colour = value)) +
      geom_tile(colour = "gray20", fill = "white") +
      geom_text(aes(label = round(value, digits = 2))) +
      labs(x = "", y = "", fill = "Corr", title = "Correlation Matrix") +
      coord_fixed() +
      theme_minimal() +
      scale_colour_gradientn(colours = c("blue", "green", "red"),
                           values = scales::rescale(c(-1, 0.5, 0.7, 1)),
                           limits = c(-1,1))
    

    enter image description here


    EDIT

    I added this to solve the issue related to labels sorting.

    First of all, I'll edit mtcars to make it look like your data.

    colnames(mtcars) <- paste0("Month", 1:11)
    mtcars$Month12 <- rnorm(32)
    

    Okay, now let's proceed with the graph. We just need to add a small edit: we make the var names as ordered factors.

    corr_matrix <- cor(mtcars)
    
    library(dplyr)
    library(tidyr)
    library(ggplot2)
    
    corr_matrix %>% 
        as_tibble(rownames = "var1") %>% 
        gather(var2, value, -var1) %>% 
        
        # here is the additional line you need!
        mutate(across(c(var1, var2), factor, levels = paste0("Month", 1:12), ordered = TRUE)) %>% 
        
        ggplot(aes(x = var1, y = var2, fill = value)) +
        geom_tile() +
        geom_text(aes(label = round(value, digits = 2))) +
        labs(x = "", y = "", fill = "Corr", title = "Correlation Matrix") +
        coord_fixed() +
        theme_minimal() +
        scale_fill_gradientn(
            limits = c(-1,1),
            
            # here choose the colours you want
            colours = c("blue", "green", "red"), 
            
            # here choose the intervals you want (must be inside rescale!)
            values = scales::rescale(c(-1, 0.5, 0.7, 1)))
    
    

    enter image description here

    It works with dplyr version >= 1.0.0.

    If you don't have it, use this instead:

      mutate_at(c("var1", "var2"), factor, levels = paste0("Month", 1:12), ordered = TRUE) %>%