Title: | an R package for visualization of tree and annotation data |
---|---|
Description: | 'ggtree' extends the 'ggplot2' plotting system which implemented the grammar of graphics. 'ggtree' is designed for visualization and annotation of phylogenetic trees and other tree-like structures with their annotation data. |
Authors: | Guangchuang Yu [aut, cre, cph] , Tommy Tsan-Yuk Lam [aut, ths], Shuangbin Xu [aut] , Lin Li [ctb], Bradley Jones [ctb], Justin Silverman [ctb], Watal M. Iwasaki [ctb], Yonghe Xia [ctb], Ruizhu Huang [ctb] |
Maintainer: | Guangchuang Yu <[email protected]> |
License: | Artistic-2.0 |
Version: | 3.15.0 |
Built: | 2024-11-29 06:25:19 UTC |
Source: | https://github.com/bioc/ggtree |
update data with tree info (y coordination and panel)
p %+>% .data
p %+>% .data
p |
tree view |
.data |
data.frame |
add tree information to an input data. This function will setup y coordination and panel info for data used in facet_plot and geom_faceet
updated data.frame
Guangchuang Yu
G Yu, TTY Lam, H Zhu, Y Guan (2018). Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution, 35(2):3041-3043. https://doi.org/10.1093/molbev/msy194
update tree
pg %<% x
pg %<% x
pg |
ggtree object |
x |
tree object |
This operator apply the visualization directives in ggtree object (lhs) to visualize another tree object (rhs), that is similar to Format Painter.
updated ggplot object
Guangchuang Yu
library("ggplot2") nwk <- system.file("extdata", "sample.nwk", package="treeio") tree <- read.tree(nwk) p <- ggtree(tree) + geom_tippoint(color="#b5e521", alpha=1/4, size=10) p %<% rtree(30)
library("ggplot2") nwk <- system.file("extdata", "sample.nwk", package="treeio") tree <- read.tree(nwk) p <- ggtree(tree) + geom_tippoint(color="#b5e521", alpha=1/4, size=10) p %<% rtree(30)
add colorbar legend
add_colorbar(p, color, x = NULL, ymin = NULL, ymax = NULL, font.size = 4)
add_colorbar(p, color, x = NULL, ymin = NULL, ymax = NULL, font.size = 4)
p |
tree view |
color |
output of scale_color function |
x |
x position |
ymin |
ymin |
ymax |
ymax |
font.size |
font size |
ggplot2 object
Guangchuang Yu
Apply the daylight alorithm to adjust the spacing between the subtrees and tips of the specified node.
applyLayoutDaylight(df, node_id)
applyLayoutDaylight(df, node_id)
df |
tree data.frame |
node_id |
is id of the node from which daylight is measured to the other subtrees. |
list with tree data.frame with updated layout using daylight algorithm and max_change angle.
collapse binary tree to polytomy by applying 'fun' to 'feature'
as.polytomy(tree, feature, fun)
as.polytomy(tree, feature, fun)
tree |
tree object, 'phylo' object only |
feature |
selected feature |
fun |
function to select nodes to collapse |
polytomy tree
Guangchuang Yu
collapse a selected clade, which can later be expanded with the 'expand()' fuction if necessary
## S3 method for class 'ggtree' collapse(x = NULL, node, mode = "none", clade_name = NULL, ...)
## S3 method for class 'ggtree' collapse(x = NULL, node, mode = "none", clade_name = NULL, ...)
x |
tree view (i.e. the ggtree object). If tree_view is NULL, the last ggplot will be used. |
node |
internal node number |
mode |
one of 'none'(default), 'max', 'min' and 'mixed'. 'none' would simply collapse the clade as 'tip' and the rest will display a triangle, whose shape is determined by the farest/closest tip of the collapsed clade to indicate it |
clade_name |
set a name for the collapsed clade. If clade_name = NULL, do nothing |
... |
additional parameters to set the color or transparency of the triangle |
tree view
Guangchuang Yu
expand
x <- rtree(15) p <- ggtree(x) + geom_tiplab() p p1 <- collapse(p, node = 17, mode = "mixed", clade_name = "cclade", alpha = 0.8, color = "grey", fill = "light blue")
x <- rtree(15) p <- ggtree(x) + geom_tiplab() p p1 <- collapse(p, node = 17, mode = "mixed", clade_name = "cclade", alpha = 0.8, color = "grey", fill = "light blue")
convert Date to decimal format, eg "2014-05-05" to "2014.34"
Date2decimal(x)
Date2decimal(x)
x |
Date |
numeric
Guangchuang Yu
convert decimal format to Date, eg "2014.34" to "2014-05-05"
decimal2Date(x)
decimal2Date(x)
x |
numerical number, eg 2014.34 |
Date
Guangchuang Yu
expand collapsed clade
expand(tree_view = NULL, node)
expand(tree_view = NULL, node)
tree_view |
tree view (i.e. the ggtree object). If tree_view is NULL, the last ggplot object will be used. |
node |
internal node number to specify a clade. If NULL, using the whole tree |
tree view
Guangchuang Yu
collapse
x <- rtree(15) p <- ggtree(x) + geom_tiplab() p1 <- collapse(p, 17) expand(p1, 17)
x <- rtree(15) p <- ggtree(x) + geom_tiplab() p1 <- collapse(p, 17) expand(p1, 17)
extract data used in facet_plot
or geom_facet
facet_data(tree_view, panel)
facet_data(tree_view, panel)
tree_view |
ggtree object |
panel |
data plotted in specific panel. If only one dataset used in the panel, return the data frame, else return a list of data frames. |
data frame or a list of data frames
Guangchuang Yu
G Yu, TTY Lam, H Zhu, Y Guan (2018). Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution, 35(2):3041-3043. https://doi.org/10.1093/molbev/msy194
function to relable selected panels created by 'geom_facet' or 'facet-plot'
facet_labeller(p, label)
facet_labeller(p, label)
p |
facet_plot output |
label |
new labels of facet panels |
ggplot object
Guangchuang Yu
plot tree associated data in an additional panel
facet_plot(p, mapping = NULL, data, geom, panel, ...) geom_facet(mapping = NULL, data, geom, panel, ...)
facet_plot(p, mapping = NULL, data, geom, panel, ...) geom_facet(mapping = NULL, data, geom, panel, ...)
p |
tree view |
mapping |
aes mapping for 'geom' |
data |
data to plot by 'geom', first column should be matched with tip label of tree |
geom |
geom function to plot the data |
panel |
panel name for plot of input data |
... |
additional parameters for 'geom' |
'facet_plot()' automatically re-arranges the input 'data' according to the tree structure, visualizes the 'data' on specific 'panel' using the 'geom' function with aesthetic 'mapping' and other parameters, and align the graph with the tree 'p' side by side. 'geom_facet' is a 'ggplot2' layer version of 'facet_plot'
ggplot object
Guangchuang Yu
G Yu, TTY Lam, H Zhu, Y Guan (2018). Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution, 35(2):3041-3043. https://doi.org/10.1093/molbev/msy194
tr <- rtree(10) dd = data.frame(id=tr$tip.label, value=abs(rnorm(10))) p <- ggtree(tr) facet_plot(p, 'Trait', data = dd, geom=geom_point, mapping=aes(x=value))
tr <- rtree(10) dd = data.frame(id=tr$tip.label, value=abs(rnorm(10))) p <- ggtree(tr) facet_plot(p, 'Trait', data = dd, geom=geom_point, mapping=aes(x=value))
set relative widths (for column only) of facet plots
facet_widths(p, widths)
facet_widths(p, widths)
p |
ggplot or ggtree object |
widths |
relative widths of facet panels |
ggplot object by redrawing the figure (not a modified version of input object)
Guangchuang Yu
exchange the position of 2 clades
flip(tree_view = NULL, node1, node2)
flip(tree_view = NULL, node1, node2)
tree_view |
tree view (i.e. the ggtree object). If tree_view is NULL, the last ggplot will be used. |
node1 |
node number of clade 1. It should share a same parent node with node2 |
node2 |
node number of clade 2. It should share a same parent node with node1 |
ggplot object
Guangchuang Yu
set.seed(123) x <- rtree(15) p <- ggtree(x) + geom_tiplab() + geom_nodelab(aes(subset=!isTip, label=node), hjust = -.1, color = "red") flip(p, 23, 24) ## Depends on the condition of your tree
set.seed(123) x <- rtree(15) p <- ggtree(x) + geom_tiplab() + geom_nodelab(aes(subset=!isTip, label=node), hjust = -.1, color = "red") flip(p, 23, 24) ## Depends on the condition of your tree
add horizontal align lines layer to a tree
geom_aline(mapping = NULL, linetype = "dotted", linewidth = 1, ...)
geom_aline(mapping = NULL, linetype = "dotted", linewidth = 1, ...)
mapping |
aes mapping |
linetype |
set line type of the line, defaults to "dotted" |
linewidth |
set width of the line, defaults to 1 |
... |
additional parameter |
'geom_aline'align all tips to the longest one by adding padding characters to the right side of the tip.
aline layer
Yu Guangchuang
highlights the two direct descendant clades of an internal node
geom_balance( node, fill = "steelblue", color = "white", alpha = 0.5, extend = 0, extendto = NULL )
geom_balance( node, fill = "steelblue", color = "white", alpha = 0.5, extend = 0, extendto = NULL )
node |
selected node (balance) to highlight its two direct descendant |
fill |
color to fill in the highlight rectangle, default to "steelblue" |
color |
color to outline highlight rectangle and divide balance, defaults to "white" |
alpha |
alpha (transparency) for the highlight rectangle, defaults to 0.5 |
extend |
extend xmax of the highlight rectangle by the value of extend |
extendto |
extend xmax of the highlight rectangle to the value of extendto |
Particularly useful when studying neighboring clades. Note that balances that correspond to multichotomies will not be displayed.
ggplot2
Justin Silverman and modified by Guangchuang Yu
J. Silverman, et al. A phylogenetic transform enhances analysis of compositional microbiota data. (in preparation)
For more detailed demonstration, please refer to chapter 5.2.2 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
library(ggtree) set.seed(123) tr<- rtree(15) x <- ggtree(tr) x + geom_balance(17)
library(ggtree) set.seed(123) tr<- rtree(15) x <- ggtree(tr) x + geom_balance(17)
annotate a clade with bar and text label or (image)
geom_cladelab( node = NULL, label = NULL, data = NULL, mapping = NULL, geom = "text", parse = FALSE, ... )
geom_cladelab( node = NULL, label = NULL, data = NULL, mapping = NULL, geom = "text", parse = FALSE, ... )
node |
selected node to annotate, when data and mapping is NULL, it is required. |
label |
character, character to be showed, when data and mapping is NULL, it is required. |
data |
data.frame, the data to be displayed in the annotation, defaults to NULL. |
mapping |
Set of aesthetic mappings, defaults to NULL. The detail see the following explanation. |
geom |
character, one of 'text', 'label', 'shadowtext', 'image' and 'phylopic', defaults to 'text', and the parameter see the Aesthetics For Specified Geom. |
parse |
logical, whether parse label to emoji font, defaults to FALSE. |
... |
additional parameters, see also following section. additional parameters can refer the following parameters.
The parameters also can be set in mapping, when data is provided. Note: the barsize, barcolour, fontsize, textcolour, imagesize and imagecolor should not be set in mapping (aesthetics). When the color and size are not be set in mapping, user can modify them to adjust the attributes of specified geom. |
geom_cladelab()
understands the following aesthetics for geom="text"(required
aesthetics are in bold):
node
selected node to hight light, it is required.
label
labels to be shown, it is required.
colour
the colour of text, defaults to "black".
size
the size of text, defaults to 3.88.
angle
the angle of text, defaults to 0.
hjust
A numeric vector specifying horizontal justification, defaults to 0.
vjust
A numeric vector specifying vertical justification, defaults to 0.5.
alpha
the transparency of text, defaults to NA.
family
the family of text, defaults to 'sans'.
fontface
the font face of text, defaults to 1 (plain), others are
2 (bold), 3 (italic), 4 (bold.italic).
lineheight
The height of a line as a multiple of the size of text, defaults to 1.2 .
when the colour, size are not be set in mapping, and user want to modify the colour of text, they should use textcolour, fontsize to avoid the confusion with bar layer annotation.
geom_cladelab()
understands the following aesthethics for geom="label" (required
aesthetics are in bold):
node
selected node to hight light, it is required.
label
labels to be shown, it is required.
colour
the colour of text, defaults to "black".
fill
the background colour of the label, defaults to "white".
size
the size of text, defaults to 3.88.
angle
the angle of text, defaults to 0.
hjust
A numeric vector specifying horizontal justification, defaults to 0.
vjust
A numeric vector specifying vertical justification, defaults to 0.5.
alpha
the transparency of text, defaults to NA.
family
the family of text, defaults to 'sans'.
fontface
the font face of text, defaults to 1 (plain), others are
2 (bold), 3 (italic), 4 (bold.italic).
lineheight
The height of a line as a multiple of the size of text, defaults to 1.2 .
when the colour, size are not be set in mapping, and user want to modify the colour of text, they should use textcolour, fontsize to avoid the confusion with bar layer annotation.
geom_cladelab()
understands the following aesthethics for geom="shadowtext" (required
aesthetics are in bold):
node
selected node to hight light, it is required.
label
labels to be shown, it is required.
colour
the colour of text, defaults to "black".
bg.colour
the background colour of text, defaults to 'black'.
bg.r
the width of background text, defaults to 0.1.
size
the size of text, defaults to 3.88.
angle
the angle of text, defaults to 0.
hjust
A numeric vector specifying horizontal justification, defaults to 0.
vjust
A numeric vector specifying vertical justification, defaults to 0.5.
alpha
the transparency of text, defaults to NA.
family
the family of text, defaults to 'sans'.
fontface
the font face of text, defaults to 1 (plain), others are
2 (bold), 3 (italic), 4 (bold.italic).
lineheight
The height of a line as a multiple of the size of text, defaults to 1.2 .
when the colour, size are not be set in mapping, and user want to modify the colour of text, they should use textcolour, fontsize to avoid the confusion with bar layer annotation.
geom_cladelab()
understands the following aesthethics for geom="image" or geom="phylopic" (required
aesthetics are in bold):
node
selected node to hight light, it is required.
label
labels to be shown, it is required.
image
the image to be annotated, when geom="phylopic",
the uid of phylopic databases, it is required.
colour
the color of image, defaults to NULL.
size
the size of image, defaults to 0.05.
alpha
the alpha of image, defaults to 0.8.
when the colour, size are not be set in mapping, and user want to modify the colour of image, they should use imagecolour, imagesize to avoid the confusion with bar layer annotation.
set.seed(2015-12-21) tree <- rtree(30) data <- data.frame(id=c(34, 56), annote=c("another clade", "long clade names"), image=c("7fb9bea8-e758-4986-afb2-95a2c3bf983d", "0174801d-15a6-4668-bfe0-4c421fbe51e8"), group=c("A", "B"), offset=c(0.1, 0.1), offset.text=c(0.1, 0.2)) p <- ggtree(tree) + xlim(NA, 6) p + geom_cladelab(node=45, label="test label") + geom_cladelab(node=34, label="another clade") p2 <- p + geom_cladelab(data=data, mapping=aes( node=id, label=annote, image=image, color=group, offset=offset ), geom="shadowtext", hjust=0.5, align=TRUE, horizontal=FALSE, angle=90, show.legend = FALSE ) p2
set.seed(2015-12-21) tree <- rtree(30) data <- data.frame(id=c(34, 56), annote=c("another clade", "long clade names"), image=c("7fb9bea8-e758-4986-afb2-95a2c3bf983d", "0174801d-15a6-4668-bfe0-4c421fbe51e8"), group=c("A", "B"), offset=c(0.1, 0.1), offset.text=c(0.1, 0.2)) p <- ggtree(tree) + xlim(NA, 6) p + geom_cladelab(node=45, label="test label") + geom_cladelab(node=34, label="another clade") p2 <- p + geom_cladelab(data=data, mapping=aes( node=id, label=annote, image=image, color=group, offset=offset ), geom="shadowtext", hjust=0.5, align=TRUE, horizontal=FALSE, angle=90, show.legend = FALSE ) p2
annotate a clade with bar and text label
geom_cladelabel( node, label, offset = 0, offset.text = 0, extend = 0, align = FALSE, barsize = 0.5, fontsize = 3.88, angle = 0, geom = "text", hjust = 0, color = NULL, fill = NA, family = "sans", parse = FALSE, horizontal = TRUE, ... )
geom_cladelabel( node, label, offset = 0, offset.text = 0, extend = 0, align = FALSE, barsize = 0.5, fontsize = 3.88, angle = 0, geom = "text", hjust = 0, color = NULL, fill = NA, family = "sans", parse = FALSE, horizontal = TRUE, ... )
node |
selected node |
label |
clade label |
offset |
offset of bar and text from the clade |
offset.text |
offset of text from bar |
extend |
extend bar height |
align |
logical |
barsize |
size of bar |
fontsize |
size of text |
angle |
angle of text |
geom |
one of 'text' or 'label' |
hjust |
justify text horizontally |
color |
color for clade & label, of length 1 or 2 |
fill |
fill label background, only work with geom='label' |
family |
sans by default, can be any supported font |
parse |
logical, whether parse label |
horizontal |
logical, whether set label to horizontal, defaults to TRUE. |
... |
additional parameter |
ggplot layers
Guangchuang Yu
annotate a clade with bar and text label
geom_cladelabel2( node, label, offset = 0, offset.text = 0, offset.bar = 0, align = FALSE, barsize = 0.5, fontsize = 3.88, hjust = 0, geom = "text", color = NULL, family = "sans", parse = FALSE, horizontal = TRUE, ... )
geom_cladelabel2( node, label, offset = 0, offset.text = 0, offset.bar = 0, align = FALSE, barsize = 0.5, fontsize = 3.88, hjust = 0, geom = "text", color = NULL, family = "sans", parse = FALSE, horizontal = TRUE, ... )
node |
selected node |
label |
clade label |
offset |
offset of bar and text from the clade |
offset.text |
offset of text from bar |
offset.bar |
offset of bar from text |
align |
logical |
barsize |
size of bar |
fontsize |
size of text |
hjust |
justify text horizontally |
geom |
one of 'text' or 'label' |
color |
color for clade & label, of length 1 or 2 |
family |
sans by default, can be any supported font |
parse |
logical, whether parse label |
horizontal |
logical, whether set label to horizontal, defaults to TRUE. |
... |
additional parameter |
ggplot layers
JustGitting
layer of hilight clade
geom_hilight( data = NULL, mapping = NULL, node = NULL, type = "auto", to.bottom = FALSE, ... ) geom_highlight( data = NULL, mapping = NULL, node = NULL, type = "auto", to.bottom = FALSE, ... )
geom_hilight( data = NULL, mapping = NULL, node = NULL, type = "auto", to.bottom = FALSE, ... ) geom_highlight( data = NULL, mapping = NULL, node = NULL, type = "auto", to.bottom = FALSE, ... )
data |
data.frame, The data to be displayed in this layer, defaults to NULL. |
mapping |
Set of aesthetic mappings, defaults to NULL. |
node |
selected node to hilight, when data and mapping is NULL, it is required. |
type |
the type of layer, defaults to |
to.bottom |
logical, whether set the high light layer to the bottom in all layers of 'ggtree' object, default is FALSE. |
... |
additional parameters, see also the below and Aesthetics section.
|
geom_hilight
supports data.frame as input. And aesthetics of layer can be mapped.
you can see the Aesthetics section to set parameters.
a list object.
geom_hilight()
understands the following aesthetics for rectangular layer (required
aesthetics are in bold):
node
selected node to hight light, it is required.
colour
the colour of margin, defaults to NA.
fill
the colour of fill, defaults to 'steelblue'.
alpha
the transparency of fill, defaults to 0.5.
extend
extend xmax of the rectangle, defaults to 0.
extendto
specify a value, meaning the rectangle extend to, defaults to NULL.
linetype
the line type of margin, defaults to 1.
linewidth
the width of line of margin, defaults to 0.5.
geom_hilight()
understands the following aesthethics for encircle layer (required
aesthetics are in bold):
node
selected node to hight light, it is required.
colour
the colour of margin, defaults to 'black'.
fill
the colour of fill, defaults to 'steelblue'.
alpha
the transparency of fill, defaults to 0.5.
expand
expands the xspline clade region, defaults to 0.
spread
control the size, when only one point.
linewidth
the width of line of margin, defaults to 0.5.
linetype
the line type of margin, defaults to 1.
s_shape
the shape of the spline relative to the control points, defaults to 0.5.
s_open
whether the spline is a line or a closed shape, defaults to FALSE.
Guangchuang Yu and Shuangbin Xu
For more detailed demonstration, please refer to chapter 5.2.2 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
library(ggplot2) set.seed(102) tree <- rtree(60) p <- ggtree(tree) p1 <- p + geom_hilight(node=62) + geom_hilight(node=88, fill="red") p1 dat <- data.frame(id=c(62, 88), type=c("A", "B")) p2 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type)) p2 p3 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="left") p4 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="right") p5 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="both") # display the high light layer with gradiental color rectangular. p6 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), type = "gradient", alpha=0.68) p7 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), type = "gradient", gradient.direction="tr", alpha=0.68) # display the high light layer with round rectangular. p8 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), type = "roundrect", alpha=0.68) p2/ p3/ p4/ p5 / p6/ p7/ p8
library(ggplot2) set.seed(102) tree <- rtree(60) p <- ggtree(tree) p1 <- p + geom_hilight(node=62) + geom_hilight(node=88, fill="red") p1 dat <- data.frame(id=c(62, 88), type=c("A", "B")) p2 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type)) p2 p3 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="left") p4 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="right") p5 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="both") # display the high light layer with gradiental color rectangular. p6 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), type = "gradient", alpha=0.68) p7 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), type = "gradient", gradient.direction="tr", alpha=0.68) # display the high light layer with round rectangular. p8 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), type = "roundrect", alpha=0.68) p2/ p3/ p4/ p5 / p6/ p7/ p8
gemo_inset can add subplots to tree by accepting a list of ggplot objects that are ancestral stats or data associated with selected nodes in the tree. These ggplot objects can be any kind of charts or hybrid of of these charts.
add subplots as insets in a tree
geom_inset( insets, width = 0.1, height = 0.1, hjust = 0, vjust = 0, x = "node", reverse_x = FALSE, reverse_y = FALSE ) inset( tree_view, insets, width, height, hjust = 0, vjust = 0, x = "node", reverse_x = FALSE, reverse_y = FALSE )
geom_inset( insets, width = 0.1, height = 0.1, hjust = 0, vjust = 0, x = "node", reverse_x = FALSE, reverse_y = FALSE ) inset( tree_view, insets, width, height, hjust = 0, vjust = 0, x = "node", reverse_x = FALSE, reverse_y = FALSE )
insets |
a list of ggplot objects, named by node number |
width |
width of the inset, relative to the range of x-axis, defaults to .1 |
height |
height of the inset, relative to the range of y-axis, defaults to .1 |
hjust |
adjust the horizontal position of the charts, charts will go left if hjust > 0 |
vjust |
adjust the vertical position of the charts, charts will go down if vjust > 0 |
x |
the position where users want to place the charts, one of 'node' (default) and 'branch' |
reverse_x |
whether to reverse x axis of the charts by 'ggplot2::scale_x_reverse', defaults to 'FALSE' |
reverse_y |
whether to reverse y axis of the charts by 'ggplot2::scale_y_reverse', defaults to 'FALSE' |
tree_view |
tree view |
Users can also use
inset layer
tree view with insets
Guangchuang Yu
For demonstration of this function, please refer to chapter 8.3 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
geom_label2 support aes(subset) via setup_data
geom_label2( mapping = NULL, data = NULL, ..., stat = "identity", position = "identity", family = "sans", parse = FALSE, nudge_x = 0, nudge_y = 0, label.padding = unit(0.25, "lines"), label.r = unit(0.15, "lines"), label.size = 0.25, na.rm = TRUE, show.legend = NA, inherit.aes = TRUE )
geom_label2( mapping = NULL, data = NULL, ..., stat = "identity", position = "identity", family = "sans", parse = FALSE, nudge_x = 0, nudge_y = 0, label.padding = unit(0.25, "lines"), label.r = unit(0.15, "lines"), label.size = 0.25, na.rm = TRUE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings, defaults to NULL. |
data |
A layer specific dataset - only needed if you want to override the plot defaults. |
... |
other arguments passed on to 'layer'. |
stat |
Name of the stat to modify data. |
position |
The position adjustment to use for overlapping points on this layer. |
family |
"sans" by default, can be any supported font. |
parse |
if 'TRUE', the labels will be parsed as expressions, defaults to 'FALSE'. |
nudge_x |
adjust the horizontal position of the labels. |
nudge_y |
adjust the vertical position of the labels. |
label.padding |
Amount of padding around label, defaults to 'unit(0.25, "lines")'. |
label.r |
Use to set the radius of rounded corners of the label, defaults to 'unit(0.15, "lines")'. |
label.size |
Size of label border, in mm, defaults to 0.25. |
na.rm |
If "FALSE" (default), missing values are removed with a warning. If "TRUE", missing values are silently removed, logical. |
show.legend |
Whether to show legend, logical, defaults to "NA". |
inherit.aes |
Whether to inherit aesthetic mappings, logical, defaults to "TRUE". |
'geom_label2' is a modified version of geom_label, with subset aesthetic supported
label layer
Guangchuang Yu
For more detailed demonstration of this function, please refer to chapter A.4.5 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
library(ggtree) set.seed(123) tr<- rtree(15) x <- ggtree(tr) x + geom_label2(aes(label = node, subset = isTip == FALSE))
library(ggtree) set.seed(123) tr<- rtree(15) x <- ggtree(tr) x + geom_label2(aes(label = node, subset = isTip == FALSE))
geom layer to draw aligned motif
geom_motif(mapping, data, on, label, align = "centre", ...)
geom_motif(mapping, data, on, label, align = "centre", ...)
mapping |
aes mapping |
data |
data |
on |
gene to center (i.e. set middle position of the |
label |
specify a column to be used to label genes |
align |
where to place gene label, default is 'centre' and can be set to 'left' and 'right' |
... |
additional parameters |
geom layer
Guangchuang Yu
add node label layer for a tree
geom_nodelab( mapping = NULL, nudge_x = 0, nudge_y = 0, geom = "text", hjust = 0.5, node = "internal", ... )
geom_nodelab( mapping = NULL, nudge_x = 0, nudge_y = 0, geom = "text", hjust = 0.5, node = "internal", ... )
mapping |
aesthetic mappings, defaults to NULL |
nudge_x |
horizontal adjustment to nudge labels, defaults to 0 |
nudge_y |
vertical adjustment to nudge labels, defaults to 0 |
geom |
one of 'text', "shadowtext", 'label', 'image' and 'phylopic' |
hjust |
horizontal alignment, defaults to 0.5 |
node |
a character indicating which node labels will be displayed, it should be one of 'internal', 'external' and 'all'. If it is set to 'internal' will display internal node labels, 'external' will display the tip labels, and 'all' will display internal node and tip labels. |
... |
additional parameters, see also
the additional parameters of |
geom layer
Guangchuang Yu
For demonstration of this function, please refer to chapter A.4.5 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
add node label for circular layout
geom_nodelab2( mapping = NULL, nudge_x = 0, nudge_y = 0, geom = "text", hjust = 0.5, ... )
geom_nodelab2( mapping = NULL, nudge_x = 0, nudge_y = 0, geom = "text", hjust = 0.5, ... )
mapping |
aesthetic mappings, defaults to NULL |
nudge_x |
horizontal adjustment to nudge labels, defaults to 0 |
nudge_y |
vertical adjustment to nudge labels, defaults to 0 |
geom |
one of 'text', "shadowtext", 'label', 'image' and 'phylopic' |
hjust |
horizontal alignment, defaults to 0.5 |
... |
additional parameters, see also
the additional parameters of |
node label layer
Guangchuang Yu
add node point layer to a tree
geom_nodepoint( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
geom_nodepoint( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mapping created by |
data |
The data to be displayed in this layer. If 'NULL' (the default),
the data is inherited from the plot data as specified in the call to |
position |
Position adjustment. |
na.rm |
logical. If 'FALSE' (default), missing values are removed with a warning. If 'TRUE', missing values are silently removed. |
show.legend |
logical. Should this layer be included in the legends? 'NA', the default, includes if any aesthetics are mapped. 'FALSE' never includes, and 'TRUE' always includes. |
inherit.aes |
logical (defaults to 'TRUE'). If 'FALSE', overrides the default aesthetics, rather then combining with them. |
... |
addtional parameters that passed on to this layer. These are often aesthetics, used to set an aesthetic to a fixed value, like |
node point layer
Guangchuang Yu library(ggtree) tr<- rtree(15) x <- ggtree(tr) x + geom_nodepoint()
For more detailed demonstration, please refer to chapter 4.3.2 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
geom_point2 is a modified version of geom_point that supports aes(subset)
geom_point2( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
geom_point2( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mapping created by |
data |
The data to be displayed in this layer. If 'NULL' (the default),
the data is inherited from the plot data as specified in the call to |
stat |
Name of the statistical transformation to be used on the data for this layer. |
position |
Position adjustment. |
na.rm |
logical. If 'FALSE' (default), missing values are removed with a warning. If 'TRUE', missing values are silently removed. |
show.legend |
logical. Should this layer be included in the legends? 'NA', the default, includes if any aesthetics are mapped. 'FALSE' never includes, and 'TRUE' always includes. |
inherit.aes |
logical (defaults to 'TRUE'). If 'FALSE', overrides the default aesthetics, rather then combining with them. |
... |
addtional parameters that passed on to this layer. These are often aesthetics, used to set an aesthetic to a fixed value, like |
geom_point2
creates scatterplots, just similar to ggplot2::geom_point
. It extends the ggplot2::geom_point
to support filtering via the subset
aesthetic mapping (see Aesthetics section).
point layer
geom_point2()
understands the following aesthetics
subset
logical expression indicating elements or rows to keep: missing values are taken as false; should be in aes().
colour
the colour of point, defaults to "black".
fill
the colour of fill, defaults to "black".
alpha
the transparency of fill, defaults to 1.
size
the size of point, defaults to 1.5.
shape
specify a shape, defaults to 19.
stroke
control point border thickness of point, defaults to 0.5.
Guangchuang Yu
G Yu, DK Smith, H Zhu, Y Guan, TTY Lam (2017). ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution, 8(1):28-36. https://doi.org/10.1111/2041-210X.12628
G Yu*, TTY Lam, H Zhu, Y Guan*. Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution, 2018, 35(2):3041-3043. https://doi.org/10.1093/molbev/msy194
G Yu. Using ggtree to visualize data on tree-like structures. Current Protocols in Bioinformatics, 2020, 69:e96. https://doi.org/10.1002/cpbi.96
For more information about tree visualization, please refer to the online book https://yulab-smu.top/treedata-book/
geom_point; geom_rootpoint add point of root; geom_nodepoint add points of internal nodes; geom_tippoint add points of external nodes (also known as tips or leaves).
library(ggtree) ## add point by aes(subset) tr <- rtree(10) # group tip and node ggtree(tr) + geom_point2(aes(shape=isTip, color=isTip), size=3) # specify a node to display ggtree(tr) + geom_point2(aes(subset=(node==15)), shape=21, size=5, fill='green') # specify a tip to display ggtree(tr) + geom_point2(aes(subset=(label %in% c("t1", "t3"))), shape=21, size=5, fill='green') ## color point with continuous variables library(ggtree) library(treeio) library(ggplot2) beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree") beast_tree <- read.beast(beast_file) p <- ggtree(beast_tree) + geom_tiplab(hjust = -.1)+ geom_nodepoint(aes(fill = rate), shape = 21, size = 4) + scale_fill_continuous(low = 'blue', high = 'red') + theme_tree2() + theme(legend.position = 'right') p
library(ggtree) ## add point by aes(subset) tr <- rtree(10) # group tip and node ggtree(tr) + geom_point2(aes(shape=isTip, color=isTip), size=3) # specify a node to display ggtree(tr) + geom_point2(aes(subset=(node==15)), shape=21, size=5, fill='green') # specify a tip to display ggtree(tr) + geom_point2(aes(subset=(label %in% c("t1", "t3"))), shape=21, size=5, fill='green') ## color point with continuous variables library(ggtree) library(treeio) library(ggplot2) beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree") beast_tree <- read.beast(beast_file) p <- ggtree(beast_tree) + geom_tiplab(hjust = -.1)+ geom_nodepoint(aes(fill = rate), shape = 21, size = 4) + scale_fill_continuous(low = 'blue', high = 'red') + theme_tree2() + theme(legend.position = 'right') p
horizontal bar of range (HPD, range etc) on nodes to present uncertainty of evolutionary inference
geom_range(range, center = "auto", ...)
geom_range(range, center = "auto", ...)
range |
range(interval) to be displayed, e.g. "height_0.95_HPD" |
center |
center of the range, mean, median or auto (default, the center of the range) |
... |
additional parameter, e.g. color, linewidth, alpha |
ggplot layer
Guangchuang Yu
For demonstration of this function, please refer to chapter 5.2.4 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
display root edge layer for a tree
geom_rootedge(rootedge = NULL, ...)
geom_rootedge(rootedge = NULL, ...)
rootedge |
length of rootedge; use phylo$root.edge if rootedge = NULL (default). |
... |
additional parameters Additional parameters can be referred to the following parameters:
|
geom_rootedge
is used to create a rootedge as ggtree doesn’t plot the root edge by default.
ggtree rootedge layer
Guangchuang Yu
G Yu, DK Smith, H Zhu, Y Guan, TTY Lam (2017). ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution, 8(1):28-36. https://doi.org/10.1111/2041-210X.12628
library(ggtree) set.seed(123) ## with root edge = 1 tree1 <- read.tree(text='((A:1,B:2):3,C:2):1;') ggtree(tree1) + geom_tiplab() + geom_rootedge() ## without root edge tree2 <- read.tree(text='((A:1,B:2):3,C:2);') ggtree(tree2) + geom_tiplab() + geom_rootedge() ## setting root edge tree2$root.edge <- 2 ggtree(tree2) + geom_tiplab() + geom_rootedge() ## specify length of root edge for just plotting ## this will ignore tree$root.edge ggtree(tree2) + geom_tiplab() + geom_rootedge(rootedge = 3) ## For more detailed demonstration of this function, please refer to chapter A.4.5 of ## *Data Integration, Manipulation and Visualization of Phylogenetic Trees* ## <http://yulab-smu.top/treedata-book/index.html> by Guangchuang Yu.
library(ggtree) set.seed(123) ## with root edge = 1 tree1 <- read.tree(text='((A:1,B:2):3,C:2):1;') ggtree(tree1) + geom_tiplab() + geom_rootedge() ## without root edge tree2 <- read.tree(text='((A:1,B:2):3,C:2);') ggtree(tree2) + geom_tiplab() + geom_rootedge() ## setting root edge tree2$root.edge <- 2 ggtree(tree2) + geom_tiplab() + geom_rootedge() ## specify length of root edge for just plotting ## this will ignore tree$root.edge ggtree(tree2) + geom_tiplab() + geom_rootedge(rootedge = 3) ## For more detailed demonstration of this function, please refer to chapter A.4.5 of ## *Data Integration, Manipulation and Visualization of Phylogenetic Trees* ## <http://yulab-smu.top/treedata-book/index.html> by Guangchuang Yu.
geom_rootpoint is used to add root point layer to a tree
geom_rootpoint( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
geom_rootpoint( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mapping created by |
data |
The data to be displayed in this layer. If 'NULL' (the default),
the data is inherited from the plot data as specified in the call to |
position |
Position adjustment. |
na.rm |
logical. If 'FALSE' (default), missing values are removed with a warning. If 'TRUE', missing values are silently removed. |
show.legend |
logical. Should this layer be included in the legends? 'NA', the default, includes if any aesthetics are mapped. 'FALSE' never includes, and 'TRUE' always includes. |
inherit.aes |
logical (defaults to 'TRUE'). If 'FALSE', overrides the default aesthetics, rather then combining with them. |
... |
addtional parameters that passed on to this layer. These are often aesthetics, used to set an aesthetic to a fixed value, like |
geom_rootpoint inherit from geom_point2, and it is used to display and customize the points on the root
root point layer
Guangchuang Yu
Guangchuang Yu. Using ggtree to visualize data on tree-like structures. Current Protocols in Bioinformatics. 2020, 69:e96. doi:10.1002/cpbi.96
Guangchuang Yu, Tommy Tsan-Yuk Lam, Huachen Zhu, Yi Guan. Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution. 2018, 35(12):3041-3043. doi:10.1093/molbev/msy194
Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution. 2017, 8(1):28-36. doi:10.1111/2041-210X.12628
For more information, please refer to the online book:Data Integration, Manipulation and Visualization of Phylogenetic Trees.http://yulab-smu.top/treedata-book/
geom_point; geom_rootpoint add point of root; geom_nodepoint add points of internal nodes; geom_tippoint add points of external nodes (also known as tips or leaves).
library(ggtree) tr <- rtree(10) ## add root point ggtree(tr) + geom_rootpoint() ggtree(tr) + geom_rootpoint(size=2,color="red",shape=2)
library(ggtree) tr <- rtree(10) ## add root point ggtree(tr) + geom_rootpoint() ggtree(tr) + geom_rootpoint(size=2,color="red",shape=2)
geom_segment2 support aes(subset) via setup_data
geom_segment2( mapping = NULL, data = NULL, stat = "identity", position = "identity", lineend = "butt", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, nudge_x = 0, arrow = NULL, arrow.fill = NULL, ... )
geom_segment2( mapping = NULL, data = NULL, stat = "identity", position = "identity", lineend = "butt", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, nudge_x = 0, arrow = NULL, arrow.fill = NULL, ... )
mapping |
Set of aesthetic mappings, defaults to NULL |
data |
A layer specific dataset - only needed if you want to override the plot defaults. |
stat |
Name of stat to modify data. |
position |
The position adjustment to use for overlapping points on this layer. |
lineend |
Line end style, one of butt (default), round and square. |
na.rm |
If "FALSE" (default), missing values are removed with a warning. If "TRUE", missing values are silently removed, logical. |
show.legend |
Whether to show legend, logical. |
inherit.aes |
Whether to inherit aesthetic mappings, logical, defaults to "TRUE". |
nudge_x |
adjust the horizontal position of the segments. |
arrow |
specification for arrow heads, as created by arrow(). |
arrow.fill |
fill color to usse for the arrow head (if closed). |
... |
additional parameter |
'geom_segment2' is a modified version of geom_segment, with subset aesthetic supported
add segment layer
Guangchuang Yu
annotate associated taxa (from taxa1 to taxa2, can be Monophyletic, Polyphyletic or Paraphyletc Taxa) with bar and (optional) text label
geom_strip( taxa1, taxa2, label, offset = 0, offset.text = 0, align = TRUE, barsize = 0.5, extend = 0, fontsize = 3.88, angle = 0, geom = "text", hjust = 0, color = "black", fill = NA, family = "sans", parse = FALSE, ... )
geom_strip( taxa1, taxa2, label, offset = 0, offset.text = 0, align = TRUE, barsize = 0.5, extend = 0, fontsize = 3.88, angle = 0, geom = "text", hjust = 0, color = "black", fill = NA, family = "sans", parse = FALSE, ... )
taxa1 |
taxa1 |
taxa2 |
taxa2 |
label |
add label alongside the bar (optional) |
offset |
offset of bar and text from the clade |
offset.text |
offset of text from bar |
align |
logical, whether to align bars to the most distant bar ,defaults to "TRUE" Note that if "FALSE", the bars might cross the tree |
barsize |
set size of the bar |
extend |
extend bar length vertically |
fontsize |
set size of the text |
angle |
set the angle of text |
geom |
one of 'text' or 'label' |
hjust |
adjust the horizonal position of the bar |
color |
set color for bar and label |
fill |
set color to fill label background, only work with geom='label' |
family |
"sans" by default, can be any supported font |
parse |
logical, whether to parse labels, if "TRUE", the labels will be parsed into expressions, defaults to "FALSE" |
... |
additional parameter |
ggplot layers
Guangchuang Yu
For more detailed demonstration of this function, please refer to chapter 5.2.1 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
library(ggtree) tr<- rtree(15) x <- ggtree(tr) x + geom_strip(13, 1, color = "red") + geom_strip(3, 7, color = "blue")
library(ggtree) tr<- rtree(15) x <- ggtree(tr) x + geom_strip(13, 1, color = "red") + geom_strip(3, 7, color = "blue")
annotate associated taxa (from taxa1 to taxa2, can be Monophyletic, Polyphyletic or Paraphyletc Taxa) with bar and (optional) text label or image
geom_striplab( taxa1 = NULL, taxa2 = NULL, label = NULL, data = NULL, mapping = NULL, geom = "text", parse = FALSE, ... )
geom_striplab( taxa1 = NULL, taxa2 = NULL, label = NULL, data = NULL, mapping = NULL, geom = "text", parse = FALSE, ... )
taxa1 |
can be label or node number |
taxa2 |
can be label or node number |
label |
character, character to be showed, when data and mapping is NULL, it is required. |
data |
data.frame, the data to be displayed in the annotation, default is NULL. |
mapping |
Set of aesthetic mappings, default is NULL. The detail see the following explanation. |
geom |
character, one of 'text', 'label', 'shadowtext', 'image' and 'phylopic', default is 'text', and the parameter see the Aesthetics For Specified Geom. |
parse |
logical, whether parse label to emoji font, default is FALSE. |
... |
additional parameters, see also following section. additional parameters can refer the following parameters. ##'
The parameters also can be set in mapping, when data is provided. Note: the barsize, barcolour, fontsize, textcolour, imagesize and imagecolor should not be set in mapping (aesthetics). When the color and size are not be set in mapping, user can modify them to adjust the attributes of specified geom. |
geom_striplab()
understands the following aesthetics for geom="text"(required
aesthetics are in bold):
taxa1
selected tip label or tip node, it is required.
taxa2
selected another tip label or tip node, it is required.
label
labels to be shown, it is required.
colour
the colour of text, default is "black".
size
the size of text, default is 3.88.
angle
the angle of text, default is 0.
hjust
A numeric vector specifying horizontal justification, default is 0.
vjust
A numeric vector specifying vertical justification, default is 0.5.
alpha
the transparency of text, default is NA.
family
the family of text, default is 'sans'.
fontface
the font face of text, default is 1 (plain), others are
2 (bold), 3 (italic), 4 (bold.italic).
lineheight
The height of a line as a multiple of the size of text, default is 1.2 .
when the colour, size are not be set in mapping, and user want to modify the colour of text, they should use textcolour, fontsize to avoid the confusion with bar layer annotation.
geom_striplab()
understands the following aesthethics for geom="label" (required
aesthetics are in bold):
taxa1
selected node to hight light, it is required.
taxa2
selected another tip label or tip node, it is required.
label
labels to be shown, it is required.
colour
the colour of text, default is "black".
fill
the background colour of the label, default is "white".
size
the size of text, default is 3.88.
angle
the angle of text, default is 0.
hjust
A numeric vector specifying horizontal justification, default is 0.
vjust
A numeric vector specifying vertical justification, default is 0.5.
alpha
the transparency of text, default is NA.
family
the family of text, default is 'sans'.
fontface
the font face of text, default is 1 (plain), others are
2 (bold), 3 (italic), 4 (bold.italic).
lineheight
The height of a line as a multiple of the size of text, default is 1.2 .
when the colour, size are not be set in mapping, and user want to modify the colour of text, they should use textcolour, fontsize to avoid the confusion with bar layer annotation.
geom_striplab()
understands the following aesthethics for geom="shadowtext" (required
aesthetics are in bold):
taxa1
selected node to hight light, it is required.
taxa2
selected another tip label or tip node, it is required.
label
labels to be shown, it is required.
colour
the colour of text, default is "black".
bg.colour
the background colour of text, default is 'black'.
bg.r
the width of background text, default is 0.1.
size
the size of text, default is 3.88.
angle
the angle of text, default is 0.
hjust
A numeric vector specifying horizontal justification, default is 0.
vjust
A numeric vector specifying vertical justification, default is 0.5.
alpha
the transparency of text, default is NA.
family
the family of text, default is 'sans'.
fontface
the font face of text, default is 1 (plain), others are
2 (bold), 3 (italic), 4 (bold.italic).
lineheight
The height of a line as a multiple of the size of text, default is 1.2 .
when the colour, size are not be set in mapping, and user want to modify the colour of text, they should use textcolour, fontsize to avoid the confusion with bar layer annotation.
geom_striplab()
understands the following aesthethics for geom="image" or geom="phylopic" (required
aesthetics are in bold):
taxa1
selected node to hight light, it is required.
taxa2
selected another tip label or tip node, it is required.
label
labels to be shown, it is required.
image
the image to be annotated, when geom="phylopic",
the uid of phylopic databases, it is required.
colour
the color of image, default is NULL.
size
the size of image, default is 0.05.
alpha
the alpha of image, default is 0.8.
when the colour, size are not be set in mapping, and user want to modify the colour of image, they should use imagecolour, imagesize to avoid the confusion with bar layer annotation.
set.seed(123) tr <- rtree(10) dt <- data.frame(ta1=c("t5", "t1"), ta2=c("t6", "t3"), group=c("A", "B")) p <- ggtree(tr) + geom_tiplab() p2 <- p + geom_striplab( data = dt, mapping = aes(taxa1 = ta1, taxa2 = ta2, label = group, color=group), align = TRUE, show.legend = FALSE ) p2
set.seed(123) tr <- rtree(10) dt <- data.frame(ta1=c("t5", "t1"), ta2=c("t6", "t3"), group=c("A", "B")) p <- ggtree(tr) + geom_tiplab() p2 <- p + geom_striplab( data = dt, mapping = aes(taxa1 = ta1, taxa2 = ta2, label = group, color=group), align = TRUE, show.legend = FALSE ) p2
geom_taxalink
supports data.frame as input,
the colour
, linewidth
, linetype
and alpha
can be mapped. When the data
was provided,
the mapping
should be also provided, which taxa1
and taxa2
should be mapped created
by aes
, aes_
or aes_string
. In addition, the hratio
, control the height of curve line,
when tree layout is cirular
, default is 1. ncp
, the number of control points used to draw the
curve, more control points creates a smoother curve, default is 1. They also can be mapped to
a column of data.
geom_taxalink( data = NULL, mapping = NULL, taxa1 = NULL, taxa2 = NULL, offset = NULL, outward = "auto", ... )
geom_taxalink( data = NULL, mapping = NULL, taxa1 = NULL, taxa2 = NULL, offset = NULL, outward = "auto", ... )
data |
data.frame, The data to be displayed in this layer, default is NULL. |
mapping |
Set of aesthetic mappings, default is NULL. |
taxa1 |
can be label or node number. |
taxa2 |
can be label or node number. |
offset |
numeric, control the shift of curve line (the ratio of axis value, range is "(0-1)"), default is NULL. |
outward |
logical, control the orientation of curve when the layout of tree is circular, fan or other layout in polar coordinate, default is "auto", meaning It will automatically. |
... |
additional parameter. |
a list object.
geom_taxalink()
understands the following aesthethics (required aesthetics are in bold):
taxa1
label or node number of tree.
taxa2
label or node number of tree.
group
group category of link.
colour
control the color of line, default is black.
linetype
control the type of line, default is 1 (solid).
linewidth
control the width of line, default is 0.5.
curvature
control the curvature of line, default is 0.5,
it will be created automatically in polar coordinate .
hratio
control the height of curve line, default is 1.
ncp
control the smooth of curve line, default is 1.
geom_text2 support aes(subset) via setup_data
geom_text2( mapping = NULL, data = NULL, ..., stat = "identity", position = "identity", family = "sans", parse = FALSE, na.rm = TRUE, show.legend = NA, inherit.aes = TRUE, nudge_x = 0, nudge_y = 0, check_overlap = FALSE )
geom_text2( mapping = NULL, data = NULL, ..., stat = "identity", position = "identity", family = "sans", parse = FALSE, na.rm = TRUE, show.legend = NA, inherit.aes = TRUE, nudge_x = 0, nudge_y = 0, check_overlap = FALSE )
mapping |
the aesthetic mapping |
data |
A layer specific dataset - only needed if you want to override he plot defaults. |
... |
other arguments passed on to 'layer' |
stat |
Name of stat to modify data |
position |
The position adjustment to use for overlapping points on this layer |
family |
sans by default, can be any supported font |
parse |
if TRUE, the labels will be passd into expressions |
na.rm |
logical |
show.legend |
logical |
inherit.aes |
logical |
nudge_x |
horizontal adjustment |
nudge_y |
vertical adjustment |
check_overlap |
if TRUE, text that overlaps previous text in the same layer will not be plotted |
text layer
Guangchuang Yu
add tip label layer for a tree
geom_tiplab( mapping = NULL, hjust = 0, align = FALSE, linetype = "dotted", linesize = 0.5, geom = "text", offset = 0, as_ylab = FALSE, ... )
geom_tiplab( mapping = NULL, hjust = 0, align = FALSE, linetype = "dotted", linesize = 0.5, geom = "text", offset = 0, as_ylab = FALSE, ... )
mapping |
aes mapping |
hjust |
horizontal adjustment, defaults to 0 |
align |
if TRUE, align all tip labels to the longest tip by adding padding characters to the left side of tip labels, defaults to "FALSE" with a line connecting each tip and its corresponding label, defaults to "FALSE" |
linetype |
set linetype of the line if align = TRUE, defaults to "dotted" |
linesize |
set line width if align = TRUE, defaults to 0.5 |
geom |
one of 'text', 'label', 'shadowtext', 'image' and 'phylopic' |
offset |
tiplab offset, horizontal adjustment to nudge tip labels, defaults to 0 |
as_ylab |
display tip labels as y-axis label, only works for rectangular and dendrogram layouts, defaults to "FALSE" |
... |
additional parameter additional parameters can refer the following parameters. The following parameters for geom="text".
The following parameters for geom="label".
The following parameters for geom="shadowtext", some parameters are like to geom="text".
The following parameters for geom="image" or geom="phylopic".
The following parameters for the line when align = TRUE.
|
'geom_tiplab' not only supports using text or label geom to display tip labels, but also supports image geom to label tip with image files or phylopics.
For adding tip labels to a tree with circular layout, 'geom_tiplab' will automatically adjust the angle of the tip labels to the tree by internally calling 'geom_tiplab2'.
tip label layer
Guangchuang Yu
For more detailed demonstration, please refer to chapter 4.3.3 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
require(ape) tr <- rtree(10) ggtree(tr) + geom_tiplab()
require(ape) tr <- rtree(10) ggtree(tr) + geom_tiplab()
add tip label for circular layout
geom_tiplab2(mapping = NULL, hjust = 0, ...)
geom_tiplab2(mapping = NULL, hjust = 0, ...)
mapping |
aes mapping |
hjust |
horizontal adjustment, defaults to 0 |
... |
additional parameter, see geom_tiplab |
'geom_tiplab2' will automatically adjust the angle of the tip labels to the tree with circular layout
tip label layer
Guangchuang Yu
https://groups.google.com/forum/#!topic/bioc-ggtree/o35PV3iHO-0
library(ggtree) set.seed(123) tr <- rtree(10) ggtree(tr, layout = "circular") + geom_tiplab2()
library(ggtree) set.seed(123) tr <- rtree(10) ggtree(tr, layout = "circular") + geom_tiplab2()
add tip point layer for a tree
geom_tippoint( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
geom_tippoint( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mapping created by |
data |
The data to be displayed in this layer. If 'NULL' (the default),
the data is inherited from the plot data as specified in the call to |
position |
Position adjustment. |
na.rm |
logical. If 'FALSE' (default), missing values are removed with a warning. If 'TRUE', missing values are silently removed. |
show.legend |
logical. Should this layer be included in the legends? 'NA', the default, includes if any aesthetics are mapped. 'FALSE' never includes, and 'TRUE' always includes. |
inherit.aes |
logical (defaults to 'TRUE'). If 'FALSE', overrides the default aesthetics, rather then combining with them. |
... |
addtional parameters that passed on to this layer. These are often aesthetics, used to set an aesthetic to a fixed value, like |
tip point layer
Guangchuang Yu
For more detailed demonstration, please refer to chapter 4.3.2 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
library(ggtree) tr<- rtree(15) x <- ggtree(tr) x + geom_tippoint()
library(ggtree) tr<- rtree(15) x <- ggtree(tr) x + geom_tippoint()
add tree layer
geom_tree( mapping = NULL, data = NULL, layout = "rectangular", multiPhylo = FALSE, continuous = "none", position = "identity", ... )
geom_tree( mapping = NULL, data = NULL, layout = "rectangular", multiPhylo = FALSE, continuous = "none", position = "identity", ... )
mapping |
aesthetic mapping |
data |
data of the tree |
layout |
one of 'rectangular', 'dendrogram', 'slanted', 'ellipse', 'roundrect', 'fan', 'circular', 'inward_circular', 'radial', 'equal_angle', 'daylight' or 'ape' |
multiPhylo |
logical, whether input data contains multiple phylo class, defaults to "FALSE". |
continuous |
character, continuous transition for selected aesthethic ('size' or 'color'('colour')). It should be one of 'color' (or 'colour'), 'size', 'all' and 'none', default is 'none' |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function, default is "identity". |
... |
additional parameter some dot arguments:
|
tree layer
geom_tree()
understands the following aesthethics:
color
character, control the color of line, default is black (continuous
is "none").
linetype
control the type of line, default is 1 (solid).
linewidth
numeric, control the width of line, default is 0.5 (continuous
is "none").
Yu Guangchuang
For demonstration of this function, please refer to chapter 4.2.1 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
tree <- rtree(10) ggplot(tree) + geom_tree()
tree <- rtree(10) ggplot(tree) + geom_tree()
add tree layer
geom_tree2(layout = "rectangular", ...)
geom_tree2(layout = "rectangular", ...)
layout |
one of 'rectangular', 'slanted', 'circular', 'radial' or 'unrooted' |
... |
additional parameter |
tree layer
Yu Guangchuang
add tree scale to a tree
geom_treescale( x = NULL, y = NULL, width = NULL, offset = NULL, offset.label = NULL, label = NULL, color = "black", linesize = 0.5, fontsize = 3.88, family = "sans" )
geom_treescale( x = NULL, y = NULL, width = NULL, offset = NULL, offset.label = NULL, label = NULL, color = "black", linesize = 0.5, fontsize = 3.88, family = "sans" )
x |
set x position of the scale |
y |
set y position of the scale |
width |
set the length of the tree scale |
offset |
set offset of text to line, defaults to NULL |
offset.label |
set offset of the scale title to line. |
label |
set the title of tree scale, defaults to NULL. |
color |
set color of the scale |
linesize |
set size of line |
fontsize |
set size of text |
family |
'sans' by default, can be any supported font |
'geom_treescale' automatically adds a scale bar for evolutionary distance
ggplot layers
Guangchuang Yu
For demonstration of this function, please refer to chapter 4.3.1 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
zoom selected clade of a tree
geom_zoom_clade(node, xexpand = NULL)
geom_zoom_clade(node, xexpand = NULL)
node |
internal node number to zoom in its corresponding clade |
xexpand |
numeric, extend x, meaning the ratio of range of the xlim of the original tree, defaults to NULL. |
'geom_zoom_clade' zooms in on a selected clade of a tree, while showing its on the full view of tree as a seperated panel for reference
updated tree view
Guangchuang Yu
get position of clade (xmin, xmax, ymin, ymax)
get_clade_position(treeview, node)
get_clade_position(treeview, node)
treeview |
tree view |
node |
selected node |
data.frame
Guangchuang Yu
return a data.frame that contains position information
for labeling column names of heatmap produced by gheatmap
function
get_heatmap_column_position(treeview, by = "bottom")
get_heatmap_column_position(treeview, by = "bottom")
treeview |
output of |
by |
one of 'bottom' or 'top' |
data.frame
Guangchuang Yu
get taxa name of a selected node (or tree if node=NULL) sorted by their position in plotting
get_taxa_name(tree_view = NULL, node = NULL)
get_taxa_name(tree_view = NULL, node = NULL)
tree_view |
tree view (i.e. the ggtree object). If tree_view is NULL, the last ggplot object will be used. |
node |
internal node number to specify a clade. If NULL, using the whole tree |
This function extract an ordered vector of the tips from selected clade or the whole tree based on the ggtree() plot.
ordered taxa name vector
Guangchuang Yu
tree <- rtree(30) p <- ggtree(tree) get_taxa_name(p)
tree <- rtree(30) p <- ggtree(tree) get_taxa_name(p)
path from start node to end node
get.path(phylo, from, to)
get.path(phylo, from, to)
phylo |
phylo object |
from |
start node |
to |
end node |
node vectot
Guangchuang Yu
Get the angle between the two nodes specified.
getNodeAngle.df(df, origin_node_id, node_id)
getNodeAngle.df(df, origin_node_id, node_id)
df |
tree data.frame |
origin_node_id |
origin node id number |
node_id |
end node id number |
angle in range [-1, 1]
, i.e. degrees/180, radians/pi
Get the nodes of tree from root in breadth-first order.
getNodesBreadthFirst.df(df)
getNodesBreadthFirst.df(df)
df |
tree data.frame |
list of node id's in breadth-first order.
Get all children of node from tree, including start_node.
getSubtree(tree, node)
getSubtree(tree, node)
tree |
ape phylo tree object |
node |
is the tree node id from which the tree is derived. |
list of all child node id's from starting node.
Get all children of node from df tree using breath-first.
getSubtree.df(df, node)
getSubtree.df(df, node)
df |
tree data.frame |
node |
id of starting node. |
list of all child node id's from starting node.
Get all subtrees of specified node. This includes all ancestors and relatives of node and return named list of subtrees.
getSubtreeUnrooted(tree, node)
getSubtreeUnrooted(tree, node)
tree |
ape phylo tree object |
node |
is the tree node id from which the subtrees are derived. |
named list of subtrees with the root id of subtree and list of node id's making up subtree.
Get all subtrees of node, as well as remaining branches of parent (ie, rest of tree structure as subtree) return named list of subtrees with list name as starting node id.
getSubtreeUnrooted.df(df, node)
getSubtreeUnrooted.df(df, node)
df |
tree data.frame |
node |
is the tree node id from which the subtrees are derived. |
named list of subtrees with the root id of subtree and list of node id's making up subtree.
Find the right (clockwise rotation, angle from +ve x-axis to furthest subtree nodes) and
left (anti-clockwise angle from +ve x-axis to subtree) Returning arc angle in [0, 2]
(0 to 360) domain.
getTreeArcAngles(df, origin_id, subtree)
getTreeArcAngles(df, origin_id, subtree)
df |
tree data.frame |
origin_id |
node id from which to calculate left and right hand angles of subtree. |
subtree |
named list of root id of subtree (node) and list of node ids for given subtree (subtree). |
named list with right and left angles in range [0,2]
i.e 1 = 180 degrees, 1.5 = 270 degrees.
drawing phylogenetic trees from list of phylo objects
ggdensitree( data = NULL, mapping = NULL, layout = "slanted", tip.order = "mode", align.tips = TRUE, jitter = 0, ... )
ggdensitree( data = NULL, mapping = NULL, layout = "slanted", tip.order = "mode", align.tips = TRUE, jitter = 0, ... )
data |
a list of phylo objects or any object with an as.phylo and fortify method |
mapping |
aesthetic mapping |
layout |
one of 'slanted', 'rectangluar', 'fan', 'circular' or 'radial' (default: 'slanted') |
tip.order |
the order of the tips by a character vector of taxa names; or an integer, N, to order the tips by the order of the tips in the Nth tree; 'mode' to order the tips by the most common order; 'mds' to order the tips based on MDS of the path length between the tips; or 'mds_dist' to order the tips based on MDS of the distance between the tips (default: 'mode') |
align.tips |
TRUE (default) to align trees by their tips and FALSE to align trees by their root |
jitter |
deviation to jitter tips |
... |
additional parameters passed to fortify, ggtree and geom_tree |
The trees plotted by 'ggdensitree()' will be stacked on top of each other and the structures of the trees will be rotated to ensure the consistency of the tip order.
tree layer
Yu Guangchuang, Bradley R. Jones
For more detailed demonstration of this function, please refer to chapter 4.4.2 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
require(ape) require(dplyr) require(tidyr) # Plot multiple trees with aligned tips trees <- list(read.tree(text="((a:1,b:1):1.5,c:2.5);"), read.tree(text="((a:1,c:1):1,b:2);")); ggdensitree(trees) + geom_tiplab() # Plot multiple trees with aligned tips with tip labels and separate tree colors trees.fort <- list(trees[[1]] %>% fortify %>% mutate(tree="a"), trees[[2]] %>% fortify %>% mutate(tree="b")); ggdensitree(trees.fort, aes(colour=tree)) + geom_tiplab(colour='black') # Generate example data set.seed(1) random.trees <- rmtree(5, 10) time.trees <- lapply(seq_along(random.trees), function(i) { tree <- random.trees[[i]] tree$tip.label <- paste0("t", 1:10) dates <- estimate.dates(tree, 1:10, mu=1, nsteps=1) tree$edge.length <- dates[tree$edge[, 2]] - dates[tree$edge[, 1]] fortify(tree) %>% mutate(tree=factor(i, levels=as.character(1:10))) }) # Plot multiple trees with aligned tips from multiple time points ggdensitree(time.trees, aes(colour=tree), tip.order=paste0("t", 1:10)) + geom_tiplab(colour='black') # Read example data example.trees <- read.tree(system.file("examples", "ggdensitree_example.tree", package="ggtree")) # Compute OTU grp <- list(A = c("a.t1", "a.t2", "a.t3", "a.t4"), B = c("b.t1", "b.t2", "b.t3", "b.t4"), C = c("c.t1", "c.t2", "c.t3", "c.t4")) otu.trees <- lapply(example.trees, groupOTU, grp) # Plot multiple trees colored by OTU ggdensitree(otu.trees, aes(colour=group), alpha=1/6, tip.order='mds') + scale_colour_manual(values=c("black", "red", "green", "blue"))
require(ape) require(dplyr) require(tidyr) # Plot multiple trees with aligned tips trees <- list(read.tree(text="((a:1,b:1):1.5,c:2.5);"), read.tree(text="((a:1,c:1):1,b:2);")); ggdensitree(trees) + geom_tiplab() # Plot multiple trees with aligned tips with tip labels and separate tree colors trees.fort <- list(trees[[1]] %>% fortify %>% mutate(tree="a"), trees[[2]] %>% fortify %>% mutate(tree="b")); ggdensitree(trees.fort, aes(colour=tree)) + geom_tiplab(colour='black') # Generate example data set.seed(1) random.trees <- rmtree(5, 10) time.trees <- lapply(seq_along(random.trees), function(i) { tree <- random.trees[[i]] tree$tip.label <- paste0("t", 1:10) dates <- estimate.dates(tree, 1:10, mu=1, nsteps=1) tree$edge.length <- dates[tree$edge[, 2]] - dates[tree$edge[, 1]] fortify(tree) %>% mutate(tree=factor(i, levels=as.character(1:10))) }) # Plot multiple trees with aligned tips from multiple time points ggdensitree(time.trees, aes(colour=tree), tip.order=paste0("t", 1:10)) + geom_tiplab(colour='black') # Read example data example.trees <- read.tree(system.file("examples", "ggdensitree_example.tree", package="ggtree")) # Compute OTU grp <- list(A = c("a.t1", "a.t2", "a.t3", "a.t4"), B = c("b.t1", "b.t2", "b.t3", "b.t4"), C = c("c.t1", "c.t2", "c.t3", "c.t4")) otu.trees <- lapply(example.trees, groupOTU, grp) # Plot multiple trees colored by OTU ggdensitree(otu.trees, aes(colour=group), alpha=1/6, tip.order='mds') + scale_colour_manual(values=c("black", "red", "green", "blue"))
drawing phylogenetic tree from phylo object
ggtree( tr, mapping = NULL, layout = "rectangular", open.angle = 0, mrsd = NULL, as.Date = FALSE, yscale = "none", yscale_mapping = NULL, ladderize = TRUE, right = FALSE, branch.length = "branch.length", root.position = 0, xlim = NULL, layout.params = list(as.graph = TRUE), hang = 0.1, ... )
ggtree( tr, mapping = NULL, layout = "rectangular", open.angle = 0, mrsd = NULL, as.Date = FALSE, yscale = "none", yscale_mapping = NULL, ladderize = TRUE, right = FALSE, branch.length = "branch.length", root.position = 0, xlim = NULL, layout.params = list(as.graph = TRUE), hang = 0.1, ... )
tr |
phylo object |
mapping |
aesthetic mapping |
layout |
one of 'rectangular', 'dendrogram', 'slanted', 'ellipse', 'roundrect', 'fan', 'circular', 'inward_circular', 'radial', 'equal_angle', 'daylight' or 'ape' |
open.angle |
open angle, only for 'fan' layout |
mrsd |
most recent sampling date |
as.Date |
logical whether using Date class in time tree |
yscale |
y scale |
yscale_mapping |
yscale mapping for category variable |
ladderize |
logical (default |
right |
logical. If |
branch.length |
variable for scaling branch, if 'none' draw cladogram |
root.position |
position of the root node (default = 0) |
xlim |
x limits, only works for 'inward_circular' layout |
layout.params |
list, the parameters of layout, when layout is a function.
|
hang |
numeric The fraction of the tree plot height by which labels should hang below the rest of the plot. A negative value will cause the labels to hang down from 0. This parameter only work with the 'dendrogram' layout for 'hclust' like class, default is 0.1. |
... |
additional parameter some dot arguments:
|
tree
Yu Guangchuang
G Yu, TTY Lam, H Zhu, Y Guan (2018). Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution, 35(2):3041-3043. https://doi.org/10.1093/molbev/msy194
G Yu, DK Smith, H Zhu, Y Guan, TTY Lam (2017). ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution, 8(1):28-36. https://doi.org/10.1111/2041-210X.12628
For more information, please refer to Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
require(ape) tr <- rtree(10) ggtree(tr)
require(ape) tr <- rtree(10) ggtree(tr)
append a heatmap of a matrix to the right side of a phylogenetic tree
gheatmap( p, data, offset = 0, width = 1, low = "green", high = "red", color = "white", colnames = TRUE, colnames_position = "bottom", colnames_angle = 0, colnames_level = NULL, colnames_offset_x = 0, colnames_offset_y = 0, font.size = 4, family = "", hjust = 0.5, legend_title = "value", custom_column_labels = NULL )
gheatmap( p, data, offset = 0, width = 1, low = "green", high = "red", color = "white", colnames = TRUE, colnames_position = "bottom", colnames_angle = 0, colnames_level = NULL, colnames_offset_x = 0, colnames_offset_y = 0, font.size = 4, family = "", hjust = 0.5, legend_title = "value", custom_column_labels = NULL )
p |
tree view |
data |
matrix or data.frame |
offset |
set offset of the heatmap to tree |
width |
total width of heatmap, compare to width of tree, defaults to 1, which means they are of the same length |
low |
set color of the lowest value, defaults to "green" |
high |
set color of the highest value, defaults to "red" |
color |
set color of heatmap cell border, defaults to "white" |
colnames |
logical, whether to add matrix colnames, defaults to "TRUE" |
colnames_position |
set the position of the colnames, one of 'bottom' (default) or 'top' |
colnames_angle |
set the angle of colnames |
colnames_level |
set levels of colnames |
colnames_offset_x |
set x offset for colnames |
colnames_offset_y |
set y offset for colnames |
font.size |
set font size of matrix colnames |
family |
font of matrix colnames, can be any supported font |
hjust |
adjust horizonal position of column names (0: align left, 0.5: align center (default), 1: align righ) |
legend_title |
title of fill legend |
custom_column_labels |
instead of using the colnames from the input matrix/data.frame, input a custom vector to be set as column labels |
tree view
Guangchuang Yu
For demonstration of this function, please refer to chapter 7.3 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
gzoom method
gzoom method
zoom selected subtree
gzoom(object, focus, subtree = FALSE, widths = c(0.3, 0.7), ...) ## S4 method for signature 'ggtree' gzoom(object, focus, widths = c(0.3, 0.7), xmax_adjust = 0) ## S4 method for signature 'treedata' gzoom(object, focus, subtree = FALSE, widths = c(0.3, 0.7)) ## S4 method for signature 'phylo' gzoom(object, focus, subtree = FALSE, widths = c(0.3, 0.7))
gzoom(object, focus, subtree = FALSE, widths = c(0.3, 0.7), ...) ## S4 method for signature 'ggtree' gzoom(object, focus, widths = c(0.3, 0.7), xmax_adjust = 0) ## S4 method for signature 'treedata' gzoom(object, focus, subtree = FALSE, widths = c(0.3, 0.7)) ## S4 method for signature 'phylo' gzoom(object, focus, subtree = FALSE, widths = c(0.3, 0.7))
object |
supported tree objects |
focus |
selected tips |
subtree |
logical |
widths |
widths |
... |
additional parameter |
xmax_adjust |
adjust xmax ( |
figure
plots simultaneously a whole phylogenetic tree and a portion of it.
gzoom.phylo(phy, focus, subtree = FALSE, widths = c(0.3, 0.7))
gzoom.phylo(phy, focus, subtree = FALSE, widths = c(0.3, 0.7))
phy |
phylo object |
focus |
selected tips |
subtree |
logical |
widths |
widths |
a list of ggplot object
ygc
hexpand
vexpand
expand xlim (ylim) by ratio of x (y) axis range
hexpand(ratio, direction = 1) vexpand(ratio, direction = 1) ggexpand(ratio, direction = 1, side = "hv")
hexpand(ratio, direction = 1) vexpand(ratio, direction = 1) ggexpand(ratio, direction = 1, side = "hv")
ratio |
expand x (y) axis limits by amount of xrange (yrange) * ratio |
direction |
expand x axis limit at right hand side if direction is 1 (default), or left hand side if direction is -1 |
side |
one of 'h' for horizontal and 'v' for vertical or 'hv' for both (default). |
ggexpand object
Guangchuang Yu
x <- rtree(20) x$tip.label <- paste0('RRRRREEEEEAAAAALLLLLYYYYY_Long_Lable_', x$tip.label) p1 <- ggtree(x) + geom_tiplab() p1 + ggexpand(1.5, side = "h")
x <- rtree(20) x$tip.label <- paste0('RRRRREEEEEAAAAALLLLLYYYYY_Long_Lable_', x$tip.label) p1 <- ggtree(x) + geom_tiplab() p1 + ggexpand(1.5, side = "h")
This function adds padding characters to the left side of taxa labels, adjust their length to the longest label.
label_pad(label, justify = "right", pad = "·")
label_pad(label, justify = "right", pad = "·")
label |
taxa label |
justify |
should a character vector be right-justified (default), left-justified, centred or left alone. |
pad |
padding character (defaults to dots) |
Taxa labels with padding characters added
Guangchuang Yu and Yonghe Xia
https://groups.google.com/g/bioc-ggtree/c/INJ0Nfkq3b0/m/lXefnfV5AQAJ
library(ggtree) set.seed(2015-12-21) tree <- rtree(5) tree$tip.label[2] <- "long string for test" label_pad(tree$tip.label)
library(ggtree) set.seed(2015-12-21) tree <- rtree(5) tree$tip.label[2] <- "long string for test" label_pad(tree$tip.label)
transform circular/fan layout to rectangular layout
transform rectangular layout to circular layout
transform rectangular/circular layout to inward circular layout
transform rectangular/circular layout to fan layout
transform rectangular layout to dendrogram layout
layout_rectangular() layout_circular() layout_inward_circular(xlim = NULL) layout_fan(angle = 180) layout_dendrogram()
layout_rectangular() layout_circular() layout_inward_circular(xlim = NULL) layout_fan(angle = 180) layout_dendrogram()
xlim |
setting x limits, which will affect the center space of the tree |
angle |
open tree at specific angle |
Guangchuang Yu
tree <- rtree(20) p <- ggtree(tree, layout = "circular") + layout_rectangular() tree <- rtree(20) p <- ggtree(tree) p + layout_circular() tree <- rtree(20) p <- ggtree(tree) p + layout_inward_circular(xlim=4) + geom_tiplab(hjust=1) tree <- rtree(20) p <- ggtree(tree) p + layout_fan(angle=90) tree <- rtree(20) p <- ggtree(tree) p + p + layout_dendrogram()
tree <- rtree(20) p <- ggtree(tree, layout = "circular") + layout_rectangular() tree <- rtree(20) p <- ggtree(tree) p + layout_circular() tree <- rtree(20) p <- ggtree(tree) p + layout_inward_circular(xlim=4) + geom_tiplab(hjust=1) tree <- rtree(20) p <- ggtree(tree) p + layout_fan(angle=90) tree <- rtree(20) p <- ggtree(tree) p + p + layout_dendrogram()
#' @title
layoutDaylight(model, branch.length, MAX_COUNT = 5)
layoutDaylight(model, branch.length, MAX_COUNT = 5)
model |
tree object, e.g. phylo or treedata |
branch.length |
set to 'none' for edge length of 1. Otherwise the phylogenetic tree edge length is used. |
MAX_COUNT |
the maximum number of iterations to run (default 5) |
tree as data.frame with equal angle layout.
The following aglorithm aims to implement the vague description of the "Equal-daylight Algorithm" in "Inferring Phylogenies" pp 582-584 by Joseph Felsenstein.
Leafs are subtrees with no children Initialise tree using equal angle algorithm tree_df = equal_angle(tree) nodes = get list of nodes in tree_df breadth-first nodes = remove tip nodes.
'Equal-angle layout algorithm for unrooted trees'
layoutEqualAngle(model, branch.length = "branch.length")
layoutEqualAngle(model, branch.length = "branch.length")
model |
tree object, e.g. phylo or treedata |
branch.length |
set to 'none' for edge length of 1. Otherwise the phylogenetic tree edge length is used. |
tree as data.frame with equal angle layout.
"Inferring Phylogenies" by Joseph Felsenstein.
visualize phylogenetic tree with multiple sequence alignment
msaplot( p, fasta, offset = 0, width = 1, color = NULL, window = NULL, bg_line = TRUE, height = 0.8 )
msaplot( p, fasta, offset = 0, width = 1, color = NULL, window = NULL, bg_line = TRUE, height = 0.8 )
p |
tree view |
fasta |
fasta file that contains multiple sequence alignment information |
offset |
set the offset of MSA to tree |
width |
total width of alignment, compare to width of tree, defaults to 1, which means they are of the same length |
color |
set color of the tree |
window |
specific a slice of alignment to display |
bg_line |
whether to add background line in alignment, defaults to "TRUE" |
height |
height ratio of sequence, defaults to 0.8 |
tree view
Guangchuang Yu
For demonstration of this function, please refer to chapter 7.4 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
plot multiple ggplot objects in one page
multiplot( ..., plotlist = NULL, ncol, widths = rep_len(1, ncol), labels = NULL, label_size = 5 )
multiplot( ..., plotlist = NULL, ncol, widths = rep_len(1, ncol), labels = NULL, label_size = 5 )
... |
plots |
plotlist |
plot list |
ncol |
set the number of column to display the plots |
widths |
the width of each plot |
labels |
set labels for labeling the plots |
label_size |
set font size of the label |
plot
Guangchuang Yu
generate a list of bar charts for results of ancestral state reconstruction
nodebar(data, cols, color, alpha = 1, position = "stack")
nodebar(data, cols, color, alpha = 1, position = "stack")
data |
a data.frame of stats with an additional column of node number named "node" |
cols |
columns of the data.frame that store the stats |
color |
set color of bars |
alpha |
set transparency of the charts |
position |
position of bars, if 'stack' (default) make bars stacked atop one another, 'dodge' make them dodged side-to-side |
list of ggplot objects
Guangchuang Yu
generate a list of pie charts for results of ancestral stat reconstruction
nodepie( data, cols, color, alpha = 1, outline.color = "transparent", outline.size = 0 )
nodepie( data, cols, color, alpha = 1, outline.color = "transparent", outline.size = 0 )
data |
a data.frame of stats with an additional column of node number named "node" |
cols |
columns of the data.frame that store the stats |
color |
set color of bars |
alpha |
set transparency of the charts |
outline.color |
color of outline |
outline.size |
size of outline |
list of ggplot objects
Guangchuang Yu
transform a tree in either rectangular or circular layout into the fan layout that opens with a specific angle
open_tree(treeview, angle)
open_tree(treeview, angle)
treeview |
tree view in rectangular/circular layout |
angle |
open the tree at a specific angle |
updated tree view
Guangchuang Yu
tree <- rtree(15) p <- ggtree(tree) + geom_tiplab() open_tree(p, 180)
tree <- rtree(15) p <- ggtree(tree) + geom_tiplab() open_tree(p, 180)
format a list of range (HPD, CI, etc that has length of 2)
range_format(x, trans = NULL)
range_format(x, trans = NULL)
x |
input list |
trans |
transformation function |
character vector of [lower, upper]
Guangchuang Yu
reverse timescle x-axis by setting the most recent tip to 0
revts(treeview)
revts(treeview)
treeview |
original tree view |
'scale_x_continuous(labels=abs)' is required if users want to set the x-axis lable to absolute value
updated tree view
Guangchuang Yu
tr <- rtree(10) p <- ggtree(tr) + theme_tree2() p2 <- revts(p) p2 + scale_x_continuous(labels=abs)
tr <- rtree(10) p <- ggtree(tr) + theme_tree2() p2 <- revts(p) p2 + scale_x_continuous(labels=abs)
rotate selected clade by 180 degree
rotate(tree_view = NULL, node)
rotate(tree_view = NULL, node)
tree_view |
tree view (i.e. the ggtree object). If tree_view is NULL, the last ggplot object will be used. |
node |
internal node number to specify a clade. If NULL, using the whole tree |
ggplot2 object
Guangchuang Yu
x <- rtree(15) p <- ggtree(x) + geom_tiplab() rotate(p, 17)
x <- rtree(15) p <- ggtree(x) + geom_tiplab() rotate(p, 17)
rotate circular tree in a certain angle
rotate_tree(treeview, angle)
rotate_tree(treeview, angle)
treeview |
tree view in circular layout |
angle |
the angle of rotation |
updated tree view
Guangchuang Yu
tree <- rtree(15) p <- ggtree(tree) + geom_tiplab() p2 <- open_tree(p, 180) rotate_tree(p2, 180)
tree <- rtree(15) p <- ggtree(tree) + geom_tiplab() p2 <- open_tree(p, 180) rotate_tree(p2, 180)
Rotate the points in a tree data.frame around a pivot node by the angle specified.
rotateTreePoints.df(df, pivot_node, nodes, angle)
rotateTreePoints.df(df, pivot_node, nodes, angle)
df |
tree data.frame |
pivot_node |
is the id of the pivot node. |
nodes |
list of node numbers that are to be rotated by angle around the pivot_node |
angle |
in range |
updated tree data.frame with points rotated by angle
scale_color method
scale color by a numerical tree attribute
scale_color(object, by, ...) ## S4 method for signature 'treedata' scale_color(object, by, ...) ## S4 method for signature 'phylo' scale_color(object, by, ...)
scale_color(object, by, ...) ## S4 method for signature 'treedata' scale_color(object, by, ...) ## S4 method for signature 'phylo' scale_color(object, by, ...)
object |
|
by |
one of numerical attributes |
... |
additional parameter |
color vector
scale tree color by subtree (e.g., output of cutree, kmeans, or other clustering algorithm)
scale_color_subtree(group) scale_colour_subtree(group)
scale_color_subtree(group) scale_colour_subtree(group)
group |
taxa group information |
updated tree view
Guangchuang Yu
scale x for tree with gheatmap
scale_x_ggtree(breaks = waiver(), labels = waiver())
scale_x_ggtree(breaks = waiver(), labels = waiver())
breaks |
set breaks for tree |
labels |
lables for corresponding breaks |
Since setting x-axis for tree with gheatmap by using 'theme_tree2()' is quite tricky, 'scale_x_ggtree' can help set the x-axis more reasonably.
updated tree view
Guangchuang Yu
For more detailed demonstration of this function, please refer to chapter 7.3 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
add second x-axis for geom_range
scale_x_range()
scale_x_range()
notice that the first axis is disabled in the default theme thus users need to enable it first before using scale_x_range
ggtree object
Guangchuang Yu
For demonstration of this function ,please refer to chapter 5.2.4 of Data Integration, Manipulation and Visualization of Phylogenetic Trees http://yulab-smu.top/treedata-book/index.html by Guangchuang Yu.
zoom out/in a selected clade to emphasize or de-emphasize it
scaleClade(tree_view = NULL, node, scale = 1, vertical_only = TRUE)
scaleClade(tree_view = NULL, node, scale = 1, vertical_only = TRUE)
tree_view |
tree view (i.e. the ggtree object). If tree_view is NULL, the last ggplot object will be used. |
node |
internal node number to specify a clade. If NULL, using the whole tree |
scale |
the scale of the selected clade. The clade will be zoom in when scale > 1, and will be zoom out when scale < 1 |
vertical_only |
logical. If TRUE (default), only vertical will be scaled. If FALSE, the clade will be scaled vertical and horizontally. |
tree view
Guangchuang Yu
x <- rtree(15) p <- ggtree(x) + geom_tiplab() + geom_nodelab(aes(subset=!isTip, label=node), hjust = -.1, color = "red") scaleClade(p, 24, scale = .1)
x <- rtree(15) p <- ggtree(x) + geom_tiplab() + geom_nodelab(aes(subset=!isTip, label=node), hjust = -.1, color = "red") scaleClade(p, 24, scale = .1)
set legend for multiple geom_hilight layers
set_hilight_legend(p, color, label, alpha = 1)
set_hilight_legend(p, color, label, alpha = 1)
p |
ggtree object |
color |
color vector |
label |
label vector |
alpha |
transparency of color |
updated ggtree object
Guangchuang Yu
dendrogram theme
theme_dendrogram(bgcolor = "white", fgcolor = "black", ...)
theme_dendrogram(bgcolor = "white", fgcolor = "black", ...)
bgcolor |
set background color, defaults to "white" |
fgcolor |
set color of axis |
... |
additional parameter |
Guangchuang Yu
inset theme
theme_inset(legend.position = "none", ...)
theme_inset(legend.position = "none", ...)
legend.position |
set the position of legend |
... |
additional parameter |
theme for inset function
ggplot object
Guangchuang Yu
tree theme
theme_tree(bgcolor = "white", ...)
theme_tree(bgcolor = "white", ...)
bgcolor |
set background color, defaults to "white" |
... |
additional parameter |
'theme_tree' defines a blank background to display tree
updated ggplot object with new theme
Guangchuang Yu
require(ape) tr <- rtree(10) ggtree(tr) + theme_tree()
require(ape) tr <- rtree(10) ggtree(tr) + theme_tree()
tree2 theme
theme_tree2(bgcolor = "white", fgcolor = "black", ...)
theme_tree2(bgcolor = "white", fgcolor = "black", ...)
bgcolor |
set background color, defaults to "white" |
fgcolor |
set foreground color, defaults to "black" |
... |
additional parameter |
'theme_tree2' supports displaying phylogenetic distance by setting x-axis
updated ggplot object with new theme
Guangchuang Yu
require(ape) tr <- rtree(10) ggtree(tr) + theme_tree2()
require(ape) tr <- rtree(10) ggtree(tr) + theme_tree2()
view a selected clade of tree, clade can be selected by specifying a node number or determined by the most recent common ancestor of selected tips
viewClade(tree_view = NULL, node, xmax_adjust = 0)
viewClade(tree_view = NULL, node, xmax_adjust = 0)
tree_view |
tree view (i.e. the ggtree object). If tree_view is NULL, the last ggplot object will be used. |
node |
internal node number to specify a clade. If NULL, using the whole tree |
xmax_adjust |
adjust the max range of x axis |
clade plot
Guangchuang Yu
x <- rtree(15) p <- ggtree(x) + geom_tiplab() viewClade(p, 18, xmax_adjust = 0.)
x <- rtree(15) p <- ggtree(x) + geom_tiplab() viewClade(p, 18, xmax_adjust = 0.)
expand x axis limits for specific panel
xlim_expand(xlim, panel)
xlim_expand(xlim, panel)
xlim |
x axis limits |
panel |
name of the panel to expand |
updated tree view
Guangchuang Yu
x <- rtree(30) p <- ggtree(x) + geom_tiplab() d <- data.frame(label = x$tip.label, value = rnorm(30)) p2 <- p + geom_facet(panel = "Dot", data = d, geom = geom_point, mapping = aes(x = value)) p2 + xlim_expand(c(-10, 10), 'Dot')
x <- rtree(30) p <- ggtree(x) + geom_tiplab() d <- data.frame(label = x$tip.label, value = rnorm(30)) p2 <- p + geom_facet(panel = "Dot", data = d, geom = geom_point, mapping = aes(x = value)) p2 + xlim_expand(c(-10, 10), 'Dot')
set x axis limits specially for Tree panel
xlim_tree(xlim)
xlim_tree(xlim)
xlim |
x axis limits |
updated tree view
Guangchuang Yu
x <- rtree(30) p <- ggtree(x) + geom_tiplab() d <- data.frame(label = x$tip.label, value = rnorm(30)) p2 <- p + geom_facet(panel = "Dot", data = d, geom = geom_point, mapping = aes(x = value)) p2 + xlim_tree(6)
x <- rtree(30) p <- ggtree(x) + geom_tiplab() d <- data.frame(label = x$tip.label, value = rnorm(30)) p2 <- p + geom_facet(panel = "Dot", data = d, geom = geom_point, mapping = aes(x = value)) p2 + xlim_tree(6)
zoom in on a selected clade of a tree, while showing its on the full view of tree as a seperated panel for reference
zoomClade(tree_view = NULL, node, xexpand = NULL)
zoomClade(tree_view = NULL, node, xexpand = NULL)
tree_view |
tree view (i.e. the ggtree object). If tree_view is NULL, the last ggplot object will be used. |
node |
internal node number to specify a clade. If NULL, using the whole tree |
xexpand |
numeric, expend the xlim of the zoom area. default is NULL. |
full tree with zoom in clade
Guangchuang Yu
## Not run: x <- rtree(15) p <- ggtree(x) + geom_tiplab() + geom_nodelab(aes(subset=!isTip, label=node), hjust = -.1, color = "red") zoomClade(p, 21, xexpand = .2) ## End(Not run)
## Not run: x <- rtree(15) p <- ggtree(x) + geom_tiplab() + geom_nodelab(aes(subset=!isTip, label=node), hjust = -.1, color = "red") zoomClade(p, 21, xexpand = .2) ## End(Not run)