knitr::opts_chunk$set(echo = TRUE)draft
The initial idea changed to sample just ants and dung bettles in a 80 m grid. The Carbon data came in September 2018.
library (tidyverse) # Data handling
library (raster) # grids, rasters
library (rgeos) # coord transform
library (rgdal) # coord transform
library (ggmap) # visualization
library (lubridate) # date managemgemt
library (readxl) # read excel data
library (sf) # vector maps in R
library (sp) # vector old fashon maps in R
library (mapview) # see maps in web
library (sjPlot) # wiew GLM plots
library (sjmisc) # wiew GLM plots
library (sqldf) # Table with permutations
library(fields) # to do image.plotpath <- "C:/Users/diego.lizcano/Box Sync/CodigoR/Carbon-Biodiv/Data"
beetle_data <- read_excel(paste(path,"/TNC_Escarabajo_Carbono_June_2018.xlsx", sep=""),
sheet = "Base_Escarabajos")
beetle_data <- beetle_data %>% group_by(Punto) %>%
mutate (Richness = n_distinct(Especie)) # New column richness
beetle_data$LongDD <- beetle_data$LonDD * (-1)
ants_data <- read_excel(paste(path,"/TNC_Escarabajo_Carbono_June_2018.xlsx", sep=""),
sheet = "Base_Hormigas")
ants_data <- ants_data %>% group_by(Punto) %>%
mutate (Richness = n_distinct(Especie))
ants_data$LongDD <- ants_data$LonDD * (-1)
#### Check maximum
print("max")## [1] "max"
max(unique(beetle_data$Punto))## [1] 722
#### Check minumum
print("min")## [1] "min"
min(unique(beetle_data$Punto))## [1] 16
carbono_data <- read_excel(paste(path,"/CARBONO-06-09-2018.xlsx", sep=""),
sheet = "Densidad-carbono")For carbono check missing points as deep 30 cm in point 711 and deep 10cm in point 718. Take in to account that maximum point number in biodiversity is 722 and minimum 16.
media <- mean(carbono_data$Carbono_percent, na.rm = T)
mediana <-median(carbono_data$Carbono_percent, na.rm = T)
lineas <- carbono_data %>%
group_by(Profundidad) %>%
summarise(
media = mean(Carbono_percent),
mediana = median(Carbono_percent)
)
c1 <- ggplot (data=carbono_data, aes(Carbono_percent) ) +
geom_histogram (color = "white", bins = 100) +
geom_density(color = "sky blue") +
geom_rug (aes(Carbono_percent) ) +
geom_vline(aes(xintercept=media),
data=lineas, linetype="dashed", color = "blue", size=1) +
geom_vline(aes(xintercept=mediana),
data=lineas, linetype="dashed", color = "red", size=1) +
facet_grid (Profundidad ~ .) +
ggtitle("Counts of carbon percent by depth, blue=mean, red=median")
c1c2 <- ggplot (data=carbono_data, aes(x=factor(Profundidad), y=Carbono_percent)) +
geom_boxplot(width = 0.5, outlier.shape = NA) +
geom_jitter(alpha = 1/5, width = 0.2)
c2glm1 <- lm(Carbono_percent ~ factor(Profundidad), data=carbono_data)
anova (glm1)summary(glm1)##
## Call:
## lm(formula = Carbono_percent ~ factor(Profundidad), data = carbono_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.06146 -0.02169 -0.00579 0.01593 0.40659
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.061464 0.001412 43.536 < 2e-16 ***
## factor(Profundidad)20 -0.013511 0.001990 -6.790 1.53e-11 ***
## factor(Profundidad)30 -0.017307 0.001991 -8.694 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.03435 on 1788 degrees of freedom
## Multiple R-squared: 0.04456, Adjusted R-squared: 0.04349
## F-statistic: 41.7 on 2 and 1788 DF, p-value: < 2.2e-16
Si hay diferencias!
# make categorical
carbono_data$Profundidad <- to_factor(carbono_data$Profundidad)
glm2 <- glm(Carbono_percent ~ Densidad_g + Profundidad, data=carbono_data)
ggplot(glm2, aes(x=Densidad_g, y=Carbono_percent)) +
geom_point (alpha = 1/5) +
geom_smooth (method = lm) #+ #facet_grid (Profundidad ~ .)
# wiew as sjPlot
plot_model(glm2, type = "pred", terms = c("Densidad_g", "Profundidad"))summary(glm2)##
## Call:
## glm(formula = Carbono_percent ~ Densidad_g + Profundidad, data = carbono_data)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -0.08362 -0.02058 -0.00627 0.01314 0.40912
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.090359 0.004273 21.145 < 2e-16 ***
## Densidad_g -0.023707 0.003315 -7.152 1.24e-12 ***
## Profundidad20 -0.012132 0.001972 -6.152 9.43e-10 ***
## Profundidad30 -0.015292 0.001984 -7.709 2.08e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for gaussian family taken to be 0.001147769)
##
## Null deviance: 2.2082 on 1790 degrees of freedom
## Residual deviance: 2.0511 on 1787 degrees of freedom
## AIC: -7036.3
##
## Number of Fisher Scoring iterations: 2
YES!!!! ***
beetle_data_sf = st_as_sf(beetle_data, coords = c("LongDD", "LatDD"), crs = "+proj=longlat +datum=WGS84")
# put coords
ants_data_sf = st_as_sf(ants_data, coords = c("LongDD", "LatDD"), crs = "+proj=longlat +datum=WGS84")#
Beetle_rich_map <- mapview(beetle_data_sf, zcol = "Richness",
col.regions = sf.colors(12),
alpha.regions = 0.2, alpha = 0.2,
legend = TRUE,
map.types = "Esri.WorldImagery")#
Beetle_abun_map <- mapview(beetle_data_sf, zcol = "Abundancia",
col.regions = sf.colors(12),
alpha.regions = 0.2, alpha = 0.2,
legend = TRUE,
map.types = "Esri.WorldImagery")sync(Beetle_rich_map, Beetle_abun_map)#
p <- ggplot(beetle_data, aes(x=Cobertura, y=Richness)) +
geom_boxplot()
p#
ant_rich_map <- mapview(ants_data_sf, zcol = "Richness",
col.regions = sf.colors(12),
alpha = 0.1, alpha.regions = 0.2,
legend = TRUE,
map.types = "Esri.WorldImagery")ant_abun_map <- mapview(ants_data_sf, zcol = "Abundancia",
col.regions = sf.colors(15),
alpha = 0.1, alpha.regions = 0.2,
legend = TRUE,
map.types = "Esri.WorldImagery")sync(ant_rich_map, ant_abun_map)