library( gstudio )
library( dplyr )
library( ggplot2 )
library( ggraph )
data( arapat )12 Visualization
The gstudio package returns analysis results as standard data.frame objects, which plug directly into ggplot2. This chapter shows the recommended plotting patterns for allele frequency data, population maps, and population graphs.
12.1 Allele Frequency Plots
frequencies() returns a tidy data frame with columns Locus, Allele, and Frequency (plus Stratum when partitioned). Pass it directly to geom_bar().
12.1.1 Single locus
arapat |>
frequencies( loci = "LTRS" ) |>
ggplot( aes( x = Allele, y = Frequency ) ) +
geom_bar( stat = "identity", fill = "steelblue" ) +
labs( title = "Allele Frequencies at LTRS" ) +
theme_minimal()
12.1.2 Partitioned by stratum
When stratum is supplied, frequencies() zero-fills missing allele/stratum combinations so dodged and faceted plots are correctly aligned. Filter with Frequency > 0 beforehand if you prefer to show only observed alleles.
freqs <- arapat |>
frequencies( loci = "LTRS", stratum = "Species" )
freqs |>
ggplot( aes( x = Allele, y = Frequency, fill = Stratum ) ) +
geom_bar( stat = "identity", position = "dodge" ) +
labs( title = "LTRS Allele Frequencies by Species" ) +
theme_minimal()
12.1.3 Faceted across strata
Here the zero-filled alleles are dropped with filter() before plotting:
freqs |>
filter( Frequency > 0 ) |>
ggplot( aes( x = Allele, y = Frequency, fill = Allele ) ) +
geom_bar( stat = "identity" ) +
facet_wrap( ~Stratum ) +
labs( title = "LTRS Frequencies by Species" ) +
theme_minimal() +
theme( legend.position = "none" )
12.2 Spatial Sampling Maps
strata_coordinates() aggregates individual records to population centroids, returning a data frame with Stratum, Longitude, and Latitude columns ready for geom_point(). Points alone, however, say little about where the samples are. A light outline of the land behind them gives that context.
12.2.1 A Base Map
The Natural Earth boundaries in the rnaturalearth package (with its data package rnaturalearthdata; install both once with install.packages()) are returned as sf objects, which geom_sf() draws directly. The base map below has a grey land outline, labels for the two seas, and a simple north arrow drawn with annotate(); coord_sf() sets the extent. It is an ordinary ggplot, so it can be stored and reused for every map of the region.
countries <- c( "Mexico", "United States of America" )
land <- rnaturalearth::ne_countries( country = countries,
scale = "medium",
returnclass = "sf" )
baja_map <- ggplot() +
geom_sf( data = land,
fill = "grey92",
colour = "grey55",
linewidth = 0.3 ) +
annotate( "text",
x = -111.3,
y = 27.6,
label = "Sea of Cortez",
angle = -52,
colour = "steelblue4",
fontface = "italic",
size = 3.2 ) +
annotate( "text",
x = -116.0,
y = 25.0,
label = "Pacific Ocean",
colour = "steelblue4",
fontface = "italic",
size = 3.5 ) +
annotate( "segment",
x = -109.0,
xend = -109.0,
y = 31.0,
yend = 32.0,
arrow = arrow( length = unit( 2.5, "mm" ),
type = "closed" ) ) +
annotate( "text",
x = -109.0,
y = 32.3,
label = "N",
fontface = "bold" ) +
coord_sf( xlim = c( -117.5, -108.5 ),
ylim = c( 22.5, 32.5 ),
expand = FALSE ) +
labs( x = NULL, y = NULL ) +
theme_minimal() +
theme( panel.background = element_rect( fill = "aliceblue",
colour = NA ) )Because geographic coordinates are longitude and latitude, the sample data can be added as ordinary layers; layers added later are drawn on top, so the points sit above the land. The north arrow points up the page, which is true north here because coord_sf() draws unprojected longitude and latitude. For a projected map, or a scale bar, the ggspatial package’s annotation_north_arrow() and annotation_scale() do the same job.
coords <- arapat |>
strata_coordinates()
baja_map +
geom_point( aes( x = Longitude, y = Latitude ),
data = coords,
size = 2.5,
shape = 21,
fill = "steelblue" ) +
labs( title = "Araptus attenuatus Sampling Locations" )
Individual-level data works just as well when you want within-population spread:
baja_map +
geom_point( aes( x = Longitude, y = Latitude, fill = Species ),
data = arapat,
size = 2,
shape = 21,
alpha = 0.7 )
12.3 Population Graph Visualization
For a quick look, plot() on a Population Graph returns a ggplot (nodes sized by their size attribute — within-population genetic variability for graphs from popgraph() — or by degree when there is none, and filled by a Region attribute when present; geographic coordinates used when the graph carries them, arrows for directed graphs); see the Population Graphs article for its options. For fully custom figures, use the ggraph package, which extends ggplot2 for graph objects and works natively with popgraph/igraph objects. Install it once with install.packages("ggraph").
12.3.1 Force-Directed Layout
Build a population graph from the arapat dataset and plot it with a Fruchterman-Reingold layout. Node and edge aesthetics map directly to vertex and edge attributes.
graph <- arapat |>
population_graph( stratum = "Species" )Warning in popgraph(data, groups, ...): 1 variables are collinear and being
dropped from the discriminant rotation.
igraph::V( graph )$degree <- igraph::degree( graph )
graph |>
ggraph( layout = "fr" ) +
geom_edge_link( color = "grey60", linewidth = 0.5 ) +
geom_node_point( aes( fill = degree ), size = 5, shape = 21 ) +
geom_node_label( aes( label = name ), repel = TRUE, size = 2.5 ) +
scale_fill_viridis_c() +
theme_graph() +
labs( title = "Araptus attenuatus Population Graph" )
12.3.2 On a Map with plot()
plot() draws a Population Graph on another ggplot when it is given as base =: the graph’s edges and nodes go on top, and the base map’s coordinate system and extent are kept. A graph decorated with coordinates (decorate_graph()) is placed geographically:
data( lopho )
data( baja )
lopho_dec <- lopho |>
decorate_graph( baja, stratum = "Population" )
lopho_dec |>
plot( base = baja_map )
The same base = works for a gravity field (Chapter 9), whose source-sink surface is drawn between the map and the graph. Section 9.10.2 crops that surface to land and draws it with and without the graph on top.
12.3.3 Geographic Layout with ggraph
With ggraph, use layout = "manual" with longitude and latitude pulled from vertex attributes set by decorate_graph(). A ggraph plot starts with no layers, so adding geom_sf() first puts the land underneath the edges and nodes, and coord_sf() keeps geographic proportions.
Use scale_edge_width(range = c(min, max)) to control the rendered line width in mm. Dividing weights inside aes() has no effect because ggraph’s default scale auto-normalises to the data range regardless — the scale absorbs any constant factor. For non-linear transforms (log, normalize), add a pre-computed edge attribute with igraph::E(graph)$attr <- ... before plotting.
lopho_dec |>
ggraph( layout = "manual",
x = igraph::V( lopho_dec )$Longitude,
y = igraph::V( lopho_dec )$Latitude ) +
geom_sf( data = land,
fill = "grey92",
colour = "grey55",
linewidth = 0.3 ) +
geom_edge_link( aes( width = weight ),
color = "grey40",
show.legend = FALSE ) +
scale_edge_width( range = c( 0.3, 2.5 ), guide = "none" ) +
geom_node_point( aes( size = size, color = Region ),
show.legend = FALSE ) +
geom_node_label( aes( label = name ), repel = TRUE, size = 2.5 ) +
coord_sf( xlim = c( -116, -108.5 ),
ylim = c( 22.5, 32.5 ) ) +
labs( title = "Lophocereus Population Graph",
x = NULL,
y = NULL ) +
theme_minimal()
12.3.4 Directed (Asymmetric) Graphs
asymmetric_popgraph() produces a directed graph where each undirected edge becomes two directed edges with weights reflecting the net introgression pressure. geom_edge_fan() automatically curves parallel edges so both directions are visible.
lopho_asym <- lopho_dec |>
asymmetric_popgraph()
lopho_asym |>
ggraph( layout = "manual",
x = igraph::V( lopho_asym )$Longitude,
y = igraph::V( lopho_asym )$Latitude ) +
geom_sf( data = land,
fill = "grey92",
colour = "grey55",
linewidth = 0.3 ) +
geom_edge_fan( aes( color = weight, width = weight ),
arrow = arrow( length = unit( 2.5, "mm" ),
type = "closed" ),
end_cap = circle( 3, "mm" ),
show.legend = FALSE ) +
scale_edge_width( range = c( 0.3, 2 ), guide = "none" ) +
scale_edge_color_viridis( option = "magma",
alpha = 0.4,
guide = "none" ) +
geom_node_point( size = 3, shape = 21, fill = "white", stroke = 0.8 ) +
coord_sf( xlim = c( -116, -108.5 ),
ylim = c( 22.5, 32.5 ) ) +
labs( title = "Asymmetric Gene Flow — Lophocereus",
x = NULL,
y = NULL ) +
theme_minimal()
12.4 Interactive Leaflet Maps
addAlleleFrequencies() adds allele-frequency pie charts to a leaflet map and follows the standard leaflet pipe pattern. Pre-compute frequencies with frequencies(), build the base map from strata_coordinates(), then pipe into addAlleleFrequencies().
library( leaflet )
library( leaflet.minicharts )
freqs <- arapat |>
frequencies( loci = "LTRS", stratum = "Species" )
arapat |>
strata_coordinates( stratum = "Species" ) |>
leaflet() |>
addTiles() |>
addAlleleFrequencies( freqs )Pass locus = explicitly when freqs contains more than one locus. Width and height of the pie charts (in pixels) are controlled with width = and height =.
12.5 Without Pipes
freqs <- frequencies( arapat, loci = "LTRS", stratum = "Species" )
ggplot( freqs, aes( x = Allele, y = Frequency, fill = Stratum ) ) +
geom_col( position = "dodge" )
coords <- strata_coordinates( arapat )
baja_map +
geom_point( aes( x = Longitude, y = Latitude ), data = coords )
plot( lopho_dec, base = baja_map )
lopho_dec <- decorate_graph( lopho, baja, stratum = "Population" )
ggraph( lopho_dec,
layout = "manual",
x = igraph::V( lopho_dec )$Longitude,
y = igraph::V( lopho_dec )$Latitude ) +
geom_sf( data = land, fill = "grey92", colour = "grey55" ) +
geom_edge_link() +
geom_node_point() +
coord_sf()