treeplot
Plots a tree. Assumes it is passed the root node of the tree that has AbstractTrees.children() defined. Thus, all nodes should be reachable by using AbstractTrees.PreOrderDFS() iterator on the argument tree.
tree = ((:a, :b), (:c, (:d, :e)))
fig, ax, tp = treeplot(tree)
Public attributes
Further annotations can be added from treelabels, treescatter, treecladelabel, and treehilight.
If further customization is needed one can access the geometries computed by treeplot. Specifically
tp.orderedpoints
tp.orderedpoints[]9-element Vector{Point{2, Float32}}:
[0.0, 2.625]
[1.0, 1.5]
[2.0, 1.0]
[2.0, 2.0]
[1.0, 3.75]
[2.0, 3.0]
[2.0, 4.5]
[3.0, 4.0]
[3.0, 5.0]can be used to get the coordinates of each node and leaf respectively. The order of these points is the same as the order of nodes in AbstractTrees.PreOrderDFS(tree).
It can be used directly in a Makie.scatter! plot or any other plot where we want to plot over each node.
scatter(tp.orderedpoints; markersize=20)
tp.nodepoints
tp.nodepoints[]OrderedCollections.OrderedDict{Any, Point{2, Float32}} with 9 entries:
:a => [2.0, 1.0]
:b => [2.0, 2.0]
(:a, :b) => [1.0, 1.5]
:c => [2.0, 3.0]
:d => [3.0, 4.0]
:e => [3.0, 5.0]
(:d, :e) => [2.0, 4.5]
(:c, (:d, :e)) => [1.0, 3.75]
((:a, :b), (:c, (:d, :e))) => [0.0, 2.625]Is a OrderedDict of node=>coordinate pairs in the order returned by AbstractTrees.PostOrderDFS(tree).
This is useful for more complicated plotting where we need to work with both the node and the coordinate at the same time.
As an example
fig, ax, tp = treeplot(tree)
pts = [tp.nodepoints[][n] for n in PreOrderDFS(tree) if BasicTreePlots.isleaf(n)]
scatter!(pts, markersize=15)
fig
tp.maxtreedepth
This is the largest distance of a node to the root in the tree. Can be useful when plotting
fig, ax, tp = treeplot(tree)
treelabels!(tp; depth=tp.maxtreedepth)
## equivalent to
## treelabels!(tp; depth=:align)
fig
tp.tree
This is the same as the tree used as the argument to treeplot. But using tp.tree may allow updates to flow more directly through the Makie.ComputeGraph.
layoutstyle & branchstyle
layoutstyle options: :dendrogram, :cladogram.
branchstyle options: :square, :straight.
using NewickTree, BasicTreePlots, CairoMakie
tree = nw"((a:0.1, b:0.2):0.3, (c:0.5, (d:0.3, e:0.1):0.1):0.2):0.5;"
layoutstyles = (:dendrogram, :cladogram)
branchstyles = (:square, :straight)
fig = Figure(size=(500, 500))
for i in 1:2, j in 1:2
ax = Axis(fig[i,j];
title=join([layoutstyles[i], ", ", branchstyles[j]]),
xautolimitmargin=(0.05, 0.1),
)
tp = treeplot!(fig[i,j], tree;
layoutstyle=layoutstyles[i],
branchstyle=branchstyles[j],
)
hidedecorations!(ax)
treelabels!(tp)
treescatter!(tp)
end
fig┌ Warning: Assignment to `ax` in soft scope is ambiguous because a global variable by the same name exists: `ax` will be treated as a new local. Disambiguate by using `local ax` to suppress this warning or `global ax` to assign to the existing global variable.
└ @ treeplot.md:11
┌ Warning: Assignment to `tp` in soft scope is ambiguous because a global variable by the same name exists: `tp` will be treated as a new local. Disambiguate by using `local tp` to suppress this warning or `global tp` to assign to the existing global variable.
└ @ treeplot.md:15
In a Polar axis
using NewickTree, BasicTreePlots, CairoMakie
tree = NewickTree.nw"((a:0.1, b:0.2):0.3, (c:0.5, (d:0.3, e:0.1):0.1):0.2):0.5;"
layoutstyles = (:dendrogram, :cladogram)
branchstyles = (:square, :straight)
fig = Figure(size=(500, 500))
for i in 1:2, j in 1:2
ax = PolarAxis(fig[i,j];
title=join([layoutstyles[i], ", ", branchstyles[j]]),
rautolimitmargin=(0, 0.2),
)
tp = treeplot!(tree;
layoutstyle=layoutstyles[i],
branchstyle=branchstyles[j],
)
hidedecorations!(ax)
treelabels!(tp)
treescatter!(tp)
end
fig┌ Warning: Assignment to `ax` in soft scope is ambiguous because a global variable by the same name exists: `ax` will be treated as a new local. Disambiguate by using `local ax` to suppress this warning or `global ax` to assign to the existing global variable.
└ @ treeplot.md:11
┌ Warning: Assignment to `tp` in soft scope is ambiguous because a global variable by the same name exists: `tp` will be treated as a new local. Disambiguate by using `local tp` to suppress this warning or `global tp` to assign to the existing global variable.
└ @ treeplot.md:15
orientation
Options are :right, :top, :left', and:bottom, and indicate where the leaves point away from the root.:rightand:topwill keep the root at zero the leaves will increase in distance on thexandyaxis respectively.:leftandbottomwill translate the tree after multiplying the axis by-1so that the deepest leaf is at0` and the internal nodes increase in height away from that leaf.
fig = Figure(size = (600, 600))
treeplot(fig[1, 1], tree, orientation = :right, axis = (; title = "orientation = :right"))
treeplot(fig[1, 2], tree, orientation = :left, axis = (; title = "orientation = :left"))
treeplot(fig[2, 1], tree, orientation = :top, axis = (; title = "orientation = :top"))
treeplot(fig[2, 2], tree, orientation = :bottom, axis = (; title = "orientation = :bottom"))
fig
For radial plots the orientation can also be inverted.
fig = Figure(size = (600, 300))
ax, tp = treeplot(fig[1, 1], tree, orientation = :out, axis = (;type=PolarAxis, title = "orientation = :out"))
hidedecorations!(ax)
ax, tp = treeplot(fig[1, 2], tree, orientation = :in, axis = (;type=PolarAxis, title = "orientation = :in"))
hidedecorations!(ax)
fig┌ Warning: Orientation of out is not well tested on PolarAxis
└ @ BasicTreePlotsMakieExt ~/work/BasicTreePlots.jl/BasicTreePlots.jl/ext/BasicTreePlotsMakieExt.jl:196
┌ Warning: Orientation of in is not well tested on PolarAxis
└ @ BasicTreePlotsMakieExt ~/work/BasicTreePlots.jl/BasicTreePlots.jl/ext/BasicTreePlotsMakieExt.jl:196
branchcolor & branchwidth
Can be either a single color :black, color plus alpha transperency (:black, 0.5), or a vector of numbers for each node in pre-walk order. color for each node is associated to the line connecting it to its parent.
Likewise, Can be either a single width of type Real or a vector of numbers for each node in pre-walk order. width for each node is associated to the branch connecting it to its parent.
fig = Figure(size=(900, 300))
treeplot(fig[1,1], tree;
branchcolor = :blue,
branchwidth = 2,
axis=(; title="branchcolor = :blue, branchwidth = 2")
)
treeplot(fig[1,2], tree;
branchcolor = (:red, 0.3),
branchwidth = 10 .* rand(9),
axis=(; title="branchcolor = (:red, 0.3), branchwidth = random")
)
treeplot(fig[1,3], tree;
branchcolor = 1:9,
branchwidth = 1:9,
axis=(; title="branchcolor = 1:9, branchwidth = 1:9")
)
fig
leafoffset & usemaxdepth
Offset added to first leaf. At default leaves start counting from 1. Set to -1 to start counting from 0. Useful for starting the first leaf pointing directly right when using PolarAxis or setting the first leaf at the y origin when using Axis
If usemaxdepth=true draw lines of each leaf tip to the depth of the leaf that is maximally distant from root. This options is useful for connecting leaves to there location on the y axis (or θ axis if plotted on PolarAxis).
fig = Figure()
ax1 = Axis(fig[1,1]; title="default leafoffset")
treeplot!(tree)
ax2 = Axis(fig[1,2]; title="leafoffset = -1, usemaxdepth");
treeplot!(tree; leafoffset = -1, usemaxdepth = true)
fig
showroot
If BasicTreePlots.distance() is defined for the root, showroot=true draws the root-parent link.
NewickTree.setdistance!(tree, 0.5)
fig = Figure()
ax = Axis(fig[1,1]; title="showroot = true")
treeplot!(tree; showroot = true)
fig
Notes
- Colormap and color-range related options are mixed in from Makie (
colorscale,colormap, etc.) and behave the same as otherMakieplot types.
Reference
BasicTreePlots.treeplot — Function
treeplot(tree; kwargs...)
Args:
- tree, the root node of a tree that has
AbstractTrees.children()defined.
All nodes should be reachable by using AbstractTrees.PreOrderDFS() iterator.
Examples
Quick snippet for seeing basic features
using NewickTree, CairoMakie
tree = nw"((a:0.1, b:0.2):0.3, (c:0.5, (d:0.3, e:0.1):0.1):0.2);"
fig = Figure(size=(500, 500))
layoutstyles = (:dendrogram, :cladogram)
branchstyles = (:square, :straight)
for i in 1:2, j in 1:2
ax, tp = treeplot(fig[i,j], tree;
layoutstyle=layoutstyles[i],
branchstyle=branchstyles[j],
axis=(; title=join([layoutstyles[i]), ", ", branchstyles[j]])
)
treelabels!(tp.nodepoints)
scatter!(tp.orderedpoints)
end
figThis can then be annotated with treehilight, treelabels, and treecladelabel plots
Plot type
The plot type alias for the treeplot function is TreePlot.
Attributes
alpha = 1.0 — The alpha value of the colormap or color attribute. Multiple alphas like in plot(alpha=0.2, color=(:red, 0.5), will get multiplied.
branchcolor = @inherit color :black — Can be either a single color :black, color plus alpha transperency (:black, 0.5), or a vector of numbers for each node in pre-walk order. color for each node is associated to the line connecting it to its parent.
branchpointresolution = 25 — Number of points associated to each line segment. Can be decreased to increase plotting speed. Or, increased if lines that should be smooth are not.
branchstyle = :square — Available options are :square or :straight. :square will display line from child to parent by going back to the height of the parent, before connecting back to the parent node at a right angle. straight will display line from child to parent as a straight direct line from child to parent.
branchwidth = @inherit linewidth 1.0 — Can be either a single width of type Real or a vector of numbers for each node in pre-walk order. width for each node is associated to the branch connecting it to its parent.
clip_planes = @inherit clip_planes automatic — Clip planes offer a way to do clipping in 3D space. You can set a Vector of up to 8 Plane3f planes here, behind which plots will be clipped (i.e. become invisible). By default clip planes are inherited from the parent plot or scene. You can remove parent clip_planes by passing Plane3f[].
colormap = @inherit colormap :viridis — Sets the colormap that is sampled for numeric colors. PlotUtils.cgrad(...), Makie.Reverse(any_colormap) can be used as well, or any symbol from ColorBrewer or PlotUtils. To see all available color gradients, you can call Makie.available_gradients().
colorrange = automatic — The values representing the start and end points of colormap.
colorscale = identity — The color transform function. Can be any function, but only works well together with Colorbar for identity, log, log2, log10, sqrt, logit, Makie.pseudolog10, Makie.Symlog10, Makie.AsinhScale, Makie.SinhScale, Makie.LogScale, Makie.LuptonAsinhScale, and Makie.PowerScale.
depth_shift = 0.0 — Adjusts the depth value of a plot after all other transformations, i.e. in clip space, where -1 <= depth <= 1. This only applies to GLMakie and WGLMakie and can be used to adjust render order (like a tunable overdraw).
fxaa = true — Adjusts whether the plot is rendered with fxaa (fast approximate anti-aliasing, GLMakie only). Note that some plots implement a better native anti-aliasing solution (scatter, text, lines). For them fxaa = true generally lowers quality. Plots that show smoothly interpolated data (e.g. image, surface) may also degrade in quality as fxaa = true can cause blurring.
highclip = automatic — The color for any value above the colorrange.
inspectable = @inherit inspectable — Sets whether this plot should be seen by DataInspector. The default depends on the theme of the parent scene.
inspector_clear = automatic — Sets a callback function (inspector, plot) -> ... for cleaning up custom indicators in DataInspector.
inspector_hover = automatic — Sets a callback function (inspector, plot, index) -> ... which replaces the default show_data methods.
inspector_label = automatic — Sets a callback function (plot, index, position) -> string which replaces the default label generated by DataInspector.
layoutstyle = :dendrogram — Available options are :dendrogram, or :cladogram. :dendrogram displays tree taking into account the distance between parent and children nodes as calculated from BasicTreePlots.distance(node). If the distance is not defined, it defaults to 1 and is equivalent to the :cladogram layout :cladogram displays the tree where each distance from a child node to their parent is set to 1.
leafoffset = 0 — Offset added to first leaf. At default leaves start counting from 1. Set to -1 to start counting from 0. Useful for starting the first leaf pointing directly right when using PolarAxis or setting the first leaf at the y origin when using Axis
lowclip = automatic — The color for any value below the colorrange.
maxdepthoffset = 0.0 — No docs available.
model = automatic — Sets a model matrix for the plot. This overrides adjustments made with translate!, rotate! and scale!.
nan_color = :transparent — The color for NaN values.
openangle = 0 — Angle in radians that limits span of tree around the circle when plotted on PolarAxis. if openangle = deg2rad(5) then leaf tips will spread across angles leafoffset to 2π - openangle.
orientation = :right — Options are :right, :top, :left', and:bottom, and indicate where the leaves point away from the root.:rightand:topwill keep the root at zero the leaves will increase in distance on thexandyaxis respectively.:leftandbottomwill translate the tree after multiplying the axis by-1so that the deepest leaf is at0` and the internal nodes increase in height away from that leaf.
To keep the root at zero but the leaves pointing down, use orientation=:top in combination with Axis(figloc; yreversed=true).
overdraw = false — Controls if the plot will draw over other plots. This specifically means ignoring depth checks in GL backends
showroot = false — If BasicTreePlots.distance() is not nan for root, show line linking root to parent.
space = :data — Sets the transformation space for box encompassing the plot. See Makie.spaces() for possible inputs.
ssao = false — Adjusts whether the plot is rendered with ssao (screen space ambient occlusion). Note that this only makes sense in 3D plots and is only applicable with fxaa = true.
transformation = :automatic — Controls the inheritance or directly sets the transformations of a plot. Transformations include the transform function and model matrix as generated by translate!(...), scale!(...) and rotate!(...). They can be set directly by passing a Transformation() object or inherited from the parent plot or scene. Inheritance options include:
:automatic: Inherit transformations if the parent and childspaceis compatible:inherit: Inherit transformations:inherit_model: Inherit only model transformations:inherit_transform_func: Inherit only the transform function:nothing: Inherit neither, fully disconnecting the child's transformations from the parent
Another option is to pass arguments to the transform!() function which then get applied to the plot. For example transformation = (:xz, 1.0) which rotates the xy plane to the xz plane and translates by 1.0. For this inheritance defaults to :automatic but can also be set through e.g. (:nothing, (:xz, 1.0)).
transparency = false — Adjusts how the plot deals with transparency. In GLMakie transparency = true results in using Order Independent Transparency.
usemaxdepth = false — If true draw lines of each leaf tip to the depth of the leaf that is maximally distant from root. Useful for connecting leaves to there location on the y axis (or θ axis if plotted on PolarAxis).
visible = true — Controls whether the plot gets rendered or not.