Skip to content

Instantly share code, notes, and snippets.

@yannabraham
Created October 30, 2019 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yannabraham/2c77013f3da8aebf5c7e2a023ad3bd2e to your computer and use it in GitHub Desktop.
Save yannabraham/2c77013f3da8aebf5c7e2a023ad3bd2e to your computer and use it in GitHub Desktop.
Classically one can use fan plots to visualize the full profile of a given population over a set of conditions, but if you are interested in the effect of treatment on a given channel, you can `pivot` or `invert` the fan plot and visualize the effect of treatment over all populations
library(ggplot2)
library(dplyr)
library(tidyr)
library(cytofan)
library(bodenmiller)
## let's prepare some data:
cur.mat <- cbind(rbind(refPhenoMat,untreatedPhenoMat),
rbind(refFuncMat,untreatedFuncMat))
cur.annot <- bind_rows(refAnnots %>%
mutate(Treatment='reference') %>%
mutate_all(as.character),
untreatedAnnots %>%
mutate(Treatment=as.character(Treatment)) %>%
mutate_all(as.character)) %>%
mutate(Treatment=factor(Treatment),
Treatment=relevel(Treatment,'reference'),
Cells=factor(Cells,levels=levels(refAnnots$Cells)))
cur.df <- bind_cols(data.frame(cur.mat),cur.annot)
## classically one can use fan plots to visualize the full profile of a given population
## over a set of conditions:
cur.df %>%
filter(Cells=='cd4+') %>%
gather('Channel','value',one_of(colnames(cur.mat))) %>%
ggplot(aes(x=Channel,y=value))+
geom_fan()+
facet_grid(Treatment~.)+
theme_light(base_size = 18)+
theme(axis.text.x=element_text(angle=45,hjust=1))
## but if you are interested in the effect of treatment on a given channel,
## you can `pivot` or `invert` the fan plot and visualize
## the effect of treatment over all populations:
cur.ch <- 'pS6'
cur.annot %>%
mutate(value=cur.mat[,cur.ch]) %>%
ggplot(aes(x=Cells,y=value))+
geom_fan()+
facet_grid(Treatment~.)+
theme_light(base_size = 18)+
theme(axis.text.x=element_text(angle=45,hjust=1))
## you can add a reference line to make changes easier to visualize:
cur.annot %>%
mutate(value=cur.mat[,cur.ch]) %>%
ggplot(aes(x=Cells,y=value))+
geom_fan()+
geom_line(data=cur.annot %>%
mutate(value=cur.mat[,cur.ch]) %>%
filter(Treatment=='reference') %>%
group_by(Cells) %>%
do((function(df,quants=c(0.25,0.5,0.75)) {
res <- quantile(df$value,quants)
res <- data.frame(Quantile=names(res),
value=res)
return(res)
})(.)) %>%
mutate(LineType=ifelse(Quantile=='50%','dashed','dotted')),
aes(group=Quantile,linetype=LineType),
col='blue')+
scale_linetype_identity()+
facet_grid(Treatment~.)+
theme_light(base_size = 18)+
theme(axis.text.x=element_text(angle=45,hjust=1))
## eg treatment with BCR/FcR-XL increases pS6 in B cells (`igm+` and `igm-`),
## cd14-hladrhigh, cd14-hladrmid and
## monocytes (`cd14+hladr-`, `cd14+hladrhigh`, `cd14+hladrmid`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment