wk 8
2023-02-28
# load the ggplot2 library
library(ggplot2)
library(reshape2)
# calculate the correlation matrix
correlation_matrix = cor(mtcars)
# create a heatmap of the correlation matrix
ggplot(data = melt(correlation_matrix),
aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
#The code loads the ggplot2 and
reshape2 libraries and uses the cor function to calculate the correlation
matrix of the mtcars dataset. Then, it creates a heatmap of the correlation
matrix using ggplot, where the x and y axes represent the variable names and
the fill represents the correlation value. The geom_tile() function is used to
create the heatmap, while the theme_minimal() function is used to set the theme
to a minimal design. Additionally, the theme(axis.text.x = element_text(angle =
90, vjust = 0.5, hjust=1)) is used to rotate the x-axis text labels by 90
degrees to make them more readable.
Comments
Post a Comment