trait_map: Visualize geographic patterns of trait data on a world map

View source: R/trait_map.R

trait_mapR Documentation

Visualize geographic patterns of trait data on a world map

Description

Annotate custom geometries on an equal area world map with trait data. World map contains country boundaries.

Usage

trait_map(
  data,
  aes_mapping = aes(fill = trait_ratio),
  country_color = "grey40",
  country_fill = "white",
  country_alpha = 1,
  country_size = 0.1,
  ...
)

Arguments

data

Data.frame with trait data (e.g., "trait_ratio" from fungarium::enrichment) and sf geometries (e.g., bounding boxes; see example).

aes_mapping

Aesthetics mapping for annotating geometries. Default: aes(fill=trait_ratio). In this case, geometries will be filled with color depending on the trait_ratio value. See ggplot2 for customizing color scales.

country_color

Character string specifying the outline color of countries in the world map. Default: "grey40"

country_fill

Character string specifying the fill color of countries in the world map. Default: "white"

country_alpha

Numeric specifying the country fill alpha in the world map. Default: 1

country_size

Numeric specifying country outline thickness. Default: 0.1

...

Additional aesthetics passed to geometries (see example).

Details

Use x and y scale limits to crop map to desired region of interest (see example below).

Value

      Returns a gg object.

Examples

library(fungarium)
data("agaricales_updated")

#clean coordinates and dates
agaricales_cleaned <- coord_clean(agaricales_updated)
agaricales_cleaned <- date_clean(agaricales_cleaned)

#make hex grid
grid <- hex_grid(20000)

#convert lat/long points to sf geometry
agaricales_sf <- sf::st_as_sf(agaricales_cleaned,
                              coords = c("decimalLongitude", "decimalLatitude"),
                              crs = "+proj=latlong +ellps=WGS84 +datum=WGS84")

#convert to cylindrical equal area projection coordinates
agaricales_sf <- sf::st_transform(agaricales_sf,
                                  crs = "+proj=cea +ellps=WGS84 +datum=WGS84")

#assign points to grid cells
agaricales_grid <- sf::st_join(grid, agaricales_sf, join=sf::st_contains, left=FALSE)


#find fire-associated records
string1 <- "(?i)charred|burn(t|ed)|scorched|fire.?(killed|damaged|scarred)|killed.by.fire"
string2 <- "(?i)un.?burn(t|ed)"
trait_rec <- find_trait(agaricales_grid, pos_string=string1, neg_string=string2)

#aggregate occurrences by year-hex_cell-species (optional)
agaricales_grid <- agaricales_grid[,c("year_fixed", "hex", "new_species", "geometry")]
agaricales_grid <- dplyr::distinct(as.data.frame(agaricales_grid))
trait_rec <- trait_rec[,c("year_fixed", "hex", "new_species", "geometry")]
trait_rec <- dplyr::distinct(as.data.frame(trait_rec))

#find fire-associated enrichment per grid cell
fire_enrich <- enrichment(agaricales_grid, trait_rec, ext_var="geometry", by="hex")

#filter out hex cells with low amount of occurrences
fire_enrich <- fire_enrich[fire_enrich$freq>=5,]

#plot trait data on world map
library(ggplot2)
fire_enrich <- sf::st_as_sf(fire_enrich) #convert enrichment table back to sf
map <- trait_map(fire_enrich, aes_mapping = aes(fill=trait_ratio),
                 color=NA, alpha=0.95, size=0.1)
map

#crop map to area of interest (optional)
map+
  ylim(c(300000,6000000))+
  xlim(c(-18000000,-6000000))

hjsimpso/fungarium documentation built on Aug. 23, 2023, 3:59 p.m.