我正在使用 R 包
ggsplitnet
中的 tanggle
函数来绘制系统发育网络。我想创建一个图,在网络的尖端显示各个样本名称和彩色符号,其中颜色代表任意定义的组。
这是演示该问题的简化示例:
# Load relevant packages
library(phangorn)
library(ggtree)
library(tanggle)
# Read in a phylogenetic network in nexus format
fdir <- system.file("extdata/trees", package = "phangorn")
Nnet <- phangorn::read.nexus.networx(file.path(fdir, "woodmouse.nxs"))
# Plot the network with sample names at the tips
ggsplitnet(Nnet) +
geom_tiplab2()
# Define groups
mygroups <- rep(LETTERS[1:3], each = 5)
# Replace sample names with group names
Nnet$translate$label <- mygroups
# Plot the network with different colors for groups
ggsplitnet(Nnet) +
geom_tippoint(aes(color = label), size = 5)
问题是我无法在同一图中绘制各个样本名称和组的彩色符号。看来
ggsplitnet
函数只能识别存储在 label
中 cono.net$translate
列中的数据。例如,我尝试向网络对象添加一个新列,如 Nnet$translate$groups <- mygroups
,但它不起作用。
任何有关如何实现这一目标的建议将不胜感激!
实现此目的的一种方法是使用
ggtree::fortify()
将 Nnet 转换为数据帧,然后将组值分配给 fortified_Nnet$isTip 作为新列,例如“组”。
我添加了两个绘图选项:
ggsplitnet()
结合使用,保留原始标签位置ggplot()
,如果不满足您的需求,可能需要进行修改。请注意,已包含用于示例图的
ggsave()
,较低的宽度/高度值可能会导致标签被裁剪。
library(phangorn)
library(ggtree)
library(tanggle)
# Example data
fdir <- system.file("extdata/trees", package = "phangorn")
Nnet <- phangorn::read.nexus.networx(file.path(fdir, "woodmouse.nxs"))
# Define groups
mygroups <- rep(LETTERS[1:3], each = 5)
# Create dataframe of Nnet using fortify()
dfNnet <- fortify(Nnet)
# Add group information to dfNnet
dfNnet$group <- NA
dfNnet$group[dfNnet$isTip] <- mygroups
图1:
# Plot using Nnet and dfNnet
ggsplitnet(Nnet) +
geom_tippoint(data = dfNnet,
aes(colour = group),
size = 5) +
geom_tiplab2(aes(label = label)) +
coord_cartesian(clip = "off") +
theme(plot.margin = margin(1, 1, 1, 1, "cm"))
ggsave("plot1.jpg",
width = 7,
height = 7,
dpi = 150)
情节2:
ggplot(dfNnet, aes(x = x, y = y)) +
geom_segment(aes(xend = xend, yend = yend),
colour = "grey") +
geom_point(data = dfNnet[dfNnet$isTip, ],
aes(colour = group),
size = 5) +
geom_text(data = dfNnet[dfNnet$isTip, ],
aes(label = label),
vjust = -1,
hjust = 1) +
theme_void() +
coord_cartesian(clip = "off") +
theme(plot.margin = margin(0, 1, 0, 1, "cm"))
ggsave("plot2.jpg",
width = 7,
height = 7,
dpi = 150)