alicelinder / senior-moment

Senior thesis on range extremes of deciduous trees in the northeastern US
0 stars 0 forks source link

Fig. 2: Fraction Basal Area vs. Position in Lat. Range (1/10) #11

Closed alicelinder closed 7 years ago

alicelinder commented 7 years ago

Having some issues with the code. I've decided to go with the Competitive Index outlined in Wykoff et. al 1982 (sum of basal areas of trees larger than focal individual). Everything runs smoothly until I try to graph it. It appears to be problems with levels vs. labels with regards to the sites. Any help would be much appreciated @danfbflynn

`rm(list = ls()) setwd("~/GitHub/senior-moment/data")

library(vegan) # install.packages("vegan") library(lme4)# install.packages("lme4") library(scales)# install.packages("scales") library(ggplot2) # install.packages("ggplot2") library(plyr) library(reshape)

library(sjPlot) # install.packages("sjPlot")

detach("package:dplyr", unload=TRUE) focal.dbh <- read.csv("focal.species.dbh.csv") focal.dbh <- rename(focal.dbh, c("DBH" = "fDBH"))

head(focal.dbh)

other.dbh <- read.csv("all.species.dbh.csv") other.dbh["index"] <- 1:3893 head(other.dbh)

merge datasets

all.dbh <- merge(focal.dbh, other.dbh, by = "Individual")

head(all.dbh)

Clean up DBH measures

all.dbh$DBH <- as.character(all.dbh$DBH) # convert from factor to character all.dbh$fDBH <- as.character(all.dbh$fDBH)

all.dbh$DBH[all.dbh$DBH == "less.than.1"] = 0.5 # make all less.than.1 into .5 all.dbh$fDBH[all.dbh$fDBH == "less.than.1"] = 0.5 # make all less.than.1 into .5 all.dbh$DBH[all.dbh$DBH == "less.than.2"] = 1 # make all <2 measures into 1

all.dbh$DBH <- as.numeric(as.character(all.dbh$DBH)) all.dbh$fDBH <- as.numeric(as.character(all.dbh$fDBH))# convert from factor to numeric

summary(all.dbh$DBH) # should not be any NA

View(all.dbh)

say whether focal DBH is smaller than competing individual

all.dbh$greater <- all.dbh$fDBH < all.dbh$DBH all.dbh <- as.data.frame(all.dbh)

View(all.dbh)

filter data so only individiuals with DBHs greater than the focal DBH exist

library(dplyr) compet <- filter(all.dbh, DBH > fDBH)

create basal area variables of only those individuals

compet["BA"] <- .5pi(compet$DBH)^2 compet["fBA"] <- .5pi(compet$fDBH)^2

sum the basal area for each individual

focal.dbh["competing.BA"] <- data.frame(tapply(compet$BA, compet$Individual, sum)) focal.dbh["log.cBA"] <- log(focal.dbh$competing.BA) focal.dbh$log.cBA <- as.integer(as.factor(as.character(focal.dbh$log.cBA))) focal.dbh$competing.BA <- as.integer(as.factor(as.character(focal.dbh$competing.BA))) class(focal.dbh$log.cBA)

Site and species information based on last 2 letters of individuals

focal.dbh$Individual <- as.character(focal.dbh$Individual)

focal.dbh$Site <- unlist( lapply(strsplit(focal.dbh[,1], "_"), function(x) x[[2]])) focal.dbh$sp <- substr(focal.dbh$Individual, 1, 6)

class(focal.dbh$Site) class(focal.dbh) class(focal.dbh$log.cBA)

looking at data summaries, species at each site across gradient

levels(focal.dbh$Site) <- c(3, 1, 4, 2)

focal.dbh$Site <- factor(as.numeric(focal.dbh$Site), levels = c("HF", "WM", "GR", "SH"))

coralt <- focal.dbh[focal.dbh$sp == "CORALT",] hamvir <- focal.dbh[focal.dbh$sp == "HAMVIR",] sorame <- focal.dbh[focal.dbh$sp == "SORAME",] acepen <- focal.dbh[focal.dbh$sp == "ACEPEN",] focal.small <- rbind(coralt, sorame, hamvir, acepen)

focal.medium <- rbind(hamvir, acepen)

betpap <- focal.dbh[focal.dbh$sp == "BETPAP",] faggra <- focal.dbh[focal.dbh$sp == "FAGGRA",] quealb <- focal.dbh[focal.dbh$sp == "QUEALB",] focal.large <- rbind(betpap, faggra, quealb)

grah shows numbers (possibly due to infinite log values?)

ggplot(focal.small, aes(as.numeric(Site), log.cBA, color = sp)) +

geom_smooth( se = F, aes(color = sp)) +

geom_point() + xlab("Site") + facet_wrap(~sp, ncol = 2) + ylab("Fraction of total Basal Area")

no points appear on this graph

ggplot(focal.dbh, aes(as.numeric(Site), as.numeric(competing.BA), color = sp)) + geom_point() + xlab("Site") + facet_wrap(~sp, ncol=4)`