美文网首页生物信息学工作生活
ggplot2画出条形图(facet_wrap)

ggplot2画出条形图(facet_wrap)

作者: wo_monic | 来源:发表于2019-07-03 19:46 被阅读2次

ggplot2坐标轴最常用参数
https://zhuanlan.zhihu.com/p/25173606
facet_wrap分面操作
分面详解
https://zhuanlan.zhihu.com/p/27093478
添加误差棒

具体实现代码:

rm()
setwd("D:/RNA-seq/transcript_level/RT-PCR/")
datainfo <- read.csv(file="result2.csv",header = TRUE)
library(ggplot2)
#install.packages("mime")
library(mime)
library(export)

#函数依赖export包,构造图片输出函数
out_img <- function(filename,pic_width=5,pic_height=7){
  graph2png(file=filename,width=pic_width,height=pic_height)
  graph2ppt(file=filename,width=pic_width,height=pic_height)
  graph2tif(file=filename,width=pic_width,height=pic_height)
}
#使用缠绕分面facet_wrap画出表达量差异条形图,scales="free_y"使用各自的Y值,否则默认是使用统一的y值
##labs用于添加title,theme用于标题居中
###geom_errorbar用于添加误差棒,分别指定上误差,下误差。
pg_mean <- as.data.frame(datainfo)
p <- ggplot(pg_mean,aes(x=type,y=value,fill=genename))+facet_wrap(~genename,scales = "free_y",shrink = TRUE,as.table = TRUE)+geom_bar(stat="identity",width=0.5,color="black",position = position_dodge(0.9))+
  geom_errorbar(aes(ymin=value-down,ymax=value+up),width=0.2,position=position_dodge(0.5))
p+labs(title = "qRT-PCR and RNA-seq ",x="gene list ",y="Relative expression ")+theme(plot.title = element_text(hjust = 0.5))
out_img(filename="all_result",pic_width = 10,pic_height = 12)

result2.csv数据格式

genename value type up down index
48521569 3.00 RNA_seq 0 0 1
257858745 2.11 RNA_seq 0 0 2
112257492 2.94 RNA_seq 0 0 3
3542044848 2.9 RNA_seq 0 0 4
74011558154 4.50 RNA_seq 0 0 5
254488136 1.45 RNA_seq 0 0 6
结果如图所示,黑色的是后期的马赛克

修改上述的fill值,即修改分组依据为type
fill=type之后,对应部分命令如下


pg_mean <- as.data.frame(datainfo)
genename <- reorder(pg_mean$genename,pg_mean$index)  #把genename按照index的顺序排序,后续出图小图标也按照这个顺序出图
p <- ggplot(pg_mean,aes(x=type,y=value,fill=type))+facet_wrap(genename,scales = "free_y",shrink = TRUE,as.table = TRUE,strip.position = "bottom",labeller = label_parsed)+geom_bar(stat="identity",width=0.5,color="black",position = position_dodge(0.9))+
  geom_errorbar(aes(ymin=value-down,ymax=value+up),width=0.2,position=position_dodge(0.5))
p+labs(title = "qRT-PCR and RNA-seq ",x="gene list ",y="Relative expression ")+theme(plot.title = element_text(hjust = 0.5))

参数详解:
pg_mean是前面对应的data.frame
aes() 括号内的值,可以对应于前面数据库 里面的元素
x指定横坐标分类依据(可以是字符串或数字)
y纵坐标的值,
fill分类依据,可以是gene,也可以是type。根据作图需要确定。
facet_wrap对应还有一个facet_grid都是分面工具。区别见最上面。
~genename分面依据,确定每一个小图形的分类依据。可以更改为index~genename~index+genename
index的目的是为了指定每个小图的排列顺序。但是因为,标签会把index的值也显示,暂时未找到只显示其一的 作为每个小图label的方法 。
scales="free_y" 单独划定Y坐标轴, scales="free"是默认统一y轴
strip.position="bottom" 指定小图标签位置,在底部。默认是顶部。
labeller共4种模式,指定小图标签显示方式。

设置柱子的大小,间隔的大小 ,重叠类型

geom_bar(stat="identity",width=0.5,color="black",position = position_dodge(0.9))

设置误差棒,误差棒的值,宽度,位置

geom_errorbar(aes(ymin=value-down,ymax=value+up),width=0.2,position=position_dodge(0.5))

设置顺序(把genename按照index的顺序排序,后续出图小图标也按照这个顺序出图)

genename <- reorder(pg_mean$genename,pg_mean$index)

颠倒Y轴坐标方向

scale_y_reverse()

交换x轴和Y轴

coord_flip()

相关文章

网友评论

    本文标题:ggplot2画出条形图(facet_wrap)

    本文链接:https://www.haomeiwen.com/subject/vwvchctx.html