a few simple demonstrations of the rfishbase package.

        
# demo.R rm(list=ls()) require(rfishbase) ###### Grabbing, Loading & Caching Fishbase Data ###### ## Download and parse data for first 40 ids (36 fish) #fish.data <- getData(1:40) ## Or just load the cached copy of the full fishbase data: ## Note that this loads the copy distributed with rfishbase. data(fishbase) ## To get the most recent copy of fishbase, update the cache ## and load that. The update may take up to 24 hours. # updateCache() # loadCache("2011-10-12fishdata.Rdat") ###### Sample Analysis ######### # how many fish are in the dataset? length(fish.data)
[1] 30622
## Lets start by looking at the distribution of all age data available: yr <- getSize(fish.data, "age") qplot(yr, main="Age Distribution", xlab="age (years)");
Error in eval(expr, envir, enclos): could not find function "qplot"
# We can create partitions by taxon. For instance, we get the index # to all fish wrasses and parrotfish by searching for both families # note the use of a "regular expression" to specify the OR command in the search labrid <- familySearch("(Labridae|Scaridae)", fish.data)
Error in eval(expr, envir, enclos): could not find function "familySearch"
goby <- familySearch("Gobiidae", fish.data)
Error in eval(expr, envir, enclos): could not find function "familySearch"
## Let's see if there are more labrid species or goby speices on reefs: # get all the labrids that are reefs labrid.reef <- habitatSearch("reef", fish.data[labrid])
Error in eval(expr, envir, enclos): could not find function "habitatSearch"
# How many species are reef labrids: sum(labrid.reef) # same as: length(fish.data[labrid][labrid.reef])
Error in eval(expr, envir, enclos): object 'labrid.reef' not found
# How many reef gobies: sum (habitatSearch("reef", fish.data[goby]) )
Error in eval(expr, envir, enclos): could not find function "habitatSearch"
# Let's plot the log length distribution of freshwater vs marine fish: hist(log( getSize (fish.data[habitatSearch("freshwater", fish.data)], "length")), col=rgb(1,0,0,.5), breaks=40, freq=F, xlab="length", main="marine fish are bigger")
Error in getSize(fish.data[habitatSearch("freshwater", fish.data)], "length"): could not find function "habitatSearch"
hist(log( getSize (fish.data[habitatSearch("marine", fish.data)], "length")), col=rgb(0,0,1,.5), breaks=40, add=T, freq=F)
Error in getSize(fish.data[habitatSearch("marine", fish.data)], "length"): could not find function "habitatSearch"
# Note that we can also find anadromous fish: anadromous <- habitatSearch("anadromous", fish.data)
Error in eval(expr, envir, enclos): could not find function "habitatSearch"
# this is useful for phylogenetic comparative methods. For instance, load the labrid tree: require(ape) data(labridtree) # get the species names (& remove underscores) tree$tip.label<-gsub("_", " ", tree$tip.label)
Error in gsub("_", " ", tree$tip.label): object 'tree' not found
tip.labels <- tree$tip.label
Error in eval(expr, envir, enclos): object 'tree' not found
myfish <- findSpecies(tip.labels, fish.data)
Error in gsub("_", " ", species): object 'tip.labels' not found
species.names <- sapply(fish.data[myfish], function(x) x$ScientificName)
Error in lapply(X = X, FUN = FUN, ...): object 'myfish' not found
# create a trait matrix of fin data traits <- getQuantTraits(fish.data[myfish])
Error in getQuantTraits(fish.data[myfish]): object 'myfish' not found
rownames(traits) <- species.names
Error in eval(expr, envir, enclos): object 'species.names' not found
depths <- getDepth(fish.data[myfish])
Error in getDepth(fish.data[myfish]): object 'myfish' not found
rownames(depths) <- species.names
Error in eval(expr, envir, enclos): object 'species.names' not found
size <- getSize(fish.data[myfish], "length")
Error in getSize(fish.data[myfish], "length"): object 'myfish' not found
names(size) <- species.names
Error in eval(expr, envir, enclos): object 'species.names' not found
# who's missing from treebase entirely? missing<-tip.labels[! tip.labels %in% species.names ]
Error in eval(expr, envir, enclos): object 'tip.labels' not found
tr<- drop.tip(tree, missing)
Error in inherits(phy, "phylo"): object 'tree' not found
# drop species with NAs for max depth depth <- depths[,2]
Error in eval(expr, envir, enclos): object 'depths' not found
missing<-names(depth[is.na(depth)])
Error in eval(expr, envir, enclos): object 'depth' not found
tr<- drop.tip(tr, missing)
Error in inherits(phy, "phylo"): object 'tr' not found
# repeat for NAs for length missing<-names(size[is.na(size)])
Error in eval(expr, envir, enclos): object 'size' not found
tr<- drop.tip(tr, missing)
Error in inherits(phy, "phylo"): object 'tr' not found
# drop all the data not in the tree pruned.names <- tr$tip.label
Error in eval(expr, envir, enclos): object 'tr' not found
size <- size[pruned.names]
Error in eval(expr, envir, enclos): object 'size' not found
depth <- depth[pruned.names]
Error in eval(expr, envir, enclos): object 'depth' not found
# Does depth correlate with size after correcting for phylogeny? x <- pic(size, tr)
Error in inherits(phy, "phylo"): object 'tr' not found
y <- pic(depth, tr)
Error in inherits(phy, "phylo"): object 'tr' not found
summary(lm(y~x-1)) # Yes
Enter a frame number, or 0 to exit 1: build_package(".", "inst/doc/html") 2: build_demos(package) 3: evaluate(demo_code, new.env(parent = globalenv())) 4: unlist(mapply(eval.with.details, parsed$expr, parsed$src, MoreArgs = list(e 5: mapply(eval.with.details, parsed$expr, parsed$src, MoreArgs = list(envir = 6: function (expr, envir = parent.frame(), enclos = NULL, src = NULL, debug = 7: try(ev <- withCallingHandlers(withVisible(eval(expr, envir, enclos)), warni 8: tryCatch(expr, error = function(e) { call <- conditionCall(e) if (!is 9: tryCatchList(expr, classes, parentenv, handlers) 10: tryCatchOne(expr, names, parentenv, handlers[[1]]) 11: doTryCatch(return(expr), name, parentenv, handler) 12: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHand 13: withVisible(eval(expr, envir, enclos)) 14: eval(expr, envir, enclos) 15: eval(expr, envir, enclos) 16: summary(lm(y ~ x - 1)) 17: lm(y ~ x - 1) 18: eval(mf, parent.frame()) 19: eval(expr, envir, enclos) 20: model.frame(formula = y ~ x - 1, drop.unused.levels = TRUE) 21: model.frame.default(formula = y ~ x - 1, drop.unused.levels = TRUE) 22: eval(predvars, data, env) 23: eval(expr, envir, enclos)
Error in summary(lm(y ~ x - 1)): error in evaluating the argument 'object' in selecting a method for function 'summary': Error in eval(expr, envir, enclos) : object 'y' not found
require(geiger) bm <- fitContinuous(tr, depth, model="BM")
Error in is.vector(data): object 'depth' not found
ou <- fitContinuous(tr, depth, model="OU")
Error in is.vector(data): object 'depth' not found
invert <- sapply(fish.data, function(x) length(grep("invertebrate", x$trophic))>0) piscivore <- sapply(fish.data, function(x) length(grep("(piscivore|fish)", x$trophic))>0) hist(log( getSize (fish.data[invert], "length")), col=rgb(1,0,0,.5), breaks=40, freq=F, xlab="length", main="invert eaters vs piscivores")

hist(log( getSize (fish.data[piscivore], "length")), col=rgb(0,0,1,.5), breaks=40, add=T, freq=F)

## Using a treebase tree require(rtreebase)
Loading required package: rtreebase Warning message: there is no package called 'rtreebase'
## Need to find an example of a fish tree on treebase that has branchlengths #trees <- search_treebase("Scarus", by="taxon", branch=TRUE) #str <- trees[[1]]$tip.label #gsub("(\\w+)_(\\w+)_.*", "\\1 \\2", str) #myfish <- findSpecies(species, fish.data) #getQuantTraits(fish.data[myfish])