I was creating some correlation tables for the dissertation and realized that plots are vastly more intuitive. Below is an example of how to create a correlation matrix using ggplot2.
## Renaming the variables and ordering the dataframe
library ( reshape )
levels ( cor.dat $ X 1 ) <- list ( "Miles per Gallon" = "mpg" , "# of Cylinders" = "cyl" ,
"Displacement" = "disp" , "Horsepower" = "hp" , "Weight" = "wt" , "# of Gears" = "gear" )
levels ( cor.dat $ X 2 ) <- rev ( list ( "Miles per Gallon" = "mpg" , "# of Cylinders" = "cyl" ,
"Displacement" = "disp" , "Horsepower" = "hp" , "Weight" = "wt" , "# of Gears" = "gear" ))
## Plotting
library ( ggplot2 )
library ( ggthemes )
theme_set ( theme_solarized ())
ggplot ( cor.dat , aes ( X 2 , X 1 , fill = value )) +
geom_tile () +
geom_text ( aes ( X 2 , X 1 , label = value ), color = "#073642" , size = 4 ) +
scale_fill_gradient ( name = expression ( "Spearman" * ~ rho ), low = "#fdf6e3" , high = "steelblue" ,
breaks = seq ( 0 , 1 , by = 0.2 ), limits = c ( 0.3 , 1 )) +
scale_x_discrete ( expand = c ( 0 , 0 )) +
scale_y_discrete ( expand = c ( 0 , 0 )) +
labs ( x = "" , y = "" ) +
guides ( fill = guide_colorbar ( barwidth = 7 , barheight = 1 , title.position = "top" ,
title.hjust = 0.5 )) +
theme ( axis.text.x = element_text ( angle = 45 , vjust = 1 , hjust = 1 ),
panel.grid.major = element_blank (),
panel.border = element_blank (),
panel.background = element_blank (),
axis.ticks = element_blank (),
legend.justification = c ( 1 , 0 ),
legend.position = c ( 0.9 , 0.7 ),
legend.direction = "horizontal" ) +
guides ( fill = guide_colorbar ( barwidth = 7 , barheight = 1 , title.position = "top" ,
title.hjust = 0.5 ))