Load packages

library(dplyr)
library(lme4)
library(RColorBrewer)
library(devtools)
library(sjPlot)
library(tidyr)
library(broom.mixed)
library(knitr)
library(broom)
library(jtools)
library(kableExtra)
library(huxtable)
library(dplyr)
library(gtsummary)
library(car)
library(lmerTest)
library(ggplot2)
library(sjmisc)
library(sjstats)
library(sjlabelled)
library(glmmTMB)
library(DHARMa)
library(viridisLite)
library(data.table)
library(effects)
library(emmeans)
library(kableExtra)
library(MuMIn)
library(performance)
library(gridExtra)

load tables, merge

herb_mass <- read.csv("Herb_mass.csv")
leaf_cn <- read.csv("Leaf_CN_r.csv")
sla <- read.csv("SLA.csv")

merged_df <- merge(herb_mass,leaf_cn)
merged_df <- merge(merged_df,sla)
merged_df

merged_df$Plant <- as.factor(merged_df$Plant)
merged_df$Species <- as.factor(merged_df$Species)
merged_df$Plot <- as.factor(merged_df$Plot)

create individual tables for species

artcal_df <- merged_df[merged_df$Species == "Artemisia_californica", ]

plot histograms and linear regression lines

#SLA
sla_plot <- ggplot(merged_df,aes(y = Herbivore_mass_mg, x = SLA))+
  geom_point()+
  geom_smooth(method = "lm")+
  facet_wrap(~Species) + 
  ggtitle("Herbivore mass ~ SLA")+
  xlab("SLA")+ 
  ylab("Herbivore mass")

sla_plot
## `geom_smooth()` using formula = 'y ~ x'

run lmer

## SLA

artcal_sla_lmer <- glmmTMB(Herbivore_mass_mg ~ SLA + (1|Plant) + (1|Plot), data = artcal_df, family = poisson())

artcal_lmer_sla_simres <- simulateResiduals(artcal_sla_lmer)
plot(artcal_lmer_sla_simres, title = "ARTCAL Residual plot of Herbivore mass ~ SLA")

anova_artcal_sla <- Anova(artcal_sla_lmer)
df_artcal_sla_lmer <- as.data.frame(anova_artcal_sla)
kable(df_artcal_sla_lmer, caption = "ANOVA results of ARTCAL herbivore mass ~ SLA")
ANOVA results of ARTCAL herbivore mass ~ SLA
Chisq Df Pr(>Chisq)
SLA 0.3779255 1 0.5387156
## %N

artcal_n_lmer <- glmmTMB(Herbivore_mass_mg ~ Perc_N + (1|Plant) + (1|Plot), data = artcal_df, family = poisson())

artcal_lmer_n_simres <- simulateResiduals(artcal_n_lmer)
plot(artcal_lmer_n_simres, title = "ARTCAL Residual plot of Herbivore mass ~ N")

anova_artcal_n <- Anova(artcal_n_lmer)
df_artcal_n_lmer <- as.data.frame(anova_artcal_n)
kable(df_artcal_n_lmer, caption = "ANOVA results of ARTCAL herbivore mass ~ %N")
ANOVA results of ARTCAL herbivore mass ~ %N
Chisq Df Pr(>Chisq)
Perc_N 0.0237745 1 0.8774602

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.