R语言绘图

R绘图的生物学家(4): Filtering and clean

2017-10-15  本文已影响83人  lxmic
解决一些问题之后的统计柱状图
修改之前的原始图

本节的主要内容为数据的过滤和清洗,把自己需要的感兴趣的数据通过可视化展现出来。

直接上代码,进行说明解释

# 利用summary函数进行查看chrom和type变量
summary(my_data$chrom)
summary(my_data$type)
# 解决第3节留下来的问题
#############     Now let's address those issues     ###############
# 移除x轴上的chr字符,只存在数字。gsub是R中的字符串替换函数,要移除chr字符,只要将匹配到的字符换成NOTHING就可以
# Remove the "chr" prefix
my_data$chrom <- factor(gsub("chr", "", my_data$chrom))
# 查看结果,只剩下数字字符
# See the result
summary(my_data$chrom)

ggplot(my_data,aes(x=chrom,fill=type)) + geom_bar()
# 要将染色体的顺序重新排列,重新构建一个染色体向量
# Reorder the chromosomes numerically
seq(1,22)
c(seq(1,22),"X","Y")
# 将染色体字符型变量转换成有序型变量,重新定义这个顺序,用参数levels来定义
my_data$chrom <- factor(my_data$chrom, levels=c(seq(1,22),"X","Y"))
summary(my_data$chrom)
ggplot(my_data,aes(x=chrom,fill=type)) + geom_bar()
# 重新查看类型
summary(my_data$type)
# 利用%in%来取我们敢兴趣的type
# Filter to just a few types I'm interested in
my_data <- my_data[my_data$type %in% c("1_Active_Promoter","4_Strong_Enhancer","8_Insulator"), ]

summary(my_data$type)
# 利用plyr包中的revaule()函数来进行重命名type
# Rename the types
library(plyr) 
# this library has a useful revalue() function
my_data$type <- revalue(my_data$type, c("1_Active_Promoter"="Promoter", "4_Strong_Enhancer"="Enhancer","8_Insulator"="Insulator"))
summary(my_data$type)
# Check the plot again
ggplot(my_data,aes(x=chrom,fill=type)) + geom_bar()

通过有选择的展示结果,很显然美化很多,图片变得更加的美。

相关内容参考:
R中字符串处理:http://www.jianshu.com/p/22e5307da27a
revaule函数
https://rstudio-pubs-static.s3.amazonaws.com/156538_36d10afb61984db6a98b8354d67aee40.html

上一篇下一篇

猜你喜欢

热点阅读