Last active
May 5, 2016 15:54
-
-
Save arvi1000/d37a837d50b562ae5f013dc0c08898e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a quasi replication of the correlation matrix heatmap viz from Seaborn, | |
# using R and ggplot | |
# see: http://stanford.edu/~mwaskom/software/seaborn/examples/structured_heatmap.html | |
library(data.table) | |
library(ggplot2) | |
# get data | |
brain_url <- paste0('https://raw.githubusercontent.com/', | |
'mwaskom/seaborn-data/master/brain_networks.csv') | |
# read data and drop 1st column, which is just row labels | |
brain_headers <- fread(brain_url, nrows = 3)[, -1, with=F] | |
brain_dat <- fread(brain_url, skip = 4)[, -1, with=F] | |
# collapse 3 rows of header data to single string | |
heads <- brain_headers[, sapply(.SD, paste, collapse='_')] | |
setnames(brain_dat, heads) | |
# Select a subset of the networks (as per example in URL) | |
used_networks = c(1, 5, 6, 7, 8, 11, 12, 13, 16, 17) | |
dat_subset <- | |
brain_dat[, as.numeric(t(brain_headers[1, ])) %in% used_networks, | |
with=F] | |
# corelation matrix (long format) | |
brain_cor <- melt(cor(dat_subset)) | |
# reverse levels of 2nd var (to match example, though not sure why he did that; | |
# I think the plot is better without this) | |
brain_cor$Var2 <- with(brain_cor, factor(Var2, levels = rev(levels(Var2)))) | |
ggplot(brain_cor, aes(x=Var1, y=Var2, fill=value)) + | |
geom_tile(color='white', size=0.5) + | |
labs(x='network - node - hemi', y='network - node - hemi') + | |
scale_fill_gradient2(low='#1c545d', | |
high = '#8a174f', | |
mid = 'whitesmoke') + | |
coord_equal() + | |
theme_minimal(base_size = 10) + | |
theme(axis.text.x = element_text(angle=90)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obviously this plot doesn't do the hierarchical clustering + marginal dendrograms of the original example, but still a nice simple way to visualize a correlation matrix. Result: