Normalize node coordinates to graph and image spaces
Source:R/gspace-normalize.R
normalizeGraphSpace-methods.RdAccessory functions to normalize node coordinates in GraphSpace, either by centering them within the graph boundaries or by mapping them to pixel coordinates of a background image.
Usage
# S4 method for class 'GraphSpace'
normalizeGraphSpace(
gs,
mar = 0.1,
image.space = .has_image(gs),
flip.x = FALSE,
flip.y = image.space,
flip.v = FALSE,
flip.h = FALSE,
swap.xy = FALSE,
verbose = TRUE,
rotate.xy = deprecated(),
use_image = deprecated()
)
# S4 method for class 'GraphSpace'
cropGraphSpace(gs, crop.coord = c(0, 1, 0, 1), verbose = TRUE)Arguments
- gs
A
GraphSpaceobject to be normalized.- mar
A single numeric value in
[0, 0.5]controlling the size of the outer margins around the graph. Without an image,marspecifies symmetric margins as a fraction of the graph space. With an image,maris interpreted as a fraction of the available image margins surrounding the graph.- image.space
Logical; if an image is available, whether to use it as a background reference map. When enabled,
xandygraph coordinates are interpreted as pixel coordinates in the image matrix. Images can be inspected and assigned withgs_image.- flip.x
Logical; whether to flip the node coordinates along the x-axis.
- flip.y
Logical; whether to flip the node coordinates along the y-axis. Useful for aligning nodes with image backgrounds, which often use an inverted coordinate system. Defaults to
image.space.- flip.v
Logical; whether to vertically flip the background image matrix (top-to-bottom) to align with the graph coordinate system.
- flip.h
Logical; whether to horizontally flip the background image matrix (left-to-right) to align with the graph coordinate system.
- swap.xy
Logical; whether to swap x and y node coordinates. Useful when the graph coordinate system is transposed relative to the image or reference map.
- verbose
A single logical value specifying to display detailed messages (when
verbose=TRUE) or not (whenverbose=FALSE).- rotate.xy
Deprecated from RGraphSpace 1.4.3; use
swap.xyinstead.- use_image
Deprecated from RGraphSpace 1.4.0; use
image.spaceinstead.- crop.coord
An optional numeric vector of length four specifying a cropping region (xmin, xmax, ymin, ymax), with values in normalized coordinates
[0,1].
Details
These functions provide different strategies for coordinate transformation:
normalizeGraphSpace: Re-scales node coordinates to a
[0, 1]unit square based on the graph's bounding box (whenimage.space = FALSE) or maps them to pixel coordinates (whenimage.space = TRUEand an image is provided; see gs_image). It handles image-to-graph alignment viaflip.\*andswap.\*arguments, used to adjust the graph origin with the image matrix layout. Users should be aware of the potential discrepancy between image matrix orientation (top-down) and graph coordinates (bottom-up). The function attempts to automatically adjust the y-axis to align the graph's bottom-up coordinates with the image's top-down layout, but further manual adjustments might be required.cropGraphSpace: Subsets the normalized graph space into a specific region defined by
crop.coord. It recalculates node positions and background image boundaries to maintain spatial consistency after cropping. This function requires a previously normalizedGraphSpaceobject.
Note
This is an accessory function typically called during
the preprocessing of GraphSpace objects before rendering.
Examples
library(RGraphSpace)
library(igraph)
# Create a star graph
gtoy1 <- make_full_graph(30)
# Create a GraphSpace
gs <- GraphSpace(gtoy1)
#> Validating the 'igraph' object...
#> Vertex attributes 'x' and 'y' missing; computing layout...
#> Vertex attribute 'name' missing; assigning names...
#> Ignoring graph-level attributes: 'name', 'loops'
#> Creating a 'GraphSpace' object...
gs <- normalizeGraphSpace(gs)
#> Normalizing node coordinates to graph space...
gs_crop <- cropGraphSpace(gs, crop.coord = c(0, 1, 0, 0.5))
plotGraphSpace(gs, add.labels = TRUE)
plotGraphSpace(gs_crop, add.labels = TRUE)