Skip to content

Instantly share code, notes, and snippets.

@jmxpearson
Created May 5, 2014 19:35
Show Gist options
  • Save jmxpearson/7f916918d9d3826dc16d to your computer and use it in GitHub Desktop.
Save jmxpearson/7f916918d9d3826dc16d to your computer and use it in GitHub Desktop.
ggplot2 grid plot of regression coefficients
ddir <- '~/data/bartc' # data directory
load(file=paste(ddir, 'fitdata', sep='/'))
extractfun <- function(x) {
outs <- c(x$baseline, x$effects)
names(outs)[1] = 'baseline'
return (outs)
}
dat <- as.data.frame(t(sapply(fitobjs, FUN=extractfun, simplify=TRUE)))
# make some plots
library(ggplot2)
plt <- ggplot(dat)
# baseline firing rates
plt + geom_histogram(aes(x=baseline)) +
xlab('Baseline firing rate (spks/s)') + ylab('Count')
# plot for each regression variable
library(reshape)
dd <- dat
vnames <- c('Baseline Firing Rate', 'During Trial', 'Outcome Period', 'During Inflation',
'Elapsed Time', 'Risk Level: Low', 'Risk Level: Medium', 'Risk Level: High',
'Control: No Reward', 'Balloon Pop', 'Banked Reward')
names(dd) <- vnames
dd <- melt(dd[,-1])
dd <- dd[dd$value != 0, ]
plt <- ggplot(dd)
plt + geom_histogram(aes(x=value)) + facet_wrap(~ variable, scales="free") +
xlab('Effect Sizes (% change from baseline)') + ylab('Count') + theme_bw() +
theme(strip.text.x = element_text(size = 12), axis.title.x = element_text(size = 16),
axis.title.y = element_text(size = 16))
# heatmap via ggplot
dd <- dat
vnames <- c('Baseline Firing Rate', 'During Trial', 'Outcome Period', 'During Inflation',
'Elapsed Time', 'Risk Level: Low', 'Risk Level: Medium', 'Risk Level: High',
'Control: No Reward', 'Balloon Pop', 'Banked Reward')
names(dd) <- vnames
dd <- cbind(1:dim(dd)[1], dd[,-1])
names(dd)[1] <- 'unit'
dd <- melt(dd, id='unit')
cutoff <- 50
subsel <- abs(dd$value) > cutoff
dd$value[subsel] <- cutoff * sign(dd$value[subsel]) # set an upper limit and censor
pdf(file='~/Dropbox/hephys/media/figs/unitgrid.pdf', paper='USr', width=11, height=8.5)
plt <- ggplot(dd, aes(x=unit, y=variable))
plt + geom_tile(aes(fill=value), color='gray') +
scale_fill_gradient2(low='blue', high='red', na.value='white', midpoint=0,
guide = guide_colorbar(title='Percent change\n from baseline')) +
scale_x_discrete('Unit', expand=c(0,0), limits=unique(dd$unit),
breaks=seq(5, max(unique(dd$unit)), 5)) +
scale_y_discrete('Variable', expand=c(0,0)) +
theme(axis.ticks=element_blank(), axis.text=element_text(color='black', size=12),
axis.title.x=element_text(size=20), axis.title.y=element_text(size=20)
)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment