衣带渐宽终不悔分享 http://blog.sciencenet.cn/u/tuqiang2014 在康河的柔波里,做一条水草, 向青草更青处漫溯。

博文

三元相图理解以及R实现

已有 13252 次阅读 2018-12-4 14:09 |系统分类:科研笔记

什么是三元相图

三元图是重心图的一种,它有三个变量,但需要三者总和为恒定值。在一个等边三角形坐标系中,图中某一点的位置代表三个变量间的比例关系。在群体遗传学中,它被称做Finetti图;在博弈论中,常被称为simplex图。

在高通量测序分析领域中,最常用的是两组间的比较,展示的方式有火山图、曼哈顿图等,而三组互相两两比较需要展示三个图吗?就没有更好的展示方式了吗?三元图的引入解决了这一问题。近年来,常用于展示三组间的相对基因表达或OTU丰度,即美观,又有意义。

三元相图怎么看

常用三角形来表示三元合金的成分,这样的三角形称为浓度三角形或成分三角形(CompositionTriangle)。常用的成分三角形是等边三角形和直角三角形以及等腰三角形。是应用最为广泛的还是等边三角形,这里也主要介绍等边三角形的三元相图。

如图所示:
oa+ob+oc=AB=BC=CA
由于oa=bC=WA
ob=Ac=WB
oc=Ba=WC
因此,可用
oa代表A组元的含量,
ob代表B组元的含量,
oc代表C组元的含量。

所以你需要画一个等边三角形还模拟一下,需要注意的是箭头的方向,还有就是三角形是等边的很是对称,所以要把字母标好。相信我,你会乱的,然后就好了。

  • 快速看谁多谁少
    快速理解位置意义:重心图,你看目标点离那个角近,就是它在相应的组中相对比例高;反之离某个角远,其相对比例也低。

  • 精确读三组比例

  1. 双平行线/小等边三角形法:由点向底边做两条平行线建立小正三角形,将底边分成三段,中间为顶部组所占比例,左段为右侧组比例,右段为左侧组比例。

垂线法:点向三条边做垂线,穿越的格数,即对角组中所占比例;
向各边零坐标方向作平行线法:按坐标系组的颜色和数值直接读各组比例;

  • 三元图规律
    三角形边平等线上的点,在平行线对应的顶点组所占比例是恒定的。
    顶点到底边直线上的点,上面任意点中两底角组相对比例恒定。
  • 三元图点大小的意义
    通常代表基因/OTU的相对表达/丰度,而且为了防止点过大或过小,数据通常还需要经过log2变换,将表达范围从0-1百万,转换为0-20的区间;如果是OTU的千分比,则转换为0-10的区间。

  • 三元图优点
    打破了普通两两比较火山图展示方式,可将三组两两比较和三组的相对关系展示在同张图中;
    两组只能比较一次,三组可以比较三次,可説的结果非常多;
    三是个非常好的数字,2组比较太常见且单调,4组及以上信息量大让人晕,三组正好高端大气上档次,而且还有内涵。

用R绘制三元相图

Software、
根据wikipedia的介绍目前有以下方法可以绘制三元相图:

当然,我们就介绍集中R做的三元相图。

用来作图的数据是这样的:

  • vcd 包
library(vcd)
data = read.table("otu_table_group.g.relative.xls",head=T,sep="\t",comment.char = "")
data1=head(data,n=12)
s=data1[cc("AA","BB","CC")]
smM<-as.matrix(s)

smM<-smM+0.000000000000000000001
colors<-c("#DC143C","#0000FF","#20B2AA","#FFA500","#9370DB","#98FB98","#F08080","#1E90FF","#7CFC00","#808000","#7B68EE","#483D8B")
ternaryplot(smM,prop_size = TRUE,grid_color = "black",labels_color = "black",main ="ternaryplot",labels = "outside",col=c("#DC143C","#0000FF","#20B2AA","#FFA500","#9370DB","#98FB98","#F08080","#1E90FF","#7CFC00","#808000","#7B68EE","#483D8B"))
grid_legend(0.85, 0.7, 19,colors,data1[,1],frame = FALSE,size=1)
dev.off()

图片.png

?ternaryplot()# 获得示例文档。
  • ggtern包

喜欢(习惯)ggplot的朋友可能会更加喜欢ggtern包。它是基于ggplot2的,参数设置方面也是一致的。

library(ggtern)
?ggtern()
s$DD<-apply(s,1,sum)
colors<-c("#DC143C","#0000FF","#20B2AA","#FFA500","#9370DB","#98FB98","#F08080","#1E90FF","#7CFC00","#808000","#7B68EE","#483D8B")
ggtern(data=s,aes(AA,BB,CC)) + 
  geom_point(aes(size=DD),col=colors) +          #define data geometry
  theme_showarrows() +    #draw labeled arrows beside axes
  ggtitle("My Favorite Color") +      #add title
  xlab("AA") +                       #replace default axis labels
  ylab("CC") +
  zlab("BB")+
  theme(legend.position="none")
grid_legend(0.85, 0.7, 19,colors,data1[,1],frame = FALSE,size=1)

如果变量较多可采用核密度估算,并使用轮廓显示结果.

#Or you can apply a color gradient to space between the contour lines
ggtern(data=s,aes(AA,BB,CC)) +                         #define data sources
  stat_density_tern(aes(fill=..level.., alpha=..level..),geom='polygon') +#now you need to use stat_density_tern
  scale_fill_gradient2(high = "red") +                                    #define the fill color
  guides(color = "none", fill = "none", alpha = "none")  +#we don't want to display legend items
  #theme_bvbg()
  #theme_bvbw()
  #theme_bluelight()
  theme_tropical()
  #theme_matrix()
  #theme_custom()
  #theme_void()
  #theme_rgbg()
  #theme_bw()
  #theme_minimal()
  #theme_rgbw()
  #theme_light()
  #theme_dark()

ggtern(data=s,aes(AA,BB,CC)) +  
stat_density_tern(aes(fill=..level.., alpha=..level..), geom='polygon') +
  scale_fill_gradient2(high = "blue") +  
  geom_point() +
  theme_showarrows() +
  ggtitle("My Favorite Color") +
  xlab("AA") + 
  ylab("BB") +
  zlab("CC") +
  guides(color = "none", fill = "none", alpha = "none")

  • Ternary

如果你想一步一步地构建一个三元相图那么Ternary也许是一个不错的选择。

#install.packages('Ternary')
library('Ternary')
par(mfrow=c(2, 2), mar=rep(0.5, 4))
for (dir in c('up', 'right', 'down', 'le')) {
  TernaryPlot(point=dir, atip='A', btip='B', ctip='C', alab='Aness', blab='Bness', clab='Cness')
  TernaryText(list(A=c(10, 01, 01), B=c(01, 10, 01), C=c(01, 01, 10)), col=cbPalette8[4], font=2)
}

par(mfrow=c(1, 2), mar=rep(0.3, 4))
TernaryPlot(alab="Redder\u2192", blab="Greener \u2192", clab="Bluer \u2192",
            point='right', lab.cex=0.8, grid.minor.lines = 0,
            grid.lty='solid', col=rgb(0.9, 0.9, 0.9), grid.col='white', 
            axis.col=rgb(0.6, 0.6, 0.6), ticks.col=rgb(0.6, 0.6, 0.6),
            padding=0.08)
data_points <- list(
  R = c(255, 0, 0), 
  O = c(240, 180, 52),
  Y = c(210, 222, 102),
  G = c(111, 222, 16),
  B = c(25, 160, 243),
  I = c(92, 12, 243),
  V = c(225, 24, 208)
)
AddToTernary(points, data_points, bg=vapply(data_points, function (x) rgb(x[1], x[2], x[3], 128, maxColorValue=255), character(1)), pch=21, cex=2.8)
AddToTernary(text, data_points, names(data_points), cex=0.8, font=2)
legend('bottomright', 
       pch=21, pt.cex=1.8,
       pt.bg=c(rgb(255, 0, 0,   128, NULL, 255), 
               rgb(240, 180,  52, 128, NULL, 255),
               rgb(210, 222, 102, 128, NULL, 255),
               rgb(111, 222,  16, 128, NULL, 255)),
       legend=c('Red', 'Orange', 'Yellow', 'Green'), 
       cex=0.8, bty='n')

###
# Next plot:
###
TernaryPlot('Steam', 'Ice', 'Water', 
            grid.lines=5, grid.lty='dotted',
            grid.minor.lines = 1, grid.minor.lty='dotted',
            point='West')
HorizontalGrid()
middle_triangle <- matrix(c(
  30, 40, 30,
  30, 30, 40,
  55, 20, 25
), ncol=3, byrow=TRUE)
TernaryPolygon(middle_triangle, col='#aaddfa', border='grey')
TernaryLines(list(c(0, 100, 0), middle_triangle[1, ]), col='grey')
TernaryLines(list(c(0, 0, 100), middle_triangle[2, ]), col='grey')
TernaryLines(list(c(100, 0, 0), middle_triangle[3, ]), col='grey')


Ternary plot
百科三元相图
扩增子图片解读7三元图:美的不要不要的,再多用也不过分
Creating ternary plots with Ternary
DRAFT Triad Data Visualizations: Part 1
Legends (ggplot2)/)



https://blog.sciencenet.cn/blog-1835014-1149803.html

上一篇:NGS通识第零讲||NGS通识
下一篇:ggtree绘制进化树
收藏 IP: 123.151.22.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

全部作者的精选博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 18:03

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部