本文整理至ggtreeExtra文档,仅用于学习
作者:Shuangbin Xu and GuangChuang Yu
链接:https://bioconductor.org/packages/devel/bioc/vignettes/ggtreeExtra/inst/doc/ggtreeExtra.html
1 安装依赖(R4.1.1)
ggExtra release version
if (!requireNamespace("BiocManager", quietly=TRUE))
install.packages("BiocManager")
BiocManager::install("ggtreeExtra")
BiocManager::install("ggstar")
BiocManager::install("knitr") # 仅用于读文件
# 加载依赖
library(ggtreeExtra)
library(ggstar)
library(ggtree)
library(ggplot2)
library(treeio)
library(ggnewscale)
library(knitr) # 仅用于读文件
2 输入文件
# 树,文件路径
trfile <- system.file("extdata", "tree.nwk", package="ggtreeExtra")
# 点,文件路径
tippoint1 <- system.file("extdata", "tree_tippoint_bar.csv", package="ggtreeExtra")
# 第一圈,文件路径
ring1 <- system.file("extdata", "first_ring_discrete.csv", package="ggtreeExtra")
# 第二圈,文件路径
ring2 <- system.file("extdata", "second_ring_continuous.csv", package="ggtreeExtra")
3 base tree
tree <- read.tree(trfile)
data = fortify(tree)
head(data)
p <- ggtree(tree, layout="fan", open.angle=10, size=0.5)
p
add star
dat1 <- read.csv(tippoint1)
knitr::kable(head(dat1))
p2 <- p +
geom_fruit(
data=dat1,
geom=geom_star,
mapping=aes(y=ID, fill=Location, size=Length, starshape=Group),
position="identity",
starstroke=0.2
) +
scale_size_continuous(
range=c(1, 3), # the range of size.
guide=guide_legend(
keywidth=0.5,
keyheight=0.5,
override.aes=list(starshape=15),
order=2
)
) +
scale_fill_manual(
values=c("#F8766D", "#C49A00", "#53B400", "#00C094", "#00B6EB", "#A58AFF", "#FB61D7"),
guide="none"
) +
scale_starshape_manual(
values=c(1, 15),
guide=guide_legend(
keywidth=0.5,
keyheight=0.5,
order=1
)
)
p2
add heatmap
dat2 <- read.csv(ring1)
knitr::kable(head(dat2))
p3 <- p2 +
new_scale_fill() +
geom_fruit(
data=dat2,
geom=geom_tile,
mapping=aes(y=ID, x=Pos, fill=Type),
offset=0.08, # The distance between external layers, default is 0.03 times of x range of tree.
pwidth=0.25 # width of the external layer, default is 0.2 times of x range of tree.
) +
scale_fill_manual(
values=c("#339933", "#dfac03"),
guide=guide_legend(keywidth=0.5, keyheight=0.5, order=3)
)
p3
dat3 <- read.csv(ring2)
knitr::kable(head(dat3))
p4 <- p3 +
new_scale_fill() +
geom_fruit(
data=dat3,
geom=geom_tile,
mapping=aes(y=ID, x=Type2, alpha=Alpha, fill=Type2),
pwidth=0.15,
axis.params=list(
axis="x", # add axis text of the layer.
text.angle=-45, # the text angle of x-axis.
hjust=0 # adjust the horizontal position of text of axis.
)
) +
scale_fill_manual(
values=c("#b22222", "#005500", "#0000be", "#9f1f9f"),
guide=guide_legend(keywidth=0.5, keyheight=0.5, order=4)
) +
scale_alpha_continuous(
range=c(0, 0.4), # the range of alpha
guide=guide_legend(keywidth=0.5, keyheight=0.5, order=5)
)
p4
add bar
p5 <- p4 +
new_scale_fill() +
geom_fruit(
data=dat1,
geom=geom_bar,
mapping=aes(y=ID, x=Abundance, fill=Location), #The 'Abundance' of 'dat1' will be mapped to x
pwidth=0.4,
stat="identity",
orientation="y", # the orientation of axis.
axis.params=list(
axis="x", # add axis text of the layer.
text.angle=-45, # the text size of axis.
hjust=0 # adjust the horizontal position of text of axis.
),
grid.params=list() # add the grid line of the external bar plot.
) +
scale_fill_manual(
values=c("#F8766D", "#C49A00", "#53B400", "#00C094", "#00B6EB", "#A58AFF", "#FB61D7"),
guide=guide_legend(keywidth=0.5, keyheight=0.5, order=6)
) +
theme(#legend.position=c(0.96, 0.5), # the position of legend.
legend.background=element_rect(fill=NA), # the background of legend.
legend.title=element_text(size=7), # the title size of legend.
legend.text=element_text(size=6), # the text size of legend.
legend.spacing.y = unit(0.02, "cm") # the distance of legends (y orientation).
)
p5
模拟树和注释文件
set.seed(1024)
# generate a tree contained 100 tip labels.
tr <- rtree(100)
# generate three datasets, which are the same except the third column name.
dt <- data.frame(id=tr$tip.label, value=abs(rnorm(100)), group=c(rep("A",50),rep("B",50)))
df <- dt
dtf <- dt
colnames(df)[[3]] <- "group2"
colnames(dtf)[[3]] <- "group3"
base
test_p <- ggtree(tr, layout="fan", open.angle=0)
test_p
add bar one
test_p1 <- test_p +
geom_fruit(
data=dt,
geom=geom_bar,
mapping=aes(y=id, x=value, fill=group),
orientation="y",
stat="identity"
) +
new_scale_fill()
test_p1
add bar two
test_p2 <- test_p1 +
geom_fruit_list(
geom_fruit(
data = df,
geom = geom_bar,
mapping = aes(y=id, x=value, fill=group2),
orientation = "y",
stat = "identity"
),
scale_fill_manual(values=c("blue", "red")), # To map group2
new_scale_fill(), # To initialize fill scale.
geom_fruit(
data = dt,
geom = geom_star,
mapping = aes(y=id, x=value, fill=group),
size = 2.5,
color = NA,
starstroke = 0
)
) +
new_scale_fill()
test_p2
add bar three
test_p3 <- test_p2 +
geom_fruit(
data=dtf,
geom=geom_bar,
mapping = aes(y=id, x=value, fill=group3),
orientation = "y",
stat = "identity"
) +
scale_fill_manual(values=c("#00AED7", "#009E73"))
test_p3