Tree Rings
  • Contact

On this page

  • Load dataset
  • Set species and variable of interest
  • Transform data
  • Install / Load timecourseRnaseq
  • Part I: Combined Plots
    • Transform data
    • Define column annotation
    • Plot
  • Part II: Individual plots
    • Transform data
    • Plot data

Heatmaps

Load dataset

Show code
library(dplyr)

path = here::here(
  "content/data/pied_net-sensitivity_2025-11-21.RDS"
)
dat <- readRDS(path)

dat |> 
  head() |> 
  kableExtra::kable()
systemfonts and textshaping have been compiled with different versions of Freetype. Because of this, textshaping will not use the font cache provided by systemfonts
variable gs Plot year mu mu_ci
winter_ppt low 27 1900 -1.739341 0.4010559
winter_ppt low 27 1901 -2.393825 0.2053101
winter_ppt low 27 1902 -1.801620 0.4045216
winter_ppt low 27 1903 -2.597147 0.4886096
winter_ppt low 27 1904 -1.495304 0.5284416
winter_ppt low 27 1905 -3.046124 0.2218432

Set species and variable of interest

Show code
sp = "pinus_edulis"

voi = "winter_ppt"

Transform data

Show code
sub <- dat |>
  filter(
    variable == voi
  ) |>
  select(
    - variable
  ) |>
  mutate(
    year = paste0("Y", year),
    plot_name = paste0("P", Plot),
    mu = if_else(
      sign(mu + mu_ci) * sign(mu - mu_ci) == -1,
      0,
      mu
    )
  ) |>
  select(
    gs,
    plot_name,
    year,
    mu
  ) |>
  arrange(
    gs,
    plot_name,
    year
  ) |>
  distinct() 

sub |> 
  head() |> 
  kableExtra::kable()
gs plot_name year mu
high P107 Y1928 1.2133467
high P107 Y1929 0.6882198
high P107 Y1931 0.0000000
high P107 Y1932 0.0000000
high P107 Y1933 0.0000000
high P107 Y1934 2.5141601

Install / Load timecourseRnaseq

Install the latest version of the package from GitHub.

Several packages allow you to install from github, we will use the renv package.

Show code
if (!requireNamespace("renv", quietly = TRUE)) {
  install.packages("renv")
}

Once renv is installed, get the latest version of the timecourseRnaseq package, and the pheatmap package

Show code
if (!requireNamespace("timecourseRnaseq", quietly = TRUE)) {
  renv::install("biplabendu/timecourseRnaseq")
}

if (!requireNamespace("pheatmap", quietly = TRUE)) {
  renv::install("pheatmap") # you can use renv to install from CRAN as well
}

Part I: Combined Plots

Transform data

Showing only the first 6 rows and 6 columns.

Show code
plot_data <- sub |>
  mutate(
    year = paste0(gs, "-", year)
  ) |>
  select(- gs) |>
  tidyr::pivot_wider(
    names_from = year,
    values_from = mu
  ) |>
  select_if(
    ~ sum(is.na(.)) == 0
  )

plot_data[1:6, 1:6]
# A tibble: 6 × 6
  plot_name `high-Y1928` `high-Y1929` `high-Y1931` `high-Y1932` `high-Y1933`
  <chr>            <dbl>        <dbl>        <dbl>        <dbl>        <dbl>
1 P107             1.21         0.688         0           0             0   
2 P11             -2.67         3.00          1.07        1.16          1.83
3 P114             1.64         2.08          1.82        1.93          1.75
4 P127             2.77         1.12          2.14        0.983         1.85
5 P16              3.98         1.50          2.73        2.12          2.17
6 P18              0.923        6.22          2.28        0.723         5.53

Define column annotation

You can make this dataset any way you seem fit.

  • We want each column to be a trait of the plots.
  • We want the rownames to specify the plot corresponding to each trait value

Showing only the first 10 rows

Show code
col_annot <- data.frame(
  colname = plot_data[-1] |> colnames()
) |>
  rowwise() |>
  mutate(
    sample = stringr::str_split(colname, "-")[[1]][1]
  ) |>
  ungroup() |>
  tibble::column_to_rownames("colname")

col_annot |> 
  head()
           sample
high-Y1928   high
high-Y1929   high
high-Y1931   high
high-Y1932   high
high-Y1933   high
high-Y1934   high

Plot

We will use the tc_plot_heatmap() function from the timecourseRnaseq pkg.

Show code
# Read the documentation
?timecourseRnaseq::tc_plot_heatmap

Use the following to plot, clustered by row-wise,

Show code
plot_data |>
  timecourseRnaseq::tc_plot_heatmap(
    title = paste0(sp, "; ", voi),
    col_annot = col_annot,
    cluster_cols = FALSE,
    cutree_cols = 2,
    n_clusters = 5
  )

Part II: Individual plots

Transform data

Show code
dat_list <- sub |>
  group_split(gs) |>
  setNames(
    c("high", "low")
  ) |>
  purrr::map(
    ~ .x |>
      select(
        - gs
      ) |>
      tidyr::pivot_wider(
        names_from = year,
        values_from = mu
      ) |>
      select_if(
        ~ sum(is.na(.)) == 0
      )
  )

Plot data

Show code
p <- purrr::map(
  names(dat_list),
  ~ dat_list[[.x]] |>
    timecourseRnaseq::tc_plot_heatmap(
      title = paste0(sp, "\n", voi, ": ", .x),
      n_clusters = 5
    )
)

 

© Beep Boop Stats