loading ggplot2 and reshape2 libraries

library(ggplot2)
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.4.1

creating a function for creatting correlation matrix

plot_correlation_matrix<- function(data)
{
  cor_matrix<-cor(data)
  melted_cor_matrix<-melt(cor_matrix)
  ggplot(data=melted_cor_matrix,aes(x=Var1,y=Var2,fill=value))+
    geom_tile(color="white")+
    scale_fill_gradient2(low="blue",high="red",mid="white",midpoint =0,limit=c(-1,1),
                         space="Lab",name="correlation")+
    theme_minimal()+
    theme(axis.text.x=element_text(angle =45,vjust=1,hjust=1,size=12)) +
    coord_fixed()+
    labs(title="correlational matrix",
          x="",
          y=""
    )}

calling function by passing mtcars dataset

plot_correlation_matrix(mtcars)