为什么要写这篇文档,昨天在教一个朋友做数据可视化发现即使是16s分析中一个简单的物种组成图对新手来说也是挺难的,数据的导入,过滤,格式转换等;这一番操作是实在是繁琐,曾经有大佬指点过我如何制作R包,于是乎经过一番学习终于做出了自己的第一个R包 ggtest
名字随便写的,感兴趣的小伙伴可以联系我获取资料
曾经的你绘制物种组成图
可以看到如果纯自己写代码,需要如下步骤没有一定的基础是不太可能顺利完成的,那么使用我的R包该如何操作,请继续往下看
library(tidyverse)
library(magrittr)
colors <-c("#E41A1C","#1E90FF","#FF8C00","#4DAF4A","#984EA3",
"#40E0D0","#FFC0CB","#00BFFF","#FFDEAD","#90EE90",
"#EE82EE","#00FFFF","#F0A3FF", "#0075DC",
"#993F00","#4C005C","#2BCE48","#FFCC99",
"#808080","#94FFB5","#8F7C00","#9DCC00",
"#C20088","#003380","#FFA405","#FFA8BB",
"#426600","#FF0010","#5EF1F2","#00998F",
"#740AFF","#990000","#FFFF00")
computed_persent <- function(path) {
data <- path %>%
read.delim(check.names = FALSE, row.names = 1)
data2 <- data %>%
mutate(sum = rowSums(.), persent = sum / sum(sum) * 100,
sum = NULL,) %>%
rbind(filter(., persent < 1) %>% colSums()) %>%
mutate(OTU_ID = c(data %>% rownames(), "others"))
filter(data2[1:(nrow(data2) - 1),], persent > 1) %>%
rbind(data2[nrow(data2),]) %>%
select(ncol(.), 1:(ncol(.) - 2)) %>%
set_rownames(seq_len(nrow(.))) %>%
return()
}
path <- "phylumt.xls"
a1 <- computed_persent(path) %>% melt()
a2 <- "group.xls" %>% read.delim()
a4 <- NULL
for (i in seq_len(nrow(a1))) {
a4[i] <- a2[which(a2[, 1] == a1[i, 2]), 2] }
a1[, 4] <- a4
ggplot(a1,aes(variable,value,fill=OTU_ID))+
geom_bar(stat="identity",position = "fill")+
facet_grid(. ~ V4,scales = "free",space="free_x")+
labs(x="",y="Proportions")+
scale_fill_manual(values = colors)+labs(fill="")+
theme(legend.title=element_blank())+
scale_y_continuous(expand=c(0,0))+theme_bw()
现在的你绘制物种组成图
library(tidyverse)
library(magrittr)
library(ggtest)
otu_filter("genus.xls") %>% ggbar()
还是相同的代码,不过是对函数的封装,可以看到使用非常的便捷,如果您没有过高的要求完全可以满足需求
可以使用如下代码获取内置的示例数据进行绘图,只需要保证group文件的格式与示例文件一致即可
library(tidyverse)
library(magrittr)
library(ggtest)
data("genus")
data("group")
write.table(genus,file="genus.xls",sep="\t",row.names = F,quote = F)
write.table(group,file="group.xls",sep="\t",row.names = F,quote = F)
otu_filter("genus.xls") %>% ggbar()
如果需要获取本文数据,欢迎关注我的公众号
- 后台回复
ggtest
R语言数据分析指南
本公众号主要分享数据可视化的一些经典案例及一些生信相关的知识供大家学习