Using R: Correlation heatmap with ggplot2

R-bloggers 2013-03-22

(This article was first published on There is grandeur in this view of life » R, and kindly contributed to R-bloggers)

Just a short post to celebrate that I learned today how incredibly easy it is to make a heatmap of correlations with ggplot2 (and reshape2, of course).

data(attitude)library(ggplot2)library(reshape2)qplot(x=Var1, y=Var2, data=melt(cor(attitude)), fill=value, geom="tile")

attitude_heatmap

So, what is going on in that short passage? cor makes a correlation matrix with all the pairwise correlations between variables (twice; plus a diagonal of ones). melt takes the matrix and creates a data frame in long form, each row consisting of id variables Var1 and Var2 and a single value. We then plot with the tile geometry, mapping the indicator variables to rows and columns, and value (i.e. correlations) to the fill colour.

Postat i:data analysis, english Tagged: ggplot2, R

To leave a comment for the author, please follow the link and comment on his blog: There is grandeur in this view of life » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series,ecdf, trading) and more...