Almost every data set we will work on has a spatial context. Until relatively recently, this was largely ignored and at most given as a 2-dimensional projection of sampling locations on a black and white map in our manuscripts. Here we explore methodologies to provide more intuitive approaches for plotting the spatial location of our sampling sites.
16.1 Procedure
First, we need to load in the proper libraries. Each of the libraries are needed for:
gstudio - Contains the A. attenuatus data set and there are built-in routines that allow you to extract coordinates.
leaflet - Loads in the JavaScript leaflet library and allows the interactive visualization of spatial data on top of maps.
16.2 Plotting Population Locations
We will start by loading in the default data set for gstudio and extracting the coordinates of our populations.
# load in the libraries and grab the datalibrary(gstudio)data(arapat)# pull out the coordinates - Population is the default stratumcoords<-strata_coordinates(arapat)coords$Stratum<-as.character(coords$Stratum)coords[1:5,]
It is also possible to plot the data in a spatial context where the size of our markers represents some biological property, like allele frequencies or genetic diversity. Here, we’ll plot the locations with circle sizes proportional to the allele frequency of the LTRS locus or the effective allele size.
# Break out allele frequencies for LTRS by population f<-frequencies(arapat,loci="LTRS",stratum="Population")# lets only use the allele 01 f<-f[f$Allele=="01",]# here are what the data look likef[1:10,]
We can also extract the effective allele size (Ae) and merge it:
# grab effect for LTRS locusae<-genetic_diversity(arapat, stratum ="Population", mode="Ae")ae<-ae[ae$Locus=="LTRS",]# merge in the datacoords<-merge(coords, ae[,c(1,3)])coords[1:5,]
In the information that pops out of the marker, you can embed a HTML table that contains richer detailed data, such as several diversity measures (\(H_O\), \(H_E\), and \(A_E\)).
A Sankey Diagram is a kind of flow diagram used to indicate allocation of one set of objects into two other sets. This is a complicated set of interactions to display. In the following example, I’ll use the populations from the A. attenuatus data set and then map each individual onto its STRUCTURE cluster and mtDNA clade. This plot needs a data.frame for nodes and one for edges created in a particular way.
library(networkD3)pops<-as.character(unique(arapat$Population))species<-as.character(unique(arapat$Species))cluster<-as.character(unique(arapat$Cluster))sankey.nodes<-data.frame( name =c(pops, species, cluster), stringsAsFactors =FALSE)# takes vector of values and finds out the number of times# each combination is found in the pair.get_sets<-function(source, target){t<-table(source, target)m<-which(t>0, arr.ind =TRUE)suppressWarnings(df<-data.frame( ind=m))df$source<-rownames(t)[df$ind.source]df$target<-colnames(t)[df$ind.target]df$value<-sapply(seq(1,dim(df)[1]), function(x){return(t[df$ind.source[x],df$ind.target[x]])})return(df[,3:5])}# For finding the number of the name instead of the namenameIndex<-function(vals,names){ret<-sapply(vals, function(x){which(x==names)-1}, USE.NAMES =FALSE)return(ret)}sankey.edges<-rbind(get_sets(arapat$Species,arapat$Population),get_sets(arapat$Population, arapat$Cluster))sankey.edges$targetNum<-nameIndex(sankey.edges$target, sankey.nodes$name)sankey.edges$sourceNum<-nameIndex(sankey.edges$source, sankey.nodes$name)
What it does produce is a very cool and dynamic plot. The colored boxes represent the groups–Species & STRUCTURE Cluster on the outside, population on the inside. Connecting lines indicate individuals who are in each of these partially overlapping groups. For example, if you grab the population Mat, you will see it has samples in it who are identified as either mtDNA type Peninsula or Cape as well as belonging to STRUCTURE groups CBP-C and SCBP-A. If you have difficult data such as these, this kind of plot may be very helpful.