目标:绘制每一种亚类的堆叠图,使呈现效果更好。是时候要挑战一下自己的舒适区了。传统的barplot实在是不行了。
1。首先综述一下目前用到了实现这一功能,用的是哪些package。
library(ggplot2) #似乎ggplot2就能实现这一效果。
2。其次创建输入数据矩阵。
data<-data.frame(Sample<-c(rep('control1',3),rep('control2',3),rep('control3',3),rep('treat1',3),rep('treat2',3),rep('treat3',3),rep('treat4',3)), contion<-rep(c('Cell','Tissue','Organ'),7), value<-c(503,264,148,299,268,98,363,289,208,108,424,353,1,495,168,152,367,146,48,596,143))
colnames(data)=c('sample',"contion","value")
创建出数据集长这样:
sample contion value
1 control1 Cell 503
2 control1 Tissue 264
3 control1 Organ 148
4 control2 Cell 299
5 control2 Tissue 268
6 control2 Organ 98
7 control3 Cell 363
8 control3 Tissue 289
9 control3 Organ 208
10 treat1 Cell 108
3。设置参数,绘图。
ggplot(data,mapping = aes(Sample,value,fill=contion))+geom_bar(stat='identity',position='fill') +labs(x = 'Sample',y = 'frequnency') +theme(axis.title =element_text(size = 16),axis.text =element_text(size = 14, color = 'black'))+theme(axis.text.x = element_text(angle = 45, hjust = 1))+coord_flip()
#实际上就很简单,把相应的参数放进槽里面即可
到这边的话,就满足需求了。对于我自己的数据的话,可能需要构建一个能够使用的数据矩阵。然后,构建完成数据矩阵之后,剩下的就很简单了。
参考链接:
https://www.jianshu.com/p/a37dac699490