画图的时候,排序是个很重要的技巧,比如有时候会看下基因组每条染色体上的SNP的标记数量,这个时候直接做条形图是一种比较直观的方法,下面我们结合实际例子来看下:
1 chr<-paste("chr",c(1:18,"X","Y"),sep="")
2 set.seed(2)
3 num<-runif(20,100,5000)
4 df<-data.frame(chr=chr,num=num)
5 df
1 barplot(t(as.matrix(df$num)),col="cyan",border = NA,names.arg = df$chr)
1 library(ggplot2)
2 ggplot(df,aes(y=num,x=chr,fill=chr))+geom_bar(stat = 'identity')
1 ggplot(df,aes(y=num,x=factor(chr,levels=(chr)),fill=chr))+
2 geom_bar(stat = 'identity')
1 ggplot(df,aes(y=num,fill=chr,
2 x=factor(chr,levels=(paste("chr",c("X","Y",1:18),sep="")))))+
3 geom_bar(stat = 'identity')