-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-trees.R
43 lines (41 loc) · 1.04 KB
/
plot-trees.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Load packages
library(stringr)
library(ape)
library(purrr)
# Define helper-functions
open_png_for_tree <- function(filename, tree) {
# Opens a PNG device with the same filename as a Newick-tree
new_filename <- str_replace(filename, "nwk", "png")
png(new_filename,
width = Ntip(tree) * 3.5, height = Nnode(tree) * 1.5,
units = "cm", res = 300
)
}
plot_tree <- function(filename) {
# Reads in a Newick-tree, plots it nicely, saves plot as PNG-file
tree <- read.tree(filename)
open_png_for_tree(filename, tree)
plot(tree,
# Plot-window settings
x.lim = c(0.5, Ntip(tree) + 0.5),
y.lim = c(0, Nnode(tree) + 0.5),
no.margin = TRUE,
# Tree settings
type = "cladogram",
direction = "upwards",
# Edge settings
edge.width = 2,
# Label settings
show.node.label = TRUE,
srt = -90,
adj = 0.25,
label.offset = 0.25
)
dev.off()
}
# Use these functions on the Newick-trees in trees/
tree_filenames <- list.files("trees",
pattern = "nwk",
full.names = TRUE
)
walk(tree_filenames, plot_tree)