| ## build a fan plot from scratch using dplyr & ggplot2 for visualization of Flow data | |
| ## after an original idea by Guy J. Abel | |
| ## http://gjabel.wordpress.com/category/r/fanplot/ | |
| ## libraries | |
| library(flowCore) | |
| library(reshape2) | |
| library(dplyr) | |
| library(ggplot2) | |
| library(RColorBrewer) | |
| ## functions | |
| do.fan <- function(x,step=0.01) { | |
| data.frame(ymin=quantile(x,probs=seq(0,1,step))[-length(seq(0,1,step))], | |
| ymax=quantile(x,probs=seq(0,1,step))[-1], | |
| id=seq(1,length(seq(step,1,step))), | |
| percent=abs(seq(step,1,step)-0.5)) | |
| } | |
| StatFan <- ggproto("StatFan", Stat, | |
| required_aes = "y", | |
| default_aes = aes(fill=stat(percent),group=stat(id)), | |
| compute_group = function(data,scales,step=0.01) do.fan(data$y,step) | |
| ) | |
| stat_fan <- function(mapping = NULL, data = NULL, geom = NULL, | |
| position = "identity", na.rm = FALSE, show.legend = NA, | |
| inherit.aes = TRUE, ...) { | |
| list( | |
| layer( | |
| stat = StatFan, data = data, mapping = mapping, geom = geom, | |
| position = position, show.legend = show.legend, inherit.aes = inherit.aes, | |
| params = list(na.rm = na.rm, ...) | |
| ) | |
| ) | |
| } | |
| # GeomFan <- ggproto( "GeomFan", GeomRibbon ) | |
| geom_fan <- function(mapping = NULL, data = NULL, | |
| position = "identity", na.rm = FALSE, show.legend = NA, | |
| inherit.aes = TRUE, colorbase='Oranges', ...) { | |
| list( | |
| layer( | |
| stat = StatFan, geom = GeomRibbon, data = data, mapping = mapping, | |
| position = position, show.legend = show.legend, inherit.aes = inherit.aes, | |
| params = list(na.rm = na.rm, ...) | |
| ), | |
| scale_fill_gradientn(colours=rev(RColorBrewer::brewer.pal(9,colorbase)) ), | |
| guides(fill=FALSE) | |
| ) | |
| } | |
| ## parameters | |
| theme_set( theme_bw(base_size=14) ) | |
| ## load the data | |
| data(GvHD) | |
| ## first lets transform the data using a logicle function | |
| # estimate the parameters of the logicle function from the first sample | |
| logicleTrans <- estimateLogicle(GvHD[[1]], | |
| channels=c('FL1-H','FL2-H','FL3-H','FL4-H')) | |
| tGvHD <- logicleTrans %on% GvHD | |
| ## extract the values | |
| mat <- fsApply(tGvHD[,c('FL1-H','FL2-H','FL3-H','FL4-H')],exprs) | |
| ## get the annotation | |
| df <- pData(tGvHD)[unlist(sapply(sampleNames(tGvHD),function(x) rep(x,nrow(tGvHD[[x]])))),] | |
| ## bind the 2 together... | |
| df <- cbind(df,mat) | |
| ## ... and melt | |
| melted <- melt(df,id.var=varLabels(tGvHD)) | |
| ## fix the channel names | |
| clean.names <- with(pData(parameters(tGvHD[[1]])), | |
| desc[match(levels(melted$variable),name)]) | |
| clean.names <- sapply(clean.names,function(x) strsplit(x,' ')[[1]][1]) | |
| levels(melted$variable) <- clean.names | |
| ## Then you can compute and treillis your fan plot however you like | |
| step <- 0.01 | |
| ggplot(melted, | |
| aes(x=variable,y=value))+ | |
| geom_fan()+ | |
| facet_grid(Visit~Patient) | |
| ggplot(melted, | |
| aes(x=variable,y=value))+ | |
| geom_fan(step=0.05)+ | |
| facet_grid(Visit~Patient) | |
| ggplot(melted, | |
| aes(x=variable,y=value))+ | |
| geom_fan()+ | |
| facet_grid(Grade~Visit,labeller=label_both) | |
| ggplot(melted %>% filter(Visit=='1'), | |
| aes(x=variable,y=value))+ | |
| geom_fan(colorbase='Greens')+ | |
| facet_wrap(~Patient) | |
| # it is still possible to adjust the color of the fan | |
| ggplot(melted %>% filter(Visit=='1'), | |
| aes(x=variable,y=value))+ | |
| geom_fan()+ | |
| facet_wrap(~Patient)+ | |
| scale_fill_gradientn(colours=rev(brewer.pal(9,'Greens')) ) |