public
Created

Example of using taxize and rgbif packages together.

  • Download Gist
taxize_rgbif_eg.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#######################################################
# by Scott Chamberlain <myrmecocystus@gmail.com>
# for blog post titled "Awesome use case: check my species names and
# gimme some distribution data"
#######################################################
# Load packages
install.packages(c("RCurl","stringr","XML","plyr","RJSONIO"))
require(RCurl);require(stringr);require(XML);require(plyr);require(RJSONIO)
 
# Clone taxize and rgbif repositories from GitHub
# taxize: https://github.com/SChamberlain/taxize_
# rgbif: https://github.com/SChamberlain/rgbif
# Instructions on signing up for GitHub, and cloning repos here:
# http://ropensci.org/installation/
 
# Load taxize functions (b/c taxize isn't packaged up yet - apologies)
setwd('path/to/taxize_/R')
funcs <- as.list(dir()) # create list of function file names
require(plyr)
l_ply(funcs, source) # loads each function in a loop
 
 
# Load rgbif functions (b/c rgbif isn't packaged up yet - apologies)
setwd('path/to/rgbif/R')
source('occurrencelist.R') # we only need occurrencelist.R for this example
 
##########################################
# Step (1) Checking species names
# read data
ournames <- c("Agapornis_roseicapillis", "Catharacta_maccormicki",
"Catharacta_skua", "Cathartes_aura", "Catharus_bicknelli",
"Catharus_fuscescens", "Catharus_guttatus", "Catharus_minimus",
"Catharus_ustulatus", "Ceratogymna_brevis")
# Check names
checkname <- function(name) {
# name: scientific name
# get TSN (taxonomic serial number)
if(class(try(tsn <- get_tsn(name, "sciname", by_="name"), silent = T)) == "try-error")
{tsn <- "no_results"}
# check accepted name
out <- getacceptname(tsn)
if(out[[1]] == "no_results") {list("check_spelling", name, "check_spelling", out)} else
if(length(out) == 2) {list("new_name", name, as.character(out)[[1]], as.character(out)[[2]])} else
if(class(as.numeric(out)) == "numeric") {list("good_name", name, name, out)}
}
 
itisout <- llply(ournames, checkname, .progress = "text") # query ITIS
 
dfnames <- ldply(itisout, function(x) { # make a data frame of results
out_ <- as.data.frame(x)
names(out_) <- c("status", "name_old", "name_new", "TSN")
out_})
 
dfnames
 
##########################################
# Step (2) Retreiving GBIF data
# prep data frame from above
dfnames$gbifname <- gsub("_", " ", dfnames[,3]) # create new name column
 
dfnames # we now have a column of names without the underscore for GBIF search
 
dfnames <- dfnames[-1,] # remove row 1 b/c it's spelling wrong
 
# get lat/long data
gbiftestout <- llply(as.list(dfnames[,5]), function(x) occurrencelist(x,
coordinatestatus = TRUE, maxresults = 10, latlongdf = TRUE))
 
gbiftestout[[1]] # here's the data frame of results from one species
 
gbiftestout_df <- ldply(gbiftestout, identity) # make a data frame of all results
 
 
rbind(head(gbiftestout_df), tail(gbiftestout_df)) # look at first and last 6 rows
 
 
 
### make maps
install.packages("maps")
require(ggplot2)
try_require("maps")
 
world <- map_data("world")
mexico <- subset(world, region=="Mexico")
 
# Make a plot for Stercorarius maccormicki
ggplot(world, aes(long, lat)) +
geom_polygon(aes(group = group), fill = "white", color = "gray40", size = .2) +
geom_jitter(data = gbiftestout[[1]],
aes(longitude, latitude), alpha=0.6, size = 4, color = "blue") +
opts(title = "Stercorarius maccormicki")
 
# Make a plot for Catharus guttatus, just in Mexico though
ggplot(mexico, aes(long, lat)) +
geom_polygon(aes(group = group), fill = "white", color = "gray40", size = .2) +
geom_jitter(data = gbiftestout[[6]],
aes(longitude, latitude), alpha=0.6, size = 4, color = "blue") +
opts(title = "Catharus guttatus")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.