前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >画一个sloan图~

画一个sloan图~

作者头像
Listenlii-生物信息知识分享
发布2020-11-19 17:19:50
4560
发布2020-11-19 17:19:50
举报

本文从前文 MicEco:计算Sloan随机性的另一方法 得到的结果中继续画图,希望得到最开始mSystems中类似的图。

代码语言:javascript
复制
 1#随便找了一个OTU表
 2otu = read.table(file = "otu.txt",sep="\t",row.names=1,header=T)
 3
 4library(MicEco)
 5library(ggplot2)
 6res = neutral.fit(t(otu))
 7
 8##########画图
 9m = res[[1]][1]
10r2 = res[[1]][3]
11out = res[[2]]

先画三条线,预测的发生率及上下界

代码语言:javascript
复制
1p1 = ggplot() +
2    geom_line(data = out,aes(x=log(p),y=freq.pred),size = 1.2,linetype = 1)+
3    geom_line(data = out,aes(x=log(p),y=Lower),size = 1.2,linetype = 2)+
4    geom_line(data = out,aes(x=log(p),y=Upper),size = 1.2,linetype = 2)+
5    xlab("log10(mean relative abundance)")+ylab("Occurrence frequency")
6p1

加散点图

代码语言:javascript
复制
 1#判断点的位置,分成三组,红-绿-黄
 2require(plyr)
 3out2 = mutate(out, group=NA)
 4out2$group[out[,2]<out[,4]]="yellow" ##低于下界
 5out2$group[out[,2]>out[,5]]="red" ##高于上界
 6out2$group[(out[,2]>=out[,4])&(out[,2]<=out[,5])]="green"##中间
 7
 8mycols<-c("green","red1","orange")
 9p2 = p1 + geom_point(data = out2,aes(x=log(p),y=freq,color = group),size = 2)+
10         scale_colour_manual(values = mycols)+
11      annotate("text",x=-12.5,y=0.25,label=paste("m = ",round(m,3),sep=''),size=7)+
12      annotate("text",x=-12.5,y=0.3,label=paste("R2 = ",round(r2,3),sep=''),size=7)
13p2

格式调整

代码语言:javascript
复制
 1plot_theme = theme(panel.background=element_blank(),
 2                   panel.grid=element_blank(),
 3                   axis.line.x=element_line(size=.5, colour="black"),
 4                   axis.line.y=element_line(size=.5, colour="black"),
 5                   axis.ticks=element_line(color="black"),
 6                   axis.text=element_text(color="black", size=24),
 7                   legend.position="none",   ##right
 8                   legend.background=element_blank(),
 9                   legend.key=element_blank(),
10                   legend.text= element_text(size=24),
11                   text=element_text(family="sans", size=24)
12)
13p3 = p2 + plot_theme;p3

计算三组OTU的比例并画饼图

代码语言:javascript
复制
 1low = nrow(out2[out2[,6]== "yellow",])
 2med = nrow(out2[out2[,6]== "green",])
 3high = nrow(out2[out2[,6]== "red",])
 4
 5type <- c('med','high','low')
 6nums <- c(med,high,low)
 7df <- data.frame(type = type, nums = nums)
 8label_value <- paste('', round(df$nums/sum(df$nums) * 100, 1), '%', sep = '')
 9label_value
10label <- paste(df$type, label_value, sep = ' ')
11label
12p4 <- ggplot(data = df, aes(x = 1, y = nums, fill = type)) + 
13  geom_bar(stat = 'identity', position = 'stack', width = 0.5)+
14  scale_fill_manual(name='', 
15                    labels=c(label[2], label[3], label[1]),
16                    values=c("red1","orange","green"))+
17  coord_polar(theta = 'y')+
18  labs(x = '', y = '', title = '')+
19  theme(axis.text = element_blank())+
20  theme(axis.ticks = element_blank())+
21  theme(legend.position = "right")
22 
24p4
25p4 = p4+theme(panel.background=element_blank(),
26              panel.grid=element_blank(),
27              legend.background=element_blank(),
28              legend.key=element_blank(),
29              legend.text= element_text(size=24))
30
31p4

拼图

代码语言:javascript
复制
1require(ggimage)
2g <- p3 + geom_subview(subview = p4 + theme_void(), x=-11.5, y=0.75, w=5, h=5)
3g
4#dev.off()

现在已经比较像了。当然还可以继续美化~

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Listenlii 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档