| Title: | Draw Beautiful Symbol Nomenclature for Glycans |
| Version: | 0.7.0 |
| Description: | A 'ggplot2'-native plotting engine for drawing reproducible beautiful Symbol Nomenclature for Glycans (SNFG) glycan cartoons from glycan structure objects or text notations, with support for batch export, structural highlighting, and deep appearance customization. It follows the SNFG specification described at https://www.ncbi.nlm.nih.gov/glycans/snfg.html. |
| License: | MIT + file LICENSE |
| Suggests: | ggraph, knitr, rmarkdown, testthat (≥ 3.0.0), tibble, vdiffr |
| Config/testthat/edition: | 3 |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.3 |
| URL: | https://glycoverse.github.io/glydraw/, https://github.com/glycoverse/glydraw |
| Imports: | glyrepr (≥ 0.10.0), rlang, dplyr, ggplot2 (≥ 4.0.0), igraph, purrr, png, glyparse, cli, checkmate, scales, fs |
| Depends: | R (≥ 4.1) |
| VignetteBuilder: | knitr |
| BugReports: | https://github.com/glycoverse/glydraw/issues |
| NeedsCompilation: | no |
| Packaged: | 2026-07-25 04:39:27 UTC; fubin |
| Author: | Bin Fu |
| Maintainer: | Bin Fu <23110220018@m.fudan.edu.cn> |
| Repository: | CRAN |
| Date/Publication: | 2026-07-25 05:00:02 UTC |
glydraw: Draw Beautiful Symbol Nomenclature for Glycans
Description
A 'ggplot2'-native plotting engine for drawing reproducible beautiful Symbol Nomenclature for Glycans (SNFG) glycan cartoons from glycan structure objects or text notations, with support for batch export, structural highlighting, and deep appearance customization. It follows the SNFG specification described at https://www.ncbi.nlm.nih.gov/glycans/snfg.html.
Author(s)
Maintainer: Bin Fu 23110220018@m.fudan.edu.cn (ORCID) [copyright holder]
Authors:
Xisong Feng 1225774778@qq.com (ORCID)
See Also
Useful links:
Report bugs at https://github.com/glycoverse/glydraw/issues
Draw a Symbol Nomenclature For Glycan (SNFG)
Description
Draw a Symbol Nomenclature For Glycan (SNFG)
Usage
draw_cartoon(
structure,
...,
show_linkage = TRUE,
orient = c("H", "V"),
fuc_orient = c("flex", "up"),
red_end = "",
edge_linewidth = 0.8,
node_linewidth = 0.8,
node_size = 1,
colors = NULL,
highlight = NULL,
style = NULL
)
Arguments
structure |
A |
... |
Ignored. |
show_linkage |
Show glycosidic linkage annotations or not. Default is TRUE. Substituent annotations are always shown. |
orient |
The orientation of glycan structure. "H" for horizontal, "V" for vertical. Default is "H" |
fuc_orient |
Fuc-like triangle orientation. |
red_end |
Reducing-end annotation. The default |
edge_linewidth |
Numeric scalar controlling the linewidth of linkage
lines. Defaults to the current value, |
node_linewidth |
Numeric scalar controlling the linewidth of node
borders. Defaults to the current value, |
node_size |
Numeric scalar used as a multiplier for the default node
size. Defaults to |
colors |
Optional named character vector of custom monosaccharide fill
colors. Names must be supported monosaccharide names, such as |
highlight |
An integer vector specifying the node indices to highlight.
This argument is applicable only when |
style |
A |
Value
a ggplot2 object
Examples
draw_cartoon("Gal(b1-3)GalNAc(a1-")
Export all glycan structures to figures
Description
Draw and save one cartoon for each glycan structure in x.
Usage
export_cartoons(
x,
dirname,
...,
file_ext = "png",
dpi = 300,
scale = 1,
show_linkage = TRUE,
orient = c("H", "V"),
fuc_orient = c("flex", "up"),
red_end = "",
edge_linewidth = 0.8,
node_linewidth = 0.8,
node_size = 1,
colors = NULL,
style = NULL
)
Arguments
x |
A |
dirname |
Directory name to save the cartoons. If it does not exist, it is created. |
... |
Ignored. |
file_ext |
File extension supported by |
dpi |
Deprecated and ignored. Use |
scale |
Numeric output-size multiplier passed to |
show_linkage |
Show glycosidic linkage annotations or not. Default is TRUE. Substituent annotations are always shown. |
orient |
The orientation of glycan structure. "H" for horizontal, "V" for vertical. Default is "H" |
fuc_orient |
Fuc-like triangle orientation. |
red_end |
Reducing-end annotation. The default |
edge_linewidth |
Numeric scalar controlling the linewidth of linkage
lines. Defaults to the current value, |
node_linewidth |
Numeric scalar controlling the linewidth of node
borders. Defaults to the current value, |
node_size |
Numeric scalar used as a multiplier for the default node
size. Defaults to |
colors |
Optional named character vector of custom monosaccharide fill
colors. Names must be supported monosaccharide names, such as |
style |
A |
Value
The function returns the list of cartoons implicitly.
File names
IUPAC-condensed nomenclatures are used as file names. If x is a named
character vector or named glyrepr::glycan_structure() vector, the vector
names are used as file names.
Why not width and height?
The familiar ggplot2::ggsave() interface uses width, height, and dpi
because ordinary ggplot2 plots are drawn into a user-chosen device size.
glydraw cartoons are different: the natural width and height are calculated
from the glycan structure so residues, linkages, labels, and borders stay
comparable across different glycans. If users supplied arbitrary width and
height, glydraw would either distort that structure-derived layout or need
to guess how to reconcile one requested size with the other.
dpi is also not the right control here because changing it alters how
point- and inch-based ggplot2 elements are rasterized relative to the fixed
cartoon canvas. glydraw therefore keeps an internal fixed design scale and
uses scale as a single multiplier for the final pixel dimensions. This
preserves the cartoon's aspect ratio and relative appearance while still
allowing larger or smaller output files.
Examples
export_cartoons(
c(
"Man(a1-3)Man(b1-4)GlcNAc(b1-",
"Gal(b1-4)GlcNAc(b1-"
),
tempdir()
)
Draw glycans at ggplot2 positions
Description
geom_glycan() draws one glycan cartoon for each data row. Each cartoon is
anchored at its mapped x and y position and retains the structure-derived
dimensions and appearance used by draw_cartoon(). The optional size
aesthetic scales the complete cartoon uniformly, including nodes, lines,
text, and spacing, without changing their relative appearance. Like points
and text, the cartoons do not expand the position scales beyond their anchor
coordinates. Use scale expansion or explicit coordinate limits when the
cartoons need more room around the panel edges. Unlike standalone cartoons
returned by draw_cartoon(), cartoons in this layer have no output border or
background.
Usage
geom_glycan(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
angle = 0,
show_linkage = TRUE,
orient = c("H", "V"),
fuc_orient = c("flex", "up"),
red_end = "",
edge_linewidth = 0.8,
node_linewidth = 0.8,
node_size = 1,
colors = NULL,
highlight = NULL,
style = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. When |
stat |
The statistical transformation to use on the data for this
layer. Defaults to |
position |
A position adjustment to use on the data for this layer.
Defaults to |
... |
Other arguments passed to |
angle |
Rotation in degrees. Like |
show_linkage |
Show glycosidic linkage annotations or not. Default is TRUE. Substituent annotations are always shown. |
orient |
The orientation of glycan structure. "H" for horizontal, "V" for vertical. Default is "H" |
fuc_orient |
Fuc-like triangle orientation. |
red_end |
Reducing-end annotation. The default |
edge_linewidth |
Numeric scalar controlling the linewidth of linkage
lines. Defaults to the current value, |
node_linewidth |
Numeric scalar controlling the linewidth of node
borders. Defaults to the current value, |
node_size |
Numeric scalar used as a multiplier for the default node
size. Defaults to |
colors |
Optional named character vector of custom monosaccharide fill
colors. Names must be supported monosaccharide names, such as |
highlight |
An integer vector specifying the node indices to highlight.
This argument is applicable only when |
style |
A |
na.rm |
If |
show.legend |
Logical. Should this layer be included in the legends? |
inherit.aes |
If |
Value
A ggplot2 layer that can be added to a ggplot2::ggplot() object.
Aesthetics
geom_glycan() understands the following aesthetics. x, y, and
structure are required; the remaining aesthetics are optional:
-
x -
y -
structure, containing glycan structure strings orglyrepr::glycan_structure()values -
size, an optional whole-cartoon scale multiplier that defaults to1. Mapped values are transformed by ggplot2's size scale; useggplot2::scale_size_identity()when the mapped values are literal multipliers. This is distinct fromnode_size, which changes residue size within the cartoon. -
hjust, an optional horizontal justification that defaults to0.5. Numeric0aligns the cartoon content's left bound withx, and1aligns its right bound. Usehjust_red_end()to anchor a vertical cartoon at its reducing end. -
vjust, an optional vertical justification that defaults to0.5. Numeric0aligns the cartoon content's bottom bound withy, including the end of a reducing-end annotation line, and1aligns its top bound. Usevjust_red_end()to anchor a horizontal cartoon at its reducing end. -
angle, an optional rotation in degrees that defaults to0. Rotation is applied after the cartoon is drawn, independently oforient.
Examples
glycans <- data.frame(
x = c(1, 3),
y = c(1, 2),
size = c(0.7, 1.1),
structure = c(
"Gal(b1-3)GalNAc(a1-",
"Man(a1-3)[Man(a1-6)]Man(b1-4)GlcNAc(b1-"
)
)
ggplot2::ggplot(
glycans,
ggplot2::aes(
x = .data$x,
y = .data$y,
structure = .data$structure,
size = .data$size
)
) +
geom_glycan() +
ggplot2::scale_size_identity() +
ggplot2::coord_cartesian(
xlim = c(0, 4),
ylim = c(0, 3),
expand = FALSE
)
# Bottom-align a row of vertical glycans with different heights.
ggplot2::ggplot(
glycans,
ggplot2::aes(x = .data$x, y = 1, structure = .data$structure)
) +
geom_glycan(orient = "V", vjust = 0)
Draw glycans as ggraph nodes
Description
geom_node_glycan() is a ggraph node layer backed by
geom_glycan(). It supplies the node layout's x and y columns as the
default position aesthetics and supports ggraph's filter aesthetic.
Usage
geom_node_glycan(
mapping = NULL,
data = NULL,
position = "identity",
show.legend = NA,
...
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The node data to display. When |
position |
A position adjustment. Defaults to |
show.legend |
Logical. Should this layer be included in legends? |
... |
Arguments passed to |
Value
A ggplot2 layer that can be added to a ggraph::ggraph() plot.
Aesthetics
geom_node_glycan() understands the same aesthetics as geom_glycan().
structure is required, while x and y default to the node layout
coordinates. It additionally supports filter, a logical aesthetic that
selects which nodes are drawn.
Examples
if (requireNamespace("ggraph", quietly = TRUE)) {
graph <- igraph::make_ring(2)
igraph::V(graph)$glycan <- c(
"GalNAc(a1-",
"Gal(b1-3)GalNAc(a1-"
)
ggraph::ggraph(graph, layout = "linear") +
ggraph::geom_edge_link() +
geom_node_glycan(ggplot2::aes(structure = .data$glycan))
}
Construct a glycan grob
Description
glycanGrob() prepares the complete drawing specification for one glycan as
a grid grob. It is the low-level drawing primitive used by draw_cartoon().
Usage
glycanGrob(
structure,
...,
show_linkage = TRUE,
orient = c("H", "V"),
fuc_orient = c("flex", "up"),
red_end = "",
edge_linewidth = 0.8,
node_linewidth = 0.8,
node_size = 1,
colors = NULL,
highlight = NULL,
style = NULL
)
Arguments
structure |
A |
... |
Ignored. |
show_linkage |
Show glycosidic linkage annotations or not. Default is TRUE. Substituent annotations are always shown. |
orient |
The orientation of glycan structure. "H" for horizontal, "V" for vertical. Default is "H" |
fuc_orient |
Fuc-like triangle orientation. |
red_end |
Reducing-end annotation. The default |
edge_linewidth |
Numeric scalar controlling the linewidth of linkage
lines. Defaults to the current value, |
node_linewidth |
Numeric scalar controlling the linewidth of node
borders. Defaults to the current value, |
node_size |
Numeric scalar used as a multiplier for the default node
size. Defaults to |
colors |
Optional named character vector of custom monosaccharide fill
colors. Names must be supported monosaccharide names, such as |
highlight |
An integer vector specifying the node indices to highlight.
This argument is applicable only when |
style |
A |
Value
A glycanGrob object inheriting from grid::gTree().
Examples
grob <- glycanGrob("Gal(b1-3)GalNAc(a1-")
grid::grid.draw(grob)
Create a glycan drawing style
Description
glydraw_style() collects the rendering options shared by glydraw's
standalone drawings, grobs, ggplot2 layers, guides, and glycan scales.
Supply the result with style = to reuse a visual specification. Explicit
rendering arguments supplied to a drawing function override the style.
Usage
glydraw_style(
show_linkage = TRUE,
orient = c("H", "V"),
fuc_orient = c("flex", "up"),
red_end = "",
edge_linewidth = 0.8,
node_linewidth = 0.8,
node_size = 1,
colors = NULL
)
Arguments
show_linkage |
Whether to show glycosidic linkage annotations. |
orient |
Glycan drawing orientation: |
fuc_orient |
Fuc-like triangle orientation: |
red_end |
Reducing-end annotation. Use |
edge_linewidth |
Linewidth of glycosidic linkages. |
node_linewidth |
Linewidth of node borders. |
node_size |
Multiplier for the default node size. |
colors |
Optional named character vector of monosaccharide fill-color overrides. |
Value
A glydraw_style object.
Examples
vertical_style <- glydraw_style(orient = "V", show_linkage = FALSE)
draw_cartoon("Gal(b1-3)GalNAc(a1-", style = vertical_style)
Use glycan cartoons as legend labels
Description
guide_glycan() is a legend guide that replaces the usual text labels with
glycan cartoons while retaining the legend keys drawn by the plot layers.
Mapped discrete values and values returned by the scale's labels argument
may be glycan structure strings supported by glyparse::auto_parse() or
glyrepr::glycan_structure() vectors.
Usage
guide_glycan(
title = ggplot2::waiver(),
theme = NULL,
position = NULL,
direction = NULL,
override.aes = list(),
nrow = NULL,
ncol = NULL,
reverse = FALSE,
order = 0,
size = 0.4,
orient = c("H", "V"),
hjust = 0,
vjust = vjust_red_end(),
show_linkage = TRUE,
red_end = "",
fuc_orient = c("flex", "up"),
edge_linewidth = 0.8,
node_linewidth = 0.8,
node_size = 1,
colors = NULL,
style = NULL
)
Arguments
title |
A character string or expression indicating a title of guide.
If |
theme |
A |
position |
A character string indicating where the legend should be placed relative to the plot panels. One of "top", "right", "bottom", "left", or "inside". |
direction |
A character string indicating the direction of the guide. One of "horizontal" or "vertical". |
override.aes |
A list specifying aesthetic parameters of legend key. See details and examples. |
nrow, ncol |
The desired number of rows and column of legends respectively. |
reverse |
logical. If |
order |
positive integer less than 99 that specifies the order of this guide among multiple guides. This controls the order in which multiple guides are displayed, not the contents of the guide itself. If 0 (default), the order is determined by a secret algorithm. |
size |
Positive scalar that uniformly scales each legend-label cartoon.
Defaults to |
orient |
Glycan drawing orientation, either |
hjust |
Horizontal cartoon justification between |
vjust |
Vertical cartoon justification between |
show_linkage |
Whether to show glycosidic linkage annotations inside
the cartoons. Defaults to |
red_end |
Reducing-end annotation passed to |
fuc_orient |
Fuc-like triangle orientation passed to |
edge_linewidth |
Linkage linewidth passed to |
node_linewidth |
Node-border linewidth passed to |
node_size |
Node-size multiplier passed to |
colors |
Optional named character vector of monosaccharide fill colors
passed to |
style |
A |
Value
A ggplot2 legend guide that draws glycan cartoons in place of text labels.
Examples
glycans <- data.frame(
structure = c(
"Gal(b1-3)GalNAc(a1-",
"Man(a1-3)[Man(a1-6)]Man(b1-4)GlcNAc(b1-"
),
abundance = c(12, 8)
)
ggplot2::ggplot(
glycans,
ggplot2::aes(
x = .data$structure,
y = .data$abundance,
fill = .data$structure
)
) +
ggplot2::geom_col() +
ggplot2::scale_fill_discrete(guide = guide_glycan())
Anchor glycan cartoons at their reducing ends
Description
These helpers align the reducing end of every glycan cartoon with its anchor
in geom_glycan(), scale_x_glycan(), scale_y_glycan(), or
guide_glycan(). Use hjust_red_end() for horizontal alignment when
orient = "V", and use vjust_red_end() for vertical alignment when
orient = "H". Because the required justification is calculated separately
from each cartoon's rendered bounds, the helpers also work for collections
of glycans with different asymmetric branches. Glycan scales and guides use
reducing-end justification by default along the axis perpendicular to the
drawing orientation; geom_glycan() remains centered by default.
Usage
hjust_red_end()
vjust_red_end()
Value
A reducing-end justification marker accepted by the glycan layer, scales, and guide.
Examples
glycan <- data.frame(
x = 0,
y = 0,
structure = "Man(a1-3)[Man(a1-6)]Man(b1-4)GlcNAc(b1-"
)
ggplot2::ggplot(
glycan,
ggplot2::aes(x = .data$x, y = .data$y, structure = .data$structure)
) +
geom_glycan(orient = "V", hjust = hjust_red_end())
ggplot2::ggplot(
glycan,
ggplot2::aes(x = .data$x, y = .data$y, structure = .data$structure)
) +
geom_glycan(orient = "H", vjust = vjust_red_end())
Print glycan cartoon
Description
Print glycan cartoon
Usage
## S3 method for class 'glydraw_cartoon'
print(x, ..., newpage = is.null(vp), vp = NULL)
Arguments
x |
A ggplot2 object returned by |
... |
Ignored. |
newpage |
Draw the plot on a new page. |
vp |
A grid viewport object or viewport name. |
Value
The original glycan cartoon, invisibly.
Save fixed-size glycan cartoon image to local device.
Description
This function saves the glycan cartoon to a file, with a suitable size.
Usage
save_cartoon(cartoon, file, ..., dpi = 300, scale = 1)
Arguments
cartoon |
A ggplot2 object returned by |
file |
File name of glycan cartoon. |
... |
Ignored. |
dpi |
Deprecated and ignored. Use |
scale |
Numeric output-size multiplier. The default |
Value
Path of the saved cartoon file.
Why not width and height?
The familiar ggplot2::ggsave() interface uses width, height, and dpi
because ordinary ggplot2 plots are drawn into a user-chosen device size.
glydraw cartoons are different: the natural width and height are calculated
from the glycan structure so residues, linkages, labels, and borders stay
comparable across different glycans. If users supplied arbitrary width and
height, glydraw would either distort that structure-derived layout or need
to guess how to reconcile one requested size with the other.
dpi is also not the right control here because changing it alters how
point- and inch-based ggplot2 elements are rasterized relative to the fixed
cartoon canvas. glydraw therefore keeps an internal fixed design scale and
uses scale as a single multiplier for the final pixel dimensions. This
preserves the cartoon's aspect ratio and relative appearance while still
allowing larger or smaller output files.
Examples
cartoon <- draw_cartoon("Gal(b1-3)GalNAc(a1-")
save_cartoon(cartoon, tempfile(fileext = ".png"))
Use glycan cartoons as axis labels
Description
scale_x_glycan() and scale_y_glycan() are discrete position scales that
replace text tick labels with compact glycan cartoons. Mapped discrete values
and values returned by the scale's labels argument may be glycan structure
strings supported by glyparse::auto_parse() or
glyrepr::glycan_structure() vectors. X-axis cartoons are vertical and
bottom-aligned by default, while y-axis cartoons are horizontal and
right-aligned by default. The cartoon orientation and alignment adapt to the
displayed axis, including when the axes are swapped by
ggplot2::coord_flip().
Usage
scale_x_glycan(
name = ggplot2::waiver(),
...,
palette = seq_len,
expand = ggplot2::waiver(),
position = "bottom",
sec.axis = ggplot2::waiver(),
continuous.limits = NULL,
size = 0.4,
angle = 0,
hjust = hjust_red_end(),
vjust = 0,
nudge_x = 0,
nudge_y = 0,
show_linkage = TRUE,
red_end = "",
fuc_orient = c("flex", "up"),
edge_linewidth = 0.8,
node_linewidth = 0.8,
node_size = 1,
colors = NULL,
style = NULL
)
scale_y_glycan(
name = ggplot2::waiver(),
...,
palette = seq_len,
expand = ggplot2::waiver(),
position = "left",
sec.axis = ggplot2::waiver(),
continuous.limits = NULL,
size = 0.4,
angle = 0,
hjust = 1,
vjust = vjust_red_end(),
nudge_x = 0,
nudge_y = 0,
show_linkage = TRUE,
red_end = "",
fuc_orient = c("flex", "up"),
edge_linewidth = 0.8,
node_linewidth = 0.8,
node_size = 1,
colors = NULL,
style = NULL
)
Arguments
name |
The name of the scale, displayed as the axis title. Use |
... |
Arguments passed to |
palette |
A palette function that returns discrete position values. |
expand |
Expansion applied to the discrete position scale. |
position |
Position of the axis. |
sec.axis |
A secondary axis specification. |
continuous.limits |
Continuous limits used to position the discrete scale. |
size |
Positive scalar that uniformly scales each axis-label cartoon.
Defaults to |
angle |
Rotation in degrees applied to each axis-label cartoon,
independently of the cartoon orientation. Defaults to |
hjust |
Horizontal justification. Vertical x-axis cartoons default to
|
vjust |
Vertical justification. Vertical x-axis cartoons default to
|
nudge_x |
Horizontal adjustment of each cartoon, in millimetres.
Positive values move cartoons to the right. When this moves cartoons
toward or away from a y-axis title, the title moves with them to preserve
the gap. Defaults to |
nudge_y |
Vertical adjustment of each cartoon, in millimetres. Positive
values move cartoons upward. When this moves cartoons toward or away from
an x-axis title, the title moves with them to preserve the gap. Defaults
to |
show_linkage |
Whether to show glycosidic linkage annotations inside
the cartoons. Defaults to |
red_end |
Reducing-end annotation passed to |
fuc_orient |
Fuc-like triangle orientation passed to |
edge_linewidth |
Linkage linewidth passed to |
node_linewidth |
Node-border linewidth passed to |
node_size |
Node-size multiplier passed to |
colors |
Optional named character vector of monosaccharide fill colors
passed to |
style |
A |
Value
A ggplot2 discrete position scale.
Examples
glycans <- data.frame(
structure = c(
"Gal(b1-3)GalNAc(a1-",
"Man(a1-3)[Man(a1-6)]Man(b1-4)GlcNAc(b1-"
),
abundance = c(12, 8)
)
ggplot2::ggplot(glycans, ggplot2::aes(x = structure, y = abundance)) +
ggplot2::geom_col() +
scale_x_glycan()