Skip to content

Instantly share code, notes, and snippets.

@mbk0asis
Created July 24, 2017 02:54
Show Gist options
  • Save mbk0asis/bcc1b427e2ddc91e86da2dbb6c084b2a to your computer and use it in GitHub Desktop.
Save mbk0asis/bcc1b427e2ddc91e86da2dbb6c084b2a to your computer and use it in GitHub Desktop.
R_saving_multiple_plots
# USING "ggplot2" and "for loop"
types <- data.frame(c(rep("breast.C",793),rep("breast.N",97),
rep("colon.C",313),rep("colon.N",38),
rep("kidney.C",324),rep("kidney.N",160),
rep("liver.C",377),rep("liver.N",50),
rep("lung.C",473),rep("lung.N",32),
rep("prostate.C",502),rep("prostate.N",50)))
colnames(types) <- "types"
tissues <- data.frame(c(rep("breast",890),rep("colon",351),
rep("kidney",484),rep("liver",427),
rep("lung",505), rep("prostate.C",552)))
colnames(tissues) <- "tissues"
hm <- read.csv("file:///E:/LAB_DATA/00-LabData/Lab/00--Archive/TCGA_DNA_met/423CpGs_set12357/423CpGs.heatmap.with.normal.csv",
header=F, row.names=1)
dta_hm <- cbind(tissues,types,t(hm))
dim(dta_hm)
library(ggplot2)
setwd("E:/LAB_DATA/00-LabData/Lab/00--Archive/TCGA_DNA_met/423CpGs_set12357/boxplot_with_normal")
colNames <- names(dta_hm)[3:425]
for(i in colNames){
print(i)
p <- ggplot(dta_hm, aes_string(x=dta_hm[,2], y=i))
p + theme_bw() + geom_violin(aes(fill=types), alpha=0.4, scale="width") + geom_boxplot(width=0.4, alpha=0.9,outlier.shape = NA) + theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) + guides(fill=FALSE) + xlab("") + ylim(0,1)
ggsave(filename=paste(i,".png",sep=""), width = 5.5, height = 4, dpi = 600)
graphics.off()
}
+ geom_jitter(width=0.2,alpha=0.3)
######################
# USING generic R plot functions
setwd("E:/LAB_DATA/00-LabData/Lab/00--Archive/TCGA_DNA_met/423CpGs_set12357/")
dta <- read.csv("file:///E:/LAB_DATA/00-LabData/Lab/00--Archive/TCGA_DNA_met/423CpGs_set12357/423CpGs.boxplot.2.csv", header = T)
file <- "cpg.list.csv"
close(con)
dev.off()
close(con)
dev.off()
con <- file(description=file, open="r")
while (length(line <- readLines(con, n = 1, warn = FALSE)) > 0) {
print(line)
dta1 <- dta[,grep(line,colnames(dta))]
tiff(paste(line,'.tif',sep = ""),units = "px",width = 500, height = 400)
par(mar=c(3,3,3,3))
stripchart(dta1, method='jitter', vertical=T, pch=20,cex=.9,
col=(c(rgb(.8,.8,.3,.7),rgb(.3,.8,.8,.7))),axes = F,
jitter=.2, ylim=c(-0.1, 1.1))
b<-boxplot(dta1, las=2, col=(c(rgb(.8,.3,.3,.5),rgb(.3,.3,.8,.5))), add = T,
main=paste(line), outline=F, xaxt="n")
lines(b$stats[3,], lwd = 2, lty = 5, type = "o")
dev.off()
}
close(con)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment