Skip to content

Instantly share code, notes, and snippets.

@jasper1918
Created August 15, 2017 21:06
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 jasper1918/790b11348b0507d65b2f37141913b30d to your computer and use it in GitHub Desktop.
Save jasper1918/790b11348b0507d65b2f37141913b30d to your computer and use it in GitHub Desktop.
MA plot
plot_ma<- function(dat, rep1, rep2, main_title=""){
l1<-log2(dat[,rep1] +1)
l2<-log2(dat[,rep2] +1)
diff <- l1 - l2
names(diff) <- "LOG2FC"
ave <- rowMeans(cbind(l1, l2), na.rm=TRUE)
df <- data.frame(rep1=l1, rep2=l2, "LOG2FC"=diff, "AVE_EXP"=ave, check.names = F)
max_y<-ceiling(max(abs(max(df$LOG2FC)),abs(min(df$LOG2FC)), 2))
seq_y<-round(seq(-max_y, max_y, by = .5),1)
#df$OUT1<-ifelse(abs(df$LOG2FC)>1, 0,1)
img<-ggplot(data=df, aes(x=AVE_EXP, y=LOG2FC)) +
geom_point(alpha=0.6, size=1.8) +
geom_smooth(method='loess', alpha=.2, span=1, se=F) +
geom_hline(aes(yintercept = 0), colour = "firebrick", size = 1.0) +
geom_hline(aes(yintercept = 1), colour = "firebrick", linetype="dashed", size = .6, alpha=.7) +
geom_hline(aes(yintercept = -1), colour = "firebrick", linetype="dashed", size = .6, alpha=.7) +
geom_hline(aes(yintercept = 2), colour = "firebrick", linetype="dashed", size = .6, alpha=.4) +
geom_hline(aes(yintercept = -2), colour = "firebrick", linetype="dashed", size = .6, alpha=.4) +
scale_x_continuous(breaks = round(seq(min(df$AVE_EXP), max(df$AVE_EXP), by = 1),1)) +
scale_y_continuous(breaks = seq_y) +
expand_limits(y=c(min(seq_y), max(seq_y))) +
xlab("Mean Expression") +
ylab("Log2 Fold Change") +
ggtitle(main_title) +
theme_bw() +
theme(axis.title.x = element_text(face = "bold", size = 15),
axis.text.x = element_text(face = "bold", size = 12)) +
theme(axis.title.y = element_text(face = "bold", size = 15),
axis.text.y = element_text(face = "bold", size = 12)) +
theme(legend.position="none")
return(img)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment