赞
踩
参考前文:R绘图笔记 | R语言绘图系统与常见绘图函数及参数
关于绘图图,前面介绍了一些:
这里介绍散点分布图与柱形分布图,这些图形在文章中是很常见的,也是必须要掌握的。
一.读入数据
如果你想获取该数据用于自己练习,下面是获取数据的地址:
https://docs.qq.com/sheet/DV0dxREV1YkJ0ZmVj
数据格式是这样的。
数据第A列是病人ID,B~E列是临床信息,其他列是病人的RNAseq数据。
你可以保存副本导出,然后自己读入。
- library(ggplot2)
- library(RColorBrewer)
- library(SuppDists) #提供rJohnson()函数
- library(ggbeeswarm)
data <- read.csv("BioInfoNotesData1.csv",row.names = 1)
假如我们需要绘制某基因在不同分期的表达情况。
- f1.data <- data[,c(1,5)]
- colnames(f1.data) <- c("Stage","Value")
- summary(f1.data$Stage)
- summary(f1.data$Stage)
- N Stage I Stage II Stage III Stage IV
- 11 75 176 128 64
先检查数据是否有缺失值,分期信息不知用N来表示,可以删除这些数据。
- f1.data<-f1.data[f1.data$Stage!="N",]
- head(f1.data)
- BioinfoNotes>head(f1.data)
- Stage Value
- TCGA-3L-AA1B-01 Stage I 7.04
- TCGA-4N-A93T-01 Stage III 7.23
- TCGA-4T-AA8H-01 Stage II 6.61
- TCGA-5M-AAT4-01 Stage IV 7.56
- TCGA-5M-AAT6-01 Stage IV 4.99
- TCGA-5M-AATE-01 Stage II 7.41
二.绘图
1.散点抖动图
- ggplot(f1.data, aes(Stage, Value))+
- geom_jitter(aes(fill = Stage),position = position_jitter(0.3),shape=21, size = 2)+
- scale_fill_manual(values=c(brewer.pal(7,"Set2")[c(1,2,4,5)]))+
- theme_classic()+
- theme(panel.background=element_rect(fill="white",colour="black",size=0.25),
- axis.line=element_line(colour="black",size=0.25),
- axis.title=element_text(size=13,face="plain",color="black"),
- axis.text = element_text(size=12,face="plain",color="black"),
- legend.position="none"
- )
2.蜂群图
- #蜂群图
- ggplot(f1.data, aes(Stage, Value))+
- geom_beeswarm(aes(fill = Stage),shape=21,colour="black",size=2,cex=2)+
- scale_fill_manual(values= c(brewer.pal(7,"Set2")[c(1,2,4,5)]))+
- xlab("Stage")+
- ylab("Value")+
- theme_classic()+
- theme(panel.background=element_rect(fill="white",colour="black",size=0.25),
- axis.line=element_line(colour="black",size=0.25),
- axis.title=element_text(size=13,face="plain",color="black"),
- axis.text = element_text(size=12,face="plain",color="black"),
- legend.position="none"
- )
3.点阵图
- ggplot(f1.data, aes(Stage, Value))+
- geom_dotplot(aes(fill = Stage),binaxis='y', stackdir='center', dotsize = 0.6)+
- scale_fill_manual(values=c(brewer.pal(7,"Set2")[c(1,2,4,5)]))+
- theme_classic()+
- theme(panel.background=element_rect(fill="white",colour="black",size=0.25),
- axis.line=element_line(colour="black",size=0.25),
- axis.title=element_text(size=13,face="plain",color="black"),
- axis.text = element_text(size=12,face="plain",color="black"),
- legend.position="none"
- )
-
-
4.带误差线的散点分布图
- ggplot(f1.data, aes(Stage, Value))+
- geom_jitter(aes(fill = Stage),position = position_jitter(0.3),shape=21, size = 2,color="black")+
-
- scale_fill_manual(values=c(brewer.pal(7,"Set2")[c(1,2,4,5)]))+
-
- stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
- geom="pointrange", color = "black",size = 1.2)+
- stat_summary(fun.y="mean", fun.args = list(mult=1),
- geom="point", color = "white",size = 4)+
-
- theme_classic()+
- theme(panel.background=element_rect(fill="white",colour="black",size=0.25),
- axis.line=element_line(colour="black",size=0.25),
- axis.title=element_text(size=13,face="plain",color="black"),
- axis.text = element_text(size=12,face="plain",color="black"),
- legend.position="none"
- )
5.带误差线的柱形分布图
- ggplot(f1.data, aes(Stage, Value))+
- stat_summary(mapping=aes(fill = Stage),fun.y=mean, fun.args = list(mult=1),geom='bar',colour="black",width=.7) +
- stat_summary(fun.data = mean_sdl, fun.args = list(mult=1),geom='errorbar', color='black',width=.2) +
- scale_fill_manual(values=c(brewer.pal(7,"Set2")[c(1,2,4,5)]))+
- ylim(0,7.5)+
- theme_classic()+
- theme(panel.background=element_rect(fill="white",colour="black",size=0.25),
- axis.line=element_line(colour="black",size=0.25),
- axis.title=element_text(size=13,face="plain",color="black"),
- axis.text = element_text(size=12,face="plain",color="black"),
- legend.position="none"
- )
6.带误差线柱形与抖动图
- ggplot(f1.data, aes(Stage, Value))+
- stat_summary(fun.y=mean, fun.args = list(mult=1),geom='bar',colour="black",fill="white",width=.7) +
- stat_summary(fun.data = mean_sdl,fun.args = list(mult=1), geom='errorbar', color='black',width=.2) +
- geom_jitter(aes(fill = Stage),position = position_jitter(0.2),shape=21, size = 2,alpha=0.9)+
- scale_fill_manual(values=c(brewer.pal(7,"Set2")[c(1,2,4,5)]))+
- theme_classic()+
- theme(panel.background=element_rect(fill="white",colour="black",size=0.25),
- axis.line=element_line(colour="black",size=0.25),
- axis.title=element_text(size=13,face="plain",color="black"),
- axis.text = element_text(size=12,face="plain",color="black"),
- legend.position="none"
- )
参考资料:
1.R语言数据可视化之美,张杰/著
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。