origin三元相图_三元相图怎么看怎么画(附R代码示例)

f432a98e9504?utm_campaign=maleskine

什么是三元相图

三元图是重心图的一种,它有三个变量,但需要三者总和为恒定值。在一个等边三角形坐标系中,图中某一点的位置代表三个变量间的比例关系。在群体遗传学中,它被称做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组元的含量。

f432a98e9504?utm_campaign=maleskine

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

快速看谁多谁少

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

精确读三组比例

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

垂线法:点向三条边做垂线,穿越的格数,即对角组中所占比例;

向各边零坐标方向作平行线法:按坐标系组的颜色和数值直接读各组比例;

三元图规律

三角形边平等线上的点,在平行线对应的顶点组所占比例是恒定的。

顶点到底边直线上的点,上面任意点中两底角组相对比例恒定。

三元图点大小的意义

通常代表基因/OTU的相对表达/丰度,而且为了防止点过大或过小,数据通常还需要经过log2变换,将表达范围从0-1百万,转换为0-20的区间;如果是OTU的千分比,则转换为0-10的区间。

三元图优点

打破了普通两两比较火山图展示方式,可将三组两两比较和三组的相对关系展示在同张图中;

两组只能比较一次,三组可以比较三次,可説的结果非常多;

三是个非常好的数字,2组比较太常见且单调,4组及以上信息量大让人晕,三组正好高端大气上档次,而且还有内涵。

用R绘制三元相图

Software、

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

R packages:

Ternary, which employs the native plot functions;

Plot-ly, which uses a bespoke suite of plotting functions;

vcd, which contains the function ternaryplot;

ggtern, an extension to ggplot2.

ternary, a python library for ternary plotting

ternplot for Matlab

ternaryplot.com online tool for ternary plotting

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

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

f432a98e9504?utm_campaign=maleskine

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[c("AA","BB","CC")]

smM

smM

colors

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()

f432a98e9504?utm_campaign=maleskine

图片.png

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

ggtern包

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

library(ggtern)

?ggtern()

s$DD

colors

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)

f432a98e9504?utm_campaign=maleskine

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

#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()

f432a98e9504?utm_campaign=maleskine

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")

f432a98e9504?utm_campaign=maleskine

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)

}

f432a98e9504?utm_campaign=maleskine

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

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

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')

f432a98e9504?utm_campaign=maleskine

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值