ggtree中基于外部数据集的着色标签 [英] Colouring labels based on external dataset in ggtree

查看:24
本文介绍了ggtree中基于外部数据集的着色标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .tre 格式的系统发育树和随附的数据集.树的确切形式并不重要,它只是一棵随机的系统发育树.数据集有两列:namescolours.

I have a phylogenetic tree in .tre format and accompanying dataset. The exact form of the tree does not matter, it is just a random phylogenetic tree. The dataset has two columns: names and colours.

在绘制这样的树时,我很可能会从随附的数据集中向树添加彩色点(两种不同的颜色).问题是当我使用以下代码时:

When plotting such tree, I would very likely add coloured points (two different colours) to the tree from that accompanying dataset. Problem is that when I am using following piece of code:

ggtree(RANDOMTREE) + geom_tippoint(pch=16, col=RANDOMDATA$color) + geom_tiplab(offset=0.1)

当然,它为点着色,但颜色具有它们在随附数据集中的顺序.

it colours the points but colours have the order they have in the accompanying dataset, of course.

但我想将基于树中物种的名称的颜色与数据集中的颜色相匹配(它们的格式相同,但顺序不同).我还没有弄清楚.你能帮我解决这个问题吗?

But I would like to match the colours based on the names of the species in a tree with the one in the dataset (they are same format, but different order). I did not figure that out, yet. Can you please help me with this?

非常感谢.

示例代码:

source("https://bioconductor.org/biocLite.R")
biocLite("ggtree")
library(ggtree)

tree<-read.tree(text="(spec1,((spec2,(spec9,(spec3,spec5))),spec8,(spec6,(spec7,spec4))));")
dataset1<-data.frame("name" = c("spec1","spec2","spec3","spec4","spec5","spec6","spec7","spec8","spec9"), "colour" = c("red","red","blue","red","red","blue","blue","red","blue"))

ggtree(tree) + geom_tiplab() + geom_tippoint(pch=16, col=as.factor(dataset1$colour))

我得到了什么:错误标记的树

我想得到的:正确标记的树

推荐答案

我可以得到正确的分组,但不能立即获得正确的颜色

I can get the right grouping, but not the right colour off the bat

p <- ggtree(tree) + geom_tiplab()
p <- p %<+% dataset1 + geom_tippoint(pch=16, aes(col=colour))
p

我用这个作为参考:https://aschuerch.github.io/posts/2017-04-24-blog-post-1.包有错误的文档.您可以通过切换红色"和蓝色"来实现您想要的:p

I used this for reference: https://aschuerch.github.io/posts/2017-04-24-blog-post-1. Package has bad documentation. You could achieve what you want by switching the "red" and the "blue" :p

它采用颜色的顺序并将其与内置的色标配对.因此,如果比例以 (red, blue) 开头,而您的系列是 (blue, red),则按该顺序匹配.有道理吗?

Its taking the ordering of the colours and pairing it with an inbuilt colour scale. So if the scale starts with (red, blue), and your series is (blue, red), its matching up in that order. Make sense?

安装这个包是一场噩梦,如果有一个更简单的包,比如https://cran.r-project.org/web/packages/data.tree/vignettes/data.tree.html,我建议尝试其他人.它卸载了我的许多核心软件包,例如dplyr 和 data.table,它有大量的依赖

edit: Installing this package was a nightmare, if there is a simpler package like https://cran.r-project.org/web/packages/data.tree/vignettes/data.tree.html, I'd suggest trying others out. It uninstalled so many of my core packages e.g. dplyr and data.table, it has a ridiculous number of dependencies

这篇关于ggtree中基于外部数据集的着色标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆