r - 带热图的圆形树
问题描述
这个问题很琐碎,但我不能很好地处理。
我正在尝试绘制带有侧面热图的圆形树。
我正在使用ggtree
,但欢迎任何ggplo2
基于方法的方法。我不太了解gheatmap
功能的问题。
我想要:
1- 热图之后的名称
2- 热图之后的 2 个文本列(虽然可能具有相同的值,但我需要知道如何添加它)
3- 热图列名处理得很好,我们是否应该删除列名和为每个使用不同的颜色比例?无论解决方案落到哪里,都可能比现在更好
library(tidyverse)
library(ggtree)
library(treeio)
library(tidytree)
beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
beast_tree <- read.beast(beast_file)
genotype_file <- system.file("examples/Genotype.txt", package="ggtree")
genotype <- read.table(genotype_file, sep="\t", stringsAsFactor=F)
colnames(genotype) <- sub("\\.$", "", colnames(genotype))
p <- ggtree(beast_tree, mrsd="2013-01-01",layout = "fan", open.angle = -270) +
geom_treescale(x=2008, y=1, offset=2) +
geom_tiplab(size=2)
gheatmap(p, genotype, offset=5, width=0.5, font.size=3,
colnames_angle=-45, hjust=0) +
scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"),
values=c("steelblue", "firebrick", "darkgreen"), name="genotype")
提前致谢
更新:
我找到了一种更好的方法来绘制热图列的名称。
此外,我发现数据的简化对于清理一些提示标签很有用。现在,我只需要在热图之后添加两个文本列。
p <- ggtree(beast_tree)
gheatmap(
p, genotype, colnames=TRUE,
colnames_angle=90,
colnames_offset_y = 5,
colnames_position = "top",
) +
scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"),
values=c("steelblue", "firebrick", "darkgreen"), name="genotype")
更新 2:
一个非常糟糕的改进,我刚刚使用 ggplot 创建标签并与之合并patchwork
library(patchwork)
p$data %>%
ggplot(aes(1, y= y, label = label )) +
geom_text(size=2) +
xlim(NA, 1) +
theme_classic() +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()) -> adText
pp + adText
解决方案
大偏移值geom_tiplabel
:
p <- ggtree(beast_tree)
p1 <- gheatmap(
p, genotype, colnames=TRUE,
colnames_angle=-45,
colnames_offset_y = 5,
colnames_position = "bottom",
width=0.3,
hjust=0, font.size=2) +
scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"),
values=c("steelblue", "firebrick", "darkgreen"), name="genotype") +
geom_tiplab(align = TRUE, linesize=0, offset = 7, size=2) +
xlim_tree(xlim=c(0, 36)) +
scale_y_continuous(limits = c(-1, NA))
p1
使用ggtreeExtra
:
library(ggtreeExtra)
library(ggtree)
library(treeio)
library(ggplot2)
beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
genotype_file <- system.file("examples/Genotype.txt", package="ggtree")
tree <- read.beast(beast_file)
genotype <- read.table(genotype_file, sep="\t")
colnames(genotype) <- sub("\\.$", "", colnames(genotype))
genotype$ID <- row.names(genotype)
dat <- reshape2::melt(genotype, id.vars="ID", variable.name = "type", value.name="genotype", factorsAsStrings=FALSE)
dat$genotype <- unlist(lapply(as.vector(dat$genotype),function(x)ifelse(nchar(x)==0,NA,x)))
p <- ggtree(tree) + geom_treescale()
p2 <- p + geom_fruit(data=dat,
geom=geom_tile,
mapping=aes(y=ID, x=type, fill=genotype),
color="white") +
scale_fill_manual(values=c("steelblue", "firebrick", "darkgreen"),
na.translate=FALSE) +
geom_axis_text(angle=-45, hjust=0, size=1.5) +
geom_tiplab(align = TRUE, linesize=0, offset = 6, size=2) +
xlim_tree(xlim=c(0, 36)) +
scale_y_continuous(limits = c(-1, NA))
p2
推荐阅读
- android - Android:为按钮添加霓虹灯效果
- node.js - 是否可以将 webpack 与使用依赖注入的库一起使用?
- python - Tkinter(保存条目输入)
- javascript - 在 for 循环中指定延迟后循环
- reactjs - 页面路由更改时加载指示器不显示
- reactjs - 找不到 Babel 插件
- ios - 为什么大多数在线代码示例在符合协议功能时都使用 override 关键字?
- swift - CollectionView 上的自定义布局
- mysql - 我如何通过 ruby on rails 删除表 my.sql
- firebase - Flutter:在 Firebase Emulator 上调用 Firebase Cloud Function 时出现 PlatformException