Develop a script in R to calculate and visualize a correlation matrix for a given dataset, with color-coded cells indicating the strength and direction of correlations, using ggplot2’s geom_tile function.
# Load required librarieslibrary(ggplot2)
Warning: package 'ggplot2' was built under R version 4.1.3
library(tidyr)
Warning: package 'tidyr' was built under R version 4.1.3
library(dplyr)
Warning: package 'dplyr' was built under R version 4.1.3
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
# Use built-in mtcars datasetdata(mtcars)# Compute correlation matrixcor_matrix <-cor(mtcars)# Convert matrix to a data frame for plottingcor_df <-as.data.frame(as.table(cor_matrix))head(cor_df)