Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Example of using taxize and rgbif packages together.
#######################################################
# 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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.