---
title: "tealeaves: Intermediate Users"
author: "Chris Mur"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{tealeaves: Intermediate Users}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

library(dplyr)
library(ggplot2)
library(magrittr)
library(tealeaves)

```

In this more advanced example, we'll model leaf temperature across a gradient of air temperatures from 0 to 40 °C for leaves under low and high light conditions. We'll also look at differences between hypostomatous and amphistomatous leaves. We use the \code{unitless = TRUE} argument to save time^[The 'unit'ed version ensures that all calculations start and end with the proper units. The unitless functions *should* give the same answer because I run tests every time the package is updated, but it's always good to double check for bugs.], but it still takes a couple minutes. I've saved the output as example data called \code{tl_example1}.

```{r, eval = FALSE, echo = TRUE}

# install tidyverse packages if necessary
# install.packages("tidyverse")

library(dplyr)
library(ggplot2)
library(magrittr)
library(tealeaves)

# Parameter sets ----

cs <- tealeaves::make_constants()
lp <- tealeaves::make_leafpar(
  replace = list(
    # Hypo- and amphistomatous leaves
    logit_sr = set_units(c(-Inf, 0))
  )
)
ep <- tealeaves::make_enviropar(
  replace = list(
    # Low and high light
    S_sw = set_units(c(220, 660), "W/m^2"),
    # Air temperature gradient
    T_air = set_units(seq(278.15, 313.15, length.out = 25), "K")  
    )
)

# Run tleaves ----
tl_example1 <- tleaves(lp, ep, cs)

usethis::use_data(tl_example1)

```

Next, we'll usee **ggplot2** to plot the leaf-to-air temperature difference as function of air temperature at the two light levels.

```{r plot temperature, echo = FALSE, eval = TRUE, fig.width=7}

tl_example1 %<>%
  
  # Drop units for plotting
  mutate_if(function(x) inherits(x, what = "units"), drop_units) %>%
  
  # Calculate leaf temperature differential
  mutate(dT = T_leaf - T_air) %>%
  
  # Factorize stomatal ratio 
  mutate("Stomatal Ratio" = case_when(
    logit_sr == -Inf ~ "hypostomatous",
    logit_sr == 0 ~ "amphistomatous"
  )) %>%
  
  # Factorize light environment 
  mutate(Light = case_when(
    round(S_sw, 0) == 220 ~ "Shade",
    round(S_sw, 0) == 660 ~ "Sun"
  ))

ggplot(tl_example1, aes(T_air, dT, color = `Stomatal Ratio`)) +
  facet_grid(Light ~ .) + 
  geom_hline(yintercept = 0) +
  geom_line() +
  labs(x = "Air Temperature [K]", 
       y = "Leaf-to-Air Temperature Difference",
       color = "Stomatal Ratio") + 
  theme_minimal() + 
  theme(panel.grid.minor = element_blank()) +
  NULL

```

The results show that leaves are warmer than the atmosphere at cool temperatures and cooler at high temperatures. In sunnier conditions, leaves are warmer. Finally, amphistomatous leaves are somewhat cooler because they evaporate more water. To see this, let's look directly at transpiration in the same data:

```{r plot transpiration, echo = FALSE, eval = TRUE, fig.width=7}

ggplot(tl_example1, aes(T_air, 1000 * E, color = `Stomatal Ratio`)) +
  facet_grid(Light ~ .) + 
  geom_line() +
  labs(x = "Air Temperature [K]", 
       y = expression(paste("Evaporation [mmol ", m^{-2}~s^{-1}, "]")),
       color = "Stomatal Ratio") + 
  theme_minimal() + 
  theme(panel.grid.minor = element_blank()) +
  NULL

```
