Y叔ggplot2教程 配套PPT
ggplot2坐标轴最常用参数
https://zhuanlan.zhihu.com/p/25173606
facet_wrap分面操作
分面详解
https://zhuanlan.zhihu.com/p/27093478
添加误差棒
R的各种边距
R颜色大全
具体实现代码:
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()
指定x轴坐标范围
coord_cartesian(xlim=c(0,5))
如果需要转换x轴和y轴,同时还要指定坐标轴范围,coord_flip()和coord_cartesian(xlim=c(0,5))一起使用是无效的。
正确的表述是coord_flip(xlim=c(0,5))
指定坐标轴从0开始
scale_y_continuous(expand = c(0, 0))
scale_x_continuous(expand = c(0, 0))
注意
ggplot的theme,设置为只显示坐标轴和背景为白色
theme_classic()