Category: R

  • pie图绘制

    x

  • WGCNA(my project)

    setwd(“D:/Ma_transcription/WGCNA/”) database <- read.table(file = “COS_deseq_counts_normalized.txt”, sep = “\t”, header = T, row.names = 1, stringsAsFactors = F) library(reshape2) library(‘WGCNA’) enableWGCNAThreads()#打开多线程 WGCNA_matrix = t(database[order(apply(database,1,mad), decreasing = T)[1:10000],]) subname=sapply(colnames(database),function(x) strsplit(x,”_”)[[1]][1]) datTraits = data.frame(gsm=names(database), subtype=subname) rownames(datTraits)=datTraits[,1] head(datTraits) # Choose a set of soft-thresholding powers powers = c(c(1:10), seq(from = 12, to=20, by=2)) datExpr <- WGCNA_matrix # Call…

  • MA图绘制

    # 导入ggplot2包 library(ggplot2) # 设置好工作目录(到数据所在目录) # 读取输入数据 “R0-vs-R3.isoforms.filter.tsv” data = read.table(“R0-vs-R3.isoforms.filter.tsv”,header=T,row.names=1) # 计算M值和A值,并将M作为y轴,A作为x轴 aes = aes(x=(log2(R0_fpkm)+log2(R3_fpkm))/2,y=log2(R0_fpkm)-log2(R3_fpkm)) # 绘制MAplot ggplot(data=data,aes) + geom_point(aes(color=significant)) # 添加辅助线 ggplot(data=data,aes) + geom_hline(yintersect=0,linetype=4,color=”blue”) + geom_point(aes(color=significant)) # 改变点的大小 maplot = ggplot(data=data,aes) + geom_hline(yintercept=0,linetype=4,color=”blue”) + geom_point(aes(color=significant),size=1) # 设置自定义染色 maplot + scale_color_manual(values=c(“green”,”black”,”red”)) # 设置标题 maplot + scale_color_manual(values=c(“green”,”black”,”red”)) + labs(title=”MAplot of R0-vs-R3″,x=”A”,y=”M”) # 保存MAplot ggsave(“R0-vs-R3.MAplot.png”,width=8,height=6)

  • DESeq2使用流程

    library(“DESeq2”) database

  • PCA图绘制

    输入表达矩阵数据文件 library(ggplot2) library(gmodels) inname = “COS_deseq_counts_normalized.txt” outname = “COS_PCA.png” group

  • 绘制盒形图

    输入表达矩阵数据文件 library(ggplot2) library(reshape2) exprSet <- read.table(file = “COS_deseq_counts_normalized.txt”, sep = “\t”, header = T) group_list <- factor(c(rep(“COS0”,3),rep(“COS1”,3),rep(“COS3”,3),rep(“COS6”,3),rep(“COS12”,3)), levels = c(“COS0”, “COS1″,”COS3″,”COS6″,”COS12″)) exprSet_L=melt(exprSet) colnames(exprSet_L)=c(‘Gene_ID’,’sample’,’value’) exprSet_L$group=rep(group_list,each=nrow(exprSet)) #以sample作为x轴,group作为填充颜色 ggplot(exprSet,aes(x=sample,y=value,fill=group)) + geom_boxplot() #以group作为x轴,sample作为填充颜色 #ggplot(exprSet,aes(x=group,y=value,fill=sample)) + geom_boxplot() # 修改sample名称 exprSet_L$sample=paste(exprSet_L$group,exprSet_L$sample,sep=”-“) ggplot(exprSet_L,aes(x=sample,y=value,fill=group)) + geom_boxplot() # 离群点的大小 cvbox=ggplot(exprSet_L,aes(x=sample,y=value,fill=group))+geom_boxplot(outlier.size=0) #outlier.size=-1 ,点将彻底消失 # 修改x轴字符方向 cvbox+theme(axis.text.x=element_text(angle=90,vjust=0.5,hjust=1)) # 修改图例位置和主题 cvbox+theme_bw()+theme(axis.text.x=element_text(angle=90,vjust=0.5,hjust=1),legend.position=”top”) # geom_jitter():为图形设置抖动,防止相同点重叠 cvbox+theme_bw()+theme(axis.text.x=element_text(angle=90,vjust=0.5,hjust=1),legend.position=”top”) + geom_jitter()

  • DESeq分析流程

    source(“https://bioconductor.org/biocLite.R”) biocLite(“DESeq”) library(“DESeq”) database <- read.table(file = “macrophage_genes.count_table.matrix”, sep = “\t”, header = T, row.names = 1) countData <- database[,1:3] condition <- factor(c(“A”,”B”,”C”))#无生物学重复 #type <- factor(c(rep(“A”,3), rep(“B”,3))) 有生物学重复 database <- round(as.matrix(countData))#取整数型 dds <- newCountDataSet(database,condition) dds <- estimateSizeFactors(dds) dds <- estimateDispersions(dds, method=”blind”, sharingMode=”fit-only” )#无生物学重复 dds <- estimateDispersions(dds) # 有生物学重复 resAvsB <- nbinomTest(dds,”A”,”B”) resAvsC <- nbinomTest(dds,”A”,”C”) table(resAvsB$pval…

  • ggplot2:气泡图及散点图小结

    气泡图也属于散点图的一种,在散点图的基础上改变点的形状,大小和颜色 1.如何改变点的形状 用自带的mtcars演示 p = ggplot(mtcars,aes(wt,mpg)) p + geom_point(shape=17,size=4) p + geom_point(aes(shape = factor(cyl)),size=4) 2.改变点的大小:气泡图 加载包、数据的读入及数据格式 library(ggplot2) pathway = read.table(“R0-vs-R3.path.richFactor.head20.tsv”,header=T,sep=”\t”) 3.初始化数据 pp = ggplot(pathway,aes(richFactor,Pathway)) pp + geom_point() 气泡图(三维数据),size 可以是一个值,也可以是数据中的一列 pp + geom_point(aes(size=R0vsR3)) 添加颜色变化——四维数据 scale_colour_gradient:自定义一个连续型的配色,常用于热图; pbubble = pp + geom_point(aes(size=R0vsR3,color=-1*log10(Qvalue))) pbubble pbubble + scale_colour_gradient(low=”green”,high=”red”) expression函数改变样式,[]是用来添加下标,^是用来添加上标 pr = pbubble + scale_colour_gradient(low=”green”,high=”red”) + labs(color=expression(-log[10](Qvalue)),size=”Gene number”,x=”Rich factor”,y=”Pathway name”,title=”Top20 of…

  • ggplot2:火山图

    火山图是散点图的一种,也是描点,用 geom_point()绘图 1.加载包,读入数据及数据格式 library(ggplot2) data = read.table(“R0-vsR3.isoforms.filter.tsv”,header=T,row.names=1) 2.绘图 r03 = ggplot(data,aes(log2FC,-1*log10(FDR))) r03 + geom_point() 3.改变点的颜色 r03 + geom_point(color=”red”) r03 + geom_point(aes(color=”red”)) r03 + geom_point(aes(color=significant)) # 按照“ significant”这一列定义点的颜色; 4.设置坐标轴范围和标题 函数xlim(),ylim(),规定X、 Y轴范围;labs(title=“..”,x=“..”,y=“..”),确定标题和坐标轴标签;expression函数改变样式,[]是用来添加下标,^是用来添加上标 r03xy = r03 + geom_point(aes(color=significant)) + xlim(-4,4) + ylim(0,30) r03xy + labs(title=”Volcano plot”,x=”log2(FC)”) r03xy + labs(title=”Volcano plot”,x=expression(log[2](FC)),y=expression(-log[10](FDR))) 5.自定义颜色 scale_color_manual(): 自定义颜色配色; r03xyp = r03xy + labs(title=”Volcano…

  • ggplot2:散点图

    1.加载ggplot2包,读取数据及数据格式 library(ggplot2) fpkm = read.table(“all.fpkm”,header=T,row.names=1) head(fpkm,10) 2.绘图 mp = ggplot(fpkm,aes(C1_FPKM,C2_FPKM)) mp + geom_point() 3.取log10后重新作图 mp = ggplot(fpkm,aes(log10(C1_FPKM),log10(C2_FPKM))) mp + geom_point() 4.添加直线 slope设置斜率,intercept设置截距,color设置线条颜色,size设置线条粗细 mp + geom_abline(slope=1,intercept=0,color=”red”,size=2) + geom_point() ggplot2的核心思想就是图层,可以像PS一样一层层的添加图层,以加号连接,更详细参数可参考ggplot2的帮助文档http://docs.ggplot2.org/current/ 视频教程:http://www.omicshare.com/class/home/index/singlev?id=34