-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
173 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
--- | ||
title: "Article: Animated plots of spectral data" | ||
subtitle: "'ggspectra' `r packageVersion('ggspectra')`" | ||
author: "Pedro J. Aphalo" | ||
date: "`r Sys.Date()`" | ||
output: | ||
rmarkdown::html_vignette: | ||
toc: yes | ||
vignette: > | ||
%\VignetteIndexEntry{Article: Plotting transformed spectral data} | ||
%\VignetteEngine{knitr::rmarkdown} | ||
%\VignetteEncoding{UTF-8} | ||
--- | ||
|
||
## Introduction | ||
|
||
Package `ggspectra` extends `ggplot2` with stats, geoms, scales and annotations suitable for light spectra. It also defines `ggplot()` and `autoplot()` methods specialized for the classes defined in package `photobiology` for storing different types of spectral data. The `autoplot()` methods are described separately in vignette _User Guide: 2 Autoplot Methods_ and the `ggplot()` methods, statistics, and scales in _User Guide: 1 Grammar of Graphics_. | ||
|
||
The new elements can be freely combined with methods and functions defined in packages 'ggplot2', `scales`, `ggrepel`, `gganimate` and other extensions to 'ggplot2'. | ||
|
||
This article, focuses on the animation of plots of time-series of spectra, which can be obtained easily with the current version of package 'ooacquire' from Ocean Optics spectrometers. Such data can also be obtained with radiation transfer models. Kinetics of chemical reactions, or any other temporal change in colour and or intensity. In addition to time, other continuous explanatory variables are well suited to using animations. | ||
|
||
Currently, plots of spectra created with method `autoplot()` cannot be animated with 'gganimate'. | ||
|
||
## Set up | ||
|
||
```{r} | ||
library(ggplot2) | ||
library(photobiology) | ||
library(photobiologySun) | ||
library(photobiologyWavebands) | ||
library(ggspectra) | ||
library(lubridate) | ||
# if suggested packages are available | ||
gganimate_installed <- requireNamespace("gganimate", quietly = TRUE) | ||
eval_chunks <- gganimate_installed | ||
if (eval_chunks) { | ||
library(gganimate) | ||
} else { | ||
message("Please, install package 'gganimate'.") | ||
} | ||
``` | ||
|
||
```{r, include=FALSE, echo=FALSE} | ||
library(knitr) | ||
opts_chunk$set(fig.align = 'center', | ||
fig.show = 'hold', fig.width = 7, fig.height = 4, | ||
cache = FALSE) | ||
options(warnPartialMatchArgs = FALSE) | ||
``` | ||
|
||
We change the default theme. | ||
|
||
```{r} | ||
theme_set(theme_bw()) | ||
``` | ||
|
||
## Short series | ||
|
||
We change the labels in the factor so that the time of measurement can be shown. | ||
|
||
```{r} | ||
my_sun.spct <- sun_evening.spct | ||
time <- when_measured(my_sun.spct, as.df = TRUE) | ||
my_sun.spct$spct.idx <- factor(my_sun.spct$spct.idx, | ||
levels = time[["spct.idx"]], | ||
labels = as.character(round_date(time[["when.measured"]], unit = "minute"))) | ||
my_sun.spct | ||
``` | ||
|
||
We create a base plot to play with: | ||
|
||
```{r, eval=eval_chunks} | ||
ggplot(data = my_sun.spct) + | ||
aes(linetype = spct.idx) + | ||
geom_line() + | ||
scale_x_wl_continuous() + | ||
scale_y_s.e.irrad_continuous() | ||
``` | ||
|
||
We can animate it in different ways. | ||
|
||
```{r, eval=eval_chunks} | ||
ggplot(data = my_sun.spct) + | ||
geom_line() + | ||
scale_x_wl_continuous() + | ||
scale_y_s.e.irrad_continuous() + | ||
transition_states(spct.idx, | ||
transition_length = 2, | ||
state_length = 1) | ||
``` | ||
We need some indication of what spectrum is being displayed. | ||
|
||
```{r, eval=eval_chunks} | ||
ggplot(data = my_sun.spct) + | ||
geom_line() + | ||
scale_x_wl_continuous() + | ||
scale_y_s.e.irrad_continuous() + | ||
transition_states(spct.idx, | ||
transition_length = 2, | ||
state_length = 1) + | ||
ggtitle('Now showing {closest_state}', | ||
subtitle = 'Frame {frame} of {nframes}') | ||
``` | ||
|
||
It is possible to add some annotations. Here some energy irradiances. | ||
|
||
```{r, eval=eval_chunks} | ||
ggplot(data = sun_evening.mspct |> smooth_spct(method = "supsmu", strength = 0.5)) + | ||
geom_line() + | ||
stat_wb_e_irrad(w.band = list(PAR(), Red("Sellaro"), Far_red("Sellaro")), ypos.fixed = 0.075) + | ||
scale_x_wl_continuous() + | ||
scale_y_s.e.irrad_continuous() + | ||
transition_states(spct.idx, | ||
transition_length = 2, | ||
state_length = 1, | ||
) + | ||
ggtitle('Now showing {closest_state}', | ||
subtitle = 'Frame {frame} of {nframes}') | ||
``` | ||
|
||
|
||
## A longer time series | ||
|
||
```{r} | ||
my_sun.spct <- sun_hourly_august.spct | ||
time <- when_measured(my_sun.spct, as.df = TRUE) | ||
my_sun.spct$spct.idx <- factor(my_sun.spct$spct.idx, | ||
levels = time[["spct.idx"]], | ||
labels = as.character(round_date(time[["when.measured"]], unit = "hour"))) | ||
my_sun.spct | ||
``` | ||
```{r, eval=eval_chunks} | ||
ggplot(data = my_sun.spct) + | ||
aes(group = spct.idx) + | ||
geom_line(alpha = 1/3) + | ||
scale_x_wl_continuous() + | ||
scale_y_s.e.irrad_continuous() | ||
``` | ||
|
||
|
||
```{r, eval=eval_chunks} | ||
anim <- ggplot(data = my_sun.spct) + | ||
geom_line() + | ||
scale_x_wl_continuous() + | ||
scale_y_s.e.irrad_continuous() + | ||
transition_states(spct.idx, | ||
transition_length = 2, | ||
state_length = 1) + | ||
ggtitle('Now showing {closest_state}', | ||
subtitle = 'Frame {frame} of {nframes}') | ||
animate(anim, duration = 40, fps = 10) | ||
``` | ||
|
||
```{r, eval=eval_chunks} | ||
anim <- ggplot(data = fscale(my_sun.spct, target = 0.5)) + | ||
geom_line() + | ||
scale_x_wl_continuous() + | ||
scale_y_s.e.irrad_continuous() + | ||
transition_states(spct.idx, | ||
transition_length = 2, | ||
state_length = 1) + | ||
ggtitle('Now showing {closest_state}', | ||
subtitle = 'Frame {frame} of {nframes}') | ||
animate(anim, duration = 40, fps = 10) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters