Skip to content

Commit fb59cad

Browse files
committed
Simplify ML tree plotting code
Use ggtree data mapping instead of building each manual scale. This fixes an issue where a double legend could appear depending on the ggplot version. Transparency has been removed for clarity.
1 parent 765fff5 commit fb59cad

1 file changed

Lines changed: 28 additions & 49 deletions

File tree

workflow/scripts/report/phylo_plots.R

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ ggsave(
190190
)
191191

192192
# ML tree with context data
193-
# Tip node color
194-
tip.color <- ifelse(tree_ml$tip.label %in% study_names, "tip_label", NA)
195193

196194
# Internal nodes color
197195
# Node labels contain SH-aLRT/UFboot values
@@ -212,61 +210,42 @@ bootstrap.values <- sapply(
212210
aLRT.mask <- aLRT.values >= snakemake@params[["alrt_th"]]
213211
boot.mask <- bootstrap.values >= snakemake@params[["boot_th"]]
214212

215-
node.color <- case_when(
216-
aLRT.mask & boot.mask ~ "boot_alrt_pass"
217-
)
218-
219213
# MRCA for target samples
220214
log_info("Calculating MRCA of target samples")
221215
study.mrca <- getMRCA(tree_ml, study_names)
222216
study.mrca.clade <- extract.clade(tree_ml, study.mrca)
223217
study.mrca.clade.ntips <- Ntip(study.mrca.clade)
224218

219+
log_info("Building M-L tree annotation")
220+
tree_ml.max.tip.length <- max(node.depth.edgelength(tree_ml)[1:length(tree$tip.label)])
221+
tree_ml.ntips <- length(tree_ml$tip.label)
222+
tree_ml.nodes <- tree_ml$Nnode + tree_ml.ntips
223+
tree_ml.labels <- tree_ml$tip.label[1:tree_ml.nodes]
224+
tree_ml.node.pass <- c(rep(FALSE, tree_ml.ntips), aLRT.mask & boot.mask)
225+
226+
ml.tree.annot <- tibble(
227+
node = 1:tree_ml.nodes,
228+
) %>%
229+
mutate(
230+
Class = case_when(
231+
tree_ml.labels %in% study_names ~ legend.names["tip_label"],
232+
tree_ml.node.pass ~ legend.names["boot_alrt_pass"],
233+
TRUE ~ NA
234+
)
235+
)
236+
225237
log_info("Plotting M-L tree with context samples")
226-
p <- ggtree(tree_ml, layout = "circular") +
227-
geom_highlight(node = study.mrca, colour = "red", fill = "red", alpha = 0) +
228-
geom_point(
229-
aes(
230-
color = c(tip.color, node.color), # First node points in the tree are tip points, then internal nodes
231-
size = c(
232-
rep("tip_label", length(tree_ml$tip.label)),
233-
rep("boot_alrt_pass", length(tree_ml$node.label))
234-
),
235-
alpha = c(
236-
rep("tip_label", length(tree_ml$tip.label)),
237-
rep("boot_alrt_pass", length(tree_ml$node.label))
238-
)
239-
),
240-
show.legend = TRUE
241-
) +
242-
geom_treescale(x = 0.0008) +
243-
geom_rootedge(0.0005) +
244-
xlim(-0.0008, NA) +
238+
p <- ggtree(tree_ml, layout = "circular") %<+% ml.tree.annot +
239+
geom_highlight(node = study.mrca, colour = "red", alpha = 0) +
240+
geom_point(aes(color = Class, size = Class)) +
241+
geom_treescale(1.05 * tree_ml.max.tip.length) +
242+
geom_rootedge(0.05 * tree_ml.max.tip.length) +
243+
xlim(-tree_ml.max.tip.length / 3, NA) +
245244
scale_color_manual(
246-
name = "Class",
247-
values = tree_colors,
248-
labels = legend.names,
249-
na.value = NA,
250-
guide = guide_legend(
251-
override.aes = list(
252-
size = node.size,
253-
alpha = node.alpha,
254-
shape = 19
255-
)
256-
)
257-
) +
258-
scale_size_manual(
259-
name = "Class",
260-
values = node.size,
261-
labels = legend.names,
262-
guide = FALSE
245+
values = setNames(tree_colors[names(legend.names)], legend.names),
246+
na.translate = FALSE
263247
) +
264-
scale_alpha_manual(
265-
name = "Class",
266-
values = node.alpha,
267-
labels = legend.names,
268-
guide = FALSE
269-
)
248+
scale_size_discrete(range = c(2, 1), na.translate = FALSE)
270249

271250
ggsave(
272251
filename = snakemake@output[["tree_ml"]],
@@ -293,7 +272,7 @@ model <- lm(distance ~ date_interval, data = tempest)
293272
p.value <- summary(model)$coefficients[2,4]
294273

295274
# TREE STATS
296-
study.node <- tree_ml$node.label[study.mrca - length(tip.color)]
275+
study.node <- tree_ml$node.label[study.mrca - length(tree_ml$tip.label)]
297276
monophyletic <- ifelse(is.monophyletic(tree_ml, study_names), "are", "are not")
298277

299278
list(

0 commit comments

Comments
 (0)