# 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" 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 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" 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 foundtip.labels <- tree$tip.labelError in eval(expr, envir, enclos): object 'tree' not found Error in gsub("_", " ", species): object 'tip.labels' not found Error in lapply(X = X, FUN = FUN, ...): object 'myfish' not found Error in getQuantTraits(fish.data[myfish]): object 'myfish' not found Error in eval(expr, envir, enclos): object 'species.names' not found Error in getDepth(fish.data[myfish]): object 'myfish' not found Error in eval(expr, envir, enclos): object 'species.names' not found Error in getSize(fish.data[myfish], "length"): object 'myfish' not found 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 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 Error in eval(expr, envir, enclos): object 'depth' not found Error in inherits(phy, "phylo"): object 'tr' not found Error in eval(expr, envir, enclos): object 'size' not found Error in inherits(phy, "phylo"): object 'tr' not found# drop all the data not in the tree pruned.names <- tr$tip.labelError in eval(expr, envir, enclos): object 'tr' not foundsize <- size[pruned.names]Error in eval(expr, envir, enclos): object 'size' not founddepth <- depth[pruned.names]Error in eval(expr, envir, enclos): object 'depth' not found Error in inherits(phy, "phylo"): object 'tr' not found Error in inherits(phy, "phylo"): object 'tr' not foundEnter 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 Error in is.vector(data): object 'depth' not found Error in is.vector(data): object 'depth' not foundinvert <- 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")
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])