An R package for the spatial analysis of population genetic marker data, including population graph construction, genetic diversity and structure estimation, directional gene flow analysis, and interactive visualization.
For complete documentation see: http://dyerlab.github.io/gstudio/
remotes::install_github("dyerlab/gstudio")All analysis functions return standard data.frame objects that plug directly into ggplot2. The package does not wrap ggplot2 internally — you get the data back and plot it yourself. Population graphs are visualized with the ggraph package.
library(gstudio)
library(ggplot2)
data(arapat)
# Allele frequencies → hand off to ggplot
frequencies(arapat, loci = "LTRS", stratum = "Species") |>
ggplot(aes(x = Allele, y = Frequency, fill = Stratum)) +
geom_bar(stat = "identity", position = "dodge") +
theme_minimal()Locus data
locus()/as.locus()— create diploid locus objects from various input formatsfrequencies()— tidy allele frequency table (zero-fills missing allele/stratum combinations whenstratum=is supplied)genetic_diversity()— per-locus diversity statistics (He, Ho, Ae, Fis, …)genetic_distance()— pairwise distance matrices (AMOVA, Euclidean, Nei, cGD, …)genetic_structure()— Gst, Gst′, Dest, Fst
Population graphs
population_graph(data, stratum)— build a conditional-independence graph from multilocus datadecorate_graph(graph, data, stratum)— attach data frame columns as vertex attributesasymmetric_weights(graph)— tSNE-inspired directional edge weights; returns a tidy data frame withdij,dji, andDeltaper edgeasymmetric_popgraph(graph)— convert an undirected popgraph to a directed one using the asymmetric weights
Interactive maps
strata_coordinates(data)— population centroid data frame (Stratum,Longitude,Latitude)addAlleleFrequencies(map, freqs)— add allele-frequency pie charts to a leaflet map following the pipe convention:
library(leaflet)
library(leaflet.minicharts)
freqs <- frequencies(arapat, loci = "LTRS", stratum = "Species")
strata_coordinates(arapat, stratum = "Species") |>
leaflet() |>
addTiles() |>
addAlleleFrequencies(freqs)Simulation
simulate_pop()/make_population()/mate()/migrate()— forward-time individual-based simulation
I/O
read_population()/write_population()— read/write genotype datato_sf()— convert popgraph nodes or edges to ansfspatial objectto_genepop()/to_structure()/to_json()— export to common formats
to_SpatialPoints()/to_SpatialLines()— useto_sf()instead (sp package superseded by sf)
Questions, bug reports, and contributions welcome. Contact Rodney J. Dyer or visit http://dyerlab.org