| Title: | Tidy Functional Data Wrangling and Visualization |
| Version: | 0.1.1 |
| Description: | Represent, visualize, describe and wrangle functional data in tidy data frames, building on the 'tf' package. Provides data types for functional observations that work as columns in data frames, enabling manipulation with 'dplyr' verbs and visualization with 'ggplot2' geoms designed for functional data. |
| License: | MIT + file LICENSE |
| URL: | https://github.com/tidyfun/tidyfun, https://tidyfun.github.io/tidyfun/ |
| BugReports: | https://github.com/tidyfun/tidyfun/issues |
| Depends: | R (≥ 4.1), tf (≥ 0.4.0) |
| Imports: | cli, dplyr, GGally, ggplot2, grid, pillar, purrr, rlang, tibble, tidyr (≥ 1.0.0), tidyselect (≥ 1.0.0) |
| Suggests: | covr, fda, fdasrvf, gridExtra, janitor, knitr, lme4, maps, mgcv, refund, rmarkdown, scales, testthat (≥ 3.0.0), tidyverse, viridisLite |
| VignetteBuilder: | knitr |
| Config/testthat/edition: | 3 |
| Encoding: | UTF-8 |
| LazyData: | true |
| RoxygenNote: | 7.3.3 |
| NeedsCompilation: | no |
| Packaged: | 2026-04-07 15:34:54 UTC; fabians |
| Author: | Fabian Scheipl |
| Maintainer: | Fabian Scheipl <fabian.scheipl@googlemail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-04-13 14:20:02 UTC |
tidyfun: Tidy Functional Data Wrangling and Visualization
Description
tidyfun makes data wrangling and exploratory analysis for functional data
in R easier.
The tf package defines the core functional data classes and methods.
tidyfun builds on those objects with:
data-frame workflows for functional data:
tf_gather()etc.graphics helpers for
tfobjects and tidy functional data frames:geom_spaghetti(),gglasagna()documentation and examples that show how to use
tfobjects in tidy workflows
Start withvignette(package = "tidyfun")for the article index.
Author(s)
Maintainer: Fabian Scheipl fabian.scheipl@googlemail.com (ORCID) [copyright holder]
Authors:
Jeff Goldsmith
Julia Wrobel (ORCID)
Other contributors:
Maximilian Mücke (ORCID) [contributor]
See Also
Useful links:
Report bugs at https://github.com/tidyfun/tidyfun/issues
Add layers to tf_ggplot objects
Description
Add layers to tf_ggplot objects
Usage
## S3 method for class 'tf_ggplot'
e1 + e2
Arguments
e1 |
A tf_ggplot object |
e2 |
A ggplot2 layer, scale, theme, etc. |
Value
A modified tf_ggplot object.
Add a tf aesthetic to an ggplot2 mapping object
Description
Add a tf aesthetic to an ggplot2 mapping object
Usage
add_tf_aes_to_mapping(mapping, aes_name, val_col, arg_col, id_col)
Autoplot and autolayer methods for tf objects
Description
Convenient plotting methods for tf objects. autoplot() creates a
complete spaghetti plot, autolayer() creates a layer that can be
added to an existing ggplot2::ggplot() or tf_ggplot().
Usage
## S3 method for class 'tf'
autoplot(object, ...)
## S3 method for class 'tf'
autolayer(object, ...)
Arguments
object |
a |
... |
passed to |
Value
A tf_ggplot() object for autoplot(), a ggplot2::layer() object for autolayer().
See Also
Other tidyfun visualization:
ggcapellini,
gglasagna(),
ggspaghetti
Examples
library(ggplot2)
f <- tf_rgp(5)
autoplot(f)
ggplot() + autolayer(f)
tf_ggplot() + autolayer(f)
Build long-format data and new mapping for a single tf layer
Description
Evaluates the layer's tf aesthetics independently on their own natural grid
(or the user-specified grid). Called by finalize_tf_ggplot() per layer.
Usage
build_tf_layer_data(
layer_info,
plot_tf_aes,
scalar_col_map,
layer_idx,
enriched_data,
user_arg,
interpolate
)
Arguments
layer_info |
List from |
plot_tf_aes |
tf aesthetics from the plot level (used when |
scalar_col_map |
Named list: key -> column name in enriched_data |
layer_idx |
Integer index of this layer (for keying layer-level scalar aes) |
enriched_data |
Data frame with original data + pre-evaluated scalar tf columns |
user_arg |
Optional evaluation grid (overrides natural grid) |
interpolate |
Whether to interpolate tf objects to |
Value
NULL when no effective tf aes, otherwise list(long_data, new_mapping)
Congestive heart failure accelerometry data
Description
Activity data from a study of congestive heart failure (CHF) patients. Data were originally presented in Huang et al. (2019); these data are publicly available, with download information in the paper.
Usage
chf_df
Format
A tibble with 329 rows and 8 columns:
- id
(numeric) Subject identifier.
- gender
(character)
"Male"or"Female".- age
(numeric) Age in years.
- bmi
(numeric) Body mass index.
- event_week
(numeric) Week of cardiac event.
- event_type
(character) Type of cardiac event.
- day
(ordered factor) Day of the week (
Mon<Tue< ... <Sun).- activity
(
tfd_reg) Minute-by-minute activity counts over a 24-hour period (arg domain 1–1440).
Source
Data are from a study of physical activity in CHF patients conducted by Huang et al. The original data are publicly available; see the paper for download details.
References
Huang, L., Bai, J., Ivanescu, A., Harris, T., Maurer, M., Green, P., and Zipunnikov, V. (2019). Multilevel Matrix-Variate Analysis and its Application to Accelerometry-Measured Physical Activity in Clinical Populations. Journal of the American Statistical Association, 114(526), 553–564. doi:10.1080/01621459.2018.1482750
See Also
dti_df for another example dataset,
vignette("x04_Visualization", package = "tidyfun") for usage examples.
Other tidyfun datasets:
dti_df
Examples
chf_df
library(ggplot2)
chf_df |>
dplyr::filter(id %in% 1:5) |>
gglasagna(activity, order_by = mean)
Diffusion tensor imaging data
Description
Fractional anisotropy (FA) tract profiles for the corpus callosum (cca)
and the right corticospinal tract (rcst) from a diffusion tensor imaging
(DTI) study of multiple sclerosis patients and healthy controls.
The original data in refund::DTI include additional variables (pasat,
Nscans, visit.time) that were not carried over here.
Usage
dti_df
Format
A tibble with 382 rows and 6 columns:
- id
(numeric) Subject identifier.
- visit
(integer) Visit number.
- sex
(factor)
"male"or"female".- case
(factor)
"control"or"MS"(multiple sclerosis status).- cca
(
tfd_irreg) FA tract profiles for the corpus callosum (up to 93 evaluation points, domain 0–1).- rcst
(
tfd_irreg) FA tract profiles for the right corticospinal tract (up to 55 evaluation points, domain 0–1).
Details
If you use this data as an example in written work, please include the following acknowledgment: "The MRI/DTI data were collected at Johns Hopkins University and the Kennedy-Krieger Institute."
Source
Data are from the Johns Hopkins University and the Kennedy-Krieger Institute. Also available in a different format as refund::DTI.
References
Goldsmith, J., Bobb, J., Crainiceanu, C., Caffo, B., and Reich, D. (2011). Penalized Functional Regression. Journal of Computational and Graphical Statistics, 20(4), 830–851. doi:10.1198/jcgs.2010.10007
Goldsmith, J., Crainiceanu, C., Caffo, B., and Reich, D. (2012). Longitudinal Penalized Functional Regression for Cognitive Outcomes on Neuronal Tract Measurements. Journal of the Royal Statistical Society: Series C, 61(3), 453–469. doi:10.1111/j.1467-9876.2011.01031.x
See Also
chf_df for another example dataset,
vignette("x04_Visualization", package = "tidyfun") for usage examples.
Other tidyfun datasets:
chf_df
Examples
dti_df
library(ggplot2)
dti_df |>
dplyr::filter(visit == 1) |>
tf_ggplot(aes(tf = cca, color = case)) +
geom_line(alpha = 0.3)
Finalize tf_ggplot by processing all tf layers independently
Description
Each layer is evaluated on its own natural argument grid (Option B architecture).
The base ggplot has data = NULL; each layer receives its own long-format data.
Usage
finalize_tf_ggplot(tf_plot)
Arguments
tf_plot |
A tf_ggplot object with accumulated layers |
Value
Regular ggplot object with all layers properly transformed
Glyph plots for tf objects
Description
Plots a miniature glyph / sparkline for each entry of a tf-object.
(Capellini are tiny spaghetti – angel hair pasta.) Aesthetics x and y
specify the location of the glyphs, the tf aesthetic defines their shapes.
The aliases geom_cappelini, geom_cappellini, and geom_capelini are
also accepted.
Usage
stat_capellini(
mapping = NULL,
data = NULL,
geom = "capellini",
position = "identity",
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
arg = NULL,
add_lines = FALSE,
add_boxes = TRUE,
width = NULL,
height = NULL,
...
)
geom_capellini(
mapping = NULL,
data = NULL,
stat = "capellini",
position = "identity",
...,
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
arg = NULL,
add_lines = TRUE,
add_boxes = TRUE,
width = NULL,
height = NULL,
box.colour = "#0000001A",
box.linetype = 1,
box.fill = NA,
box.linewidth = 0.1,
box.alpha = 0.1,
line.colour = "black",
line.linetype = 2,
line.linewidth = 0.3,
line.alpha = 0.5
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geometric object to use to display the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
na.rm |
remove NAs? defaults to |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
arg |
where to evaluate |
add_lines |
should a reference line in the middle of the range of the functions' values be added to each glyph? defaults to TRUE |
add_boxes |
should a box be added to frame each glyph? defaults to TRUE |
width |
the width of the glyphs. Defaults to 2/3 of the |
height |
the height of the glyphs. Defaults to 2/3 of the |
... |
Other arguments passed on to
|
stat |
the stat to use; defaults to |
box.colour |
aesthetic property of the box |
box.linetype |
aesthetic property of the box |
box.fill |
aesthetic property of the box |
box.linewidth |
aesthetic property of the box |
box.alpha |
aesthetic property of the box |
line.colour |
aesthetic property of the reference line |
line.linetype |
aesthetic property of the reference line |
line.linewidth |
aesthetic property of the reference line |
line.alpha |
aesthetic property of the reference line |
Value
A ggplot2::layer() object for use in a ggplot.
tf aesthetic
Mandatory. Used to designate a column of class tf to be visualized as glyphs.
See Also
Other tidyfun visualization:
autoplot.tf(),
gglasagna(),
ggspaghetti
Examples
library(ggplot2)
weather <- fda::CanadianWeather
canada <- data.frame(
place = weather$place,
region = weather$region,
lat = weather$coordinates[, 1],
lon = -weather$coordinates[, 2],
region = weather$region
)
canada$temp <- tfd(t(weather$dailyAv[, , 1]), arg = 1:365)
canada$precipl10 <- tfd(t(weather$dailyAv[, , 3]), arg = 1:365) |> tf_smooth()
canada_map <-
data.frame(maps::map("world", "Canada", plot = FALSE)[c("x", "y")])
# map of canada with annual temperature averages in red, precipitation in blue:
ggplot(canada, aes(x = lon, y = lat)) +
geom_capellini(aes(tf = precipl10), width = 3, height = 5, colour = "blue") +
geom_capellini(aes(tf = temp), width = 3, height = 5, colour = "red") +
geom_path(data = canada_map, aes(x = x, y = y), alpha = 0.1) +
coord_quickmap()
ggplot(canada, aes(x = lon, y = lat, colour = region)) +
geom_capellini(aes(tf = precipl10),
width = 5, height = 3,
line.linetype = 1, box.fill = "white", box.alpha = 0.5, box.colour = NA
)
Error bands using tf objects as bounds
Description
Plots a shaded region between tf-objects ymax and ymin.
This is primarily intended to help with plotting confidence bands
although other purposes are possible.
Usage
stat_errorband(
mapping = NULL,
data = NULL,
geom = "errorband",
position = "identity",
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
arg = NULL,
...
)
geom_errorband(
mapping = NULL,
data = NULL,
position = "identity",
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
arg = NULL,
...
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geometric object to use to display the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
na.rm |
remove NAs? defaults to |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
arg |
where to evaluate |
... |
Other arguments passed on to
|
Value
A ggplot2::layer() object for use in a ggplot.
Examples
set.seed(1221)
data <- data.frame(id = factor(1:2))
data$f <- tf_rgp(2)
data$ymax <- data$f + 1
data$ymin <- data$f - 1
library(ggplot2)
ggplot(data, aes(y = f, color = id)) +
geom_spaghetti() +
geom_errorband(aes(ymax = ymax, ymin = ymin, fill = id)) +
facet_wrap(~id)
Functional boxplots for tf objects
Description
Draw functional boxplots based on functional depth rankings.
Usage
stat_fboxplot(
mapping = NULL,
data = NULL,
geom = "fboxplot",
position = "identity",
...,
orientation = NA,
coef = 1.5,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
depth = "MBD",
depth_fn = NULL,
fence_fn = NULL,
central = 0.5,
arg = NULL
)
geom_fboxplot(
mapping = NULL,
data = NULL,
stat = "fboxplot",
position = "identity",
...,
outliers = TRUE,
outlier.colour = NULL,
outlier.color = NULL,
outlier.fill = NULL,
outlier.shape = NULL,
outlier.size = NULL,
outlier.stroke = 0.5,
outlier.alpha = NULL,
whisker.colour = NULL,
whisker.color = NULL,
whisker.linetype = NULL,
whisker.linewidth = NULL,
staple.colour = NULL,
staple.color = NULL,
staple.linetype = NULL,
staple.linewidth = NULL,
median.colour = NULL,
median.color = NULL,
median.linetype = NULL,
median.linewidth = NULL,
box.colour = NULL,
box.color = NULL,
box.linetype = NULL,
box.linewidth = NULL,
notch = FALSE,
notchwidth = 0.5,
staplewidth = 0,
varwidth = FALSE,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE,
depth = "MBD",
depth_fn = NULL,
fence_fn = NULL,
central = 0.5,
arg = NULL
)
Arguments
mapping, data, position, show.legend, inherit.aes, na.rm, orientation, ... |
Passed on to |
coef |
Inflation factor for the central envelope used to define outer fences, defaults to 1.5. |
depth |
Character scalar naming the built-in depth measure passed to
|
depth_fn |
Optional custom depth function. Must return one numeric depth value per function. |
fence_fn |
Optional custom fence function. Must return a list with
elements |
central |
Fraction of deepest curves used to construct the central
envelope. Defaults to |
arg |
Optional evaluation grid used for depth calculation, envelopes, and
drawing. Defaults to the natural grid of the mapped |
stat, geom |
Use the functional boxplot stat/geom. |
outliers |
Should outlying curves be drawn? |
outlier.colour, outlier.color, outlier.fill, outlier.shape, outlier.size |
Styling parameters for outlier curves. |
outlier.stroke |
Accepted for interface compatibility with
|
outlier.alpha |
Alpha used for outlier curves. |
whisker.colour, whisker.color, whisker.linetype, whisker.linewidth |
Styling parameters for fence lines. |
staple.colour, staple.color, staple.linetype, staple.linewidth |
Accepted for
interface compatibility with |
median.colour, median.color, median.linetype, median.linewidth |
Styling parameters for the median curve. |
box.colour, box.color, box.linetype, box.linewidth |
Styling parameters for the central band outline. |
notch, notchwidth, staplewidth, varwidth |
Accepted for interface
compatibility with |
Details
stat_fboxplot() computes a median curve (thick line), a central region
envelope (filled ribbon), functional fences (dashed lines), and optional
outlying curves (defined as "exceeds the fences somewhere", plotted as solid lines)
from a tf column. geom_fboxplot() draws these summaries as a band plus
line layers.
The interface intentionally follows ggplot2::stat_boxplot() and
ggplot2::geom_boxplot() where this is meaningful for functional data.
Use aes(tf = f) to map a tf column. Separate functional boxplots inside a
panel are defined by group, colour/color, or fill; separate panels are
handled through facetting. Note that only color or fill need to be
specified explicitly, as the other will be automatically mapped to the same
variable if not provided.
Value
A ggplot2 layer.
Examples
library(ggplot2)
set.seed(1312)
data <- data.frame(id = 1:50, grp = rep(c("A", "B"), each = 25))
data$f <- tf_rgp(50) + 5 * (data$grp == "A")
tf_ggplot(data, aes(tf = f, fill = grp)) + # same as `colour = grp` here!
geom_fboxplot(alpha = 0.3)
tf_ggplot(data, aes(tf = f)) +
geom_fboxplot(orientation = "y")
Lasagna plots for tfs using ggplot2
Description
Lasagna plots show one color bar for each function.
Usage
gglasagna(
data,
tf,
order = NULL,
label = NULL,
arg = NULL,
order_by = NULL,
order_ticks = TRUE
)
Arguments
data |
A data frame containing the |
tf |
bare name of the |
order |
(optional) bare name of a column in |
label |
(optional) bare name of a column in |
arg |
|
order_by |
a function applied to each row in |
order_ticks |
add horizontal lines indicating borders between levels of
|
Details
The vertical order of the lasagna layers is increasing in
-
order(if provided), the values returned by
order_by(if provided),and the row number of the observations.
i.e., lowest values are on top so that by default the first layer is the first
observation in data and the vertical order of the layers is the
ordering of observations obtained by dplyr::arrange(data, order, order_by(value), row_number()).
Value
a ggplot2 object
See Also
Other tidyfun visualization:
autoplot.tf(),
ggcapellini,
ggspaghetti
Examples
library(ggplot2)
set.seed(1221)
data <- expand.grid(group = factor(1:5), rep = 1:10)
data <- dplyr::mutate(data,
id = paste(group, rep, sep = "-"),
f = tf_rgp(50),
fb = tfb(f)
)
gglasagna(data, f, label = id)
gglasagna(data, fb, label = id, order = group)
# order is lowest first / on top by default
gglasagna(data, f, label = id, order = tf_depth(f))
gglasagna(data, f, label = id, order_by = dplyr::first) +
facet_wrap(~group, scales = "free")
# order of layers is by "order_by" within "order":
gglasagna(data, fb, label = id, order = group, order_by = dplyr::first)
ggplot_build method for tf_ggplot
Description
ggplot_build method for tf_ggplot
Usage
## S3 method for class 'tf_ggplot'
ggplot_build(plot, ...)
Arguments
plot |
A tf_ggplot object |
... |
Additional arguments passed through from |
Value
A built ggplot object (class ggplot_built).
Spaghetti plots for tf objects
Description
Plots a line for each entry of a tf-object.
geom_spaghetti draws a line through each function, and geom_meatballs
adds points for the evaluated grid values.
Usage
stat_tf(
mapping = NULL,
data = NULL,
geom = "spaghetti",
position = "identity",
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
arg = NULL,
...
)
geom_spaghetti(
mapping = NULL,
data = NULL,
position = "identity",
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
arg = NULL,
...
)
geom_meatballs(
mapping = NULL,
data = NULL,
position = "identity",
na.rm = TRUE,
show.legend = NA,
inherit.aes = TRUE,
arg = NULL,
spaghetti = TRUE,
...
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geometric object to use to display the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
na.rm |
remove NAs? defaults to |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
arg |
where to evaluate the functions in |
... |
Other arguments passed on to
|
spaghetti |
plot noodles along with meatballs? defaults to TRUE. |
Details
"Flipped" aesthetics are not implemented for these geoms.
Value
A ggplot2::layer() object for use in a ggplot.
y aesthetic
Mandatory. Used to designate a column of class tf to be visualized.
See Also
geom_cappelini() for glyph plots, gglasagna() for heatmaps.
Other tidyfun visualization:
autoplot.tf(),
ggcapellini,
gglasagna()
Other tidyfun visualization:
autoplot.tf(),
ggcapellini,
gglasagna()
Other tidyfun visualization:
autoplot.tf(),
ggcapellini,
gglasagna()
Other tidyfun visualization:
autoplot.tf(),
ggcapellini,
gglasagna()
Other tidyfun visualization:
autoplot.tf(),
ggcapellini,
gglasagna()
Other tidyfun visualization:
autoplot.tf(),
ggcapellini,
gglasagna()
Other tidyfun visualization:
autoplot.tf(),
ggcapellini,
gglasagna()
Examples
set.seed(1221)
data <- data.frame(col = sample(gl(5, 2)))
data$f <- tf_rgp(10)
data$fi <- tf_jiggle(data$f)
data$fb <- tfb(data$f)
library(ggplot2)
ggplot(data, aes(y = f, color = tf_depth(f))) +
geom_spaghetti()
ggplot(data, aes(y = fi, shape = col, color = col)) +
geom_meatballs()
ggplot(data, aes(y = fi)) +
geom_meatballs(spaghetti = FALSE) +
facet_wrap(~col)
Check if object is a tf_ggplot
Description
Check if object is a tf_ggplot
Usage
is_tf_ggplot(x)
Arguments
x |
Object to test |
Value
TRUE if x inherits from "tf_ggplot", FALSE otherwise.
Examples
p <- tf_ggplot(data.frame(x = 1))
is_tf_ggplot(p)
is_tf_ggplot(ggplot2::ggplot())
Parse aesthetic mappings to separate tf and regular aesthetics
Description
Parse aesthetic mappings to separate tf and regular aesthetics
Usage
parse_tf_aesthetics(mapping, data = NULL)
Arguments
mapping |
An aesthetic mapping created with |
data |
Data frame to evaluate expressions against. |
Value
A list with components tf_aes, scalar_tf_aes, and regular_aes.
Examples
parse_tf_aesthetics(ggplot2::aes(tf = f, color = group))
parse_tf_aesthetics(ggplot2::aes(x = x, y = y))
Print method for tf_ggplot
Description
Print method for tf_ggplot
Usage
## S3 method for class 'tf_ggplot'
print(x, ...)
Arguments
x |
A tf_ggplot object |
... |
Additional arguments |
Value
x, invisibly. Called for its side effect of printing the plot.
Objects exported from other packages
Description
These objects are imported from other packages. Follow the links below to see their documentation.
Evaluate tfs inside a data.frame
Description
Evaluate tfs inside a data.frame
Usage
## S3 method for class 'data.frame'
tf_evaluate(object, ..., arg)
Arguments
object |
a |
... |
optional: a selection of |
arg |
optional evaluation grid (vector or list of vectors).
Defaults to |
Details
The arg-argument of tf_evaluate.data.frame method can be a
list of arg-vectors or -lists used as the arg argument for the
tf::tf_evaluate()-method for the respective tf-columns in object.
... is not used for a tf-object, but a second unnamed argument to these
methods will be interpreted as arg.
Value
Replaces tf-columns with list columns of
smaller data.frames containing the functions' arguments (arg) and
evaluations (value) and returns the modified nested dataframe.
See Also
Other tidyfun data wrangling functions:
tf_gather(),
tf_nest(),
tf_spread(),
tf_unnest()
Examples
d <- dplyr::tibble(id = 1:3)
d$f <- tf_rgp(3, 11L)
str(tf_evaluate(d))
Gather all columns representing functional measurements into a tfd-object
Description
Similar in spirit to tidyr::gather(), but does NOT put the values in the
gathered columns into one very long "value"-column while labeling the different
original columns in a very long "key"-column – instead it creates a tfd-column
containing the functional measurements of the columns given in ....
Usage
tf_gather(
data,
...,
key = ".tfd",
arg = NULL,
domain = NULL,
evaluator = tf_approx_linear
)
Arguments
data |
a data frame – note that |
... |
A selection of columns to collect as a |
key |
the name of the created |
arg |
optional. Argument values for the functions. If not provided, will be guessed from the column names as well.
See also |
domain |
optional. Range of possible |
evaluator |
optional. A function accepting arguments x, arg, evaluations. See |
Value
a modified data.frame with a tfd column replacing the ....
See Also
Other tidyfun data wrangling functions:
tf_evaluate.data.frame(),
tf_nest(),
tf_spread(),
tf_unnest()
Examples
d <- tf_spread(growth[1:5, ]) |> dplyr::mutate(id = 1:dplyr::n())
dplyr::glimpse(d)
# tidyselect syntax for column selection:
tf_gather(d, starts_with("height"))
tf_gather(d, height_1:height_18)
tf_gather(d, -gender, -id)
# custom key name and arg values:
tf_gather(d, starts_with("height"), key = "height")
tf_gather(d, starts_with("height"), arg = seq(0, 1, length.out = 31))
Create a tf-aware ggplot
Description
tf_ggplot() creates a ggplot object that can handle tf (functional data) aesthetics.
It works similarly to ggplot() but automatically transforms tf objects into
long-format data suitable for standard ggplot2 geoms.
Usage
tf_ggplot(data = NULL, mapping = aes(), ..., arg = NULL, interpolate = TRUE)
Arguments
data |
Default dataset to use for plot. If not provided, must be supplied in each layer added to the plot. |
mapping |
Default list of aesthetic mappings to use for plot. Can include
tf-specific aesthetics like |
... |
Other arguments passed to ggplot2 functions. |
arg |
Optional. Evaluation grid for tf objects. A numeric vector of arg
values, or a single integer specifying the desired grid length (resolved to
an equidistant grid over the tf domain). If |
interpolate |
Logical. Should tf objects be interpolated to the evaluation grid? Defaults to TRUE. |
Details
tf_ggplot supports the following tf-specific aesthetics:
-
tf: Maps atfobject toyaesthetic (shorthand fortf_y) -
tf_x: Maps atfobject toxaesthetic -
tf_y: Maps atfobject toyaesthetic -
tf_ymin: Maps a tf object to ymin aesthetic (for ribbons) -
tf_ymax: Maps a tf object to ymax aesthetic (for ribbons)
When tf aesthetics are used, the data is automatically transformed:
tf objects are evaluated on a common grid
Each function becomes multiple rows (one per evaluation point)
Group identifiers are created to maintain function identity
Non-tf columns are replicated appropriately
Value
A tf_ggplot object that inherits from ggplot
Examples
# Basic usage
data <- data.frame(
id = 1:10,
group = sample(c("A", "B"), 10, replace = TRUE)
)
data$f <- tf_rgp(10)
# Method 1: tf aesthetic in constructor
tf_ggplot(data, ggplot2::aes(tf = f, color = group)) + ggplot2::geom_line()
# Method 2: tf aesthetic in geom (equivalent)
tf_ggplot(data) + ggplot2::geom_line(ggplot2::aes(tf = f, color = group))
# Confidence bands
tf_ggplot(data) +
ggplot2::geom_ribbon(
ggplot2::aes(tf_ymin = mean(f) - sd(f), tf_ymax = mean(f) + sd(f)),
alpha = 0.3
) +
ggplot2::geom_line(ggplot2::aes(tf = mean(f)))
Turn "long" tables into tidy data frames with tf-objects
Description
Similar in spirit to tidyr::nest(). This turns tables in "long" format,
where one column (.id) defines the unit of observation, one column (.arg)
defines the evaluation grids of the functional observations, and other columns (...)
define the values of the functions at those points into a (much shorter) table containing
tfd-objects. All other variables are checked for constancy over .id and
appended as well.
Usage
tf_nest(
data,
...,
.id = "id",
.arg = "arg",
domain = NULL,
evaluator = "tf_approx_linear"
)
Arguments
data |
a data frame |
... |
A selection of columns. If empty, all variables except the
|
.id |
the (bare or quoted) name of the column defining the different observations. Defaults to "id". |
.arg |
the (bare or quoted) name of the column defining the |
domain |
optional. Range of possible |
evaluator |
optional. A function accepting arguments x, arg, evaluations. See |
Details
domain and evaluator can be specified as lists or vectors
if you are nesting multiple functional data columns with different properties.
Because this interface captures evaluator names as text, supply the evaluator
as a string rather than a bare function name.
Value
a data frame with (at least) .id and tfd columns
See Also
tf::tfd() for details on domain and evaluator.
Other tidyfun data wrangling functions:
tf_evaluate.data.frame(),
tf_gather(),
tf_spread(),
tf_unnest()
Examples
d <- dplyr::tibble(id = rep(1:3, each = 5), arg = rep(1:5, 3), value = rnorm(15))
tf_nest(d, .id = id, .arg = arg)
Spread a tf-column into many columns representing the
function evaluations.
Description
Similar in spirit to tidyr::spread(), but does NOT shorten,
just widens the data frame – a tf-column is spread out into many columns
containing the functional measurements.
Usage
tf_spread(data, value, arg, sep = "_", interpolate = FALSE)
Arguments
data |
a data frame with at least one |
value |
the name of the |
arg |
(Semi-)optional. A vector of |
sep |
separating character used to create column names for the new columns,
defaults to |
interpolate |
|
Value
a wider dataframe with the tf-column spread out into many columns
each containing the functional measurements for one arg-value.
See Also
Other tidyfun data wrangling functions:
tf_evaluate.data.frame(),
tf_gather(),
tf_nest(),
tf_unnest()
Examples
d <- dplyr::tibble(g = 1:3)
d$f <- tf_rgp(3, 11L)
tf_spread(d, f)
tf_spread(d, -g)
tf_spread(d)
Turn (data frames with) tf-objects / list columns into "long" tables.
Description
Similar in spirit to tidyr::unnest(), the reverse of tf_nest().
The tf-method simply turns a single tfd or tfb vector into a "long" tibble::tibble().
Usage
tf_unnest(data, cols, arg, interpolate = TRUE, ...)
## S3 method for class 'tf'
tf_unnest(data, cols, arg, interpolate = TRUE, ...)
## S3 method for class 'data.frame'
tf_unnest(
data,
cols,
arg,
interpolate = TRUE,
keep_empty = FALSE,
ptype = NULL,
names_sep = "_",
names_repair = "check_unique",
...
)
Arguments
data |
a data.frame or a |
cols |
< When selecting multiple columns, values from the same row will be recycled to their common size. |
arg |
optional values for the |
interpolate |
return function values for |
... |
not used currently |
keep_empty |
By default, you get one row of output for each element
of the list that you are unchopping/unnesting. This means that if there's a
size-0 element (like |
ptype |
Optionally, a named list of column name-prototype pairs to
coerce |
names_sep |
If |
names_repair |
Used to check that output data frame has valid names. Must be one of the following options:
See |
Details
Caution: this uses slightly different defaults for names of unnested columns than
tidyr::unnest().For
data.frames, include an ID column with a unique row identifier before unnesting. Without it, arg-value pairs cannot be matched back to their original functions after unnesting.
Value
a "long" data frame with tf-columns expanded into arg, value-
columns.
See Also
Other tidyfun data wrangling functions:
tf_evaluate.data.frame(),
tf_gather(),
tf_nest(),
tf_spread()
Examples
d <- dplyr::tibble(id = 1:3)
d$f <- tf_rgp(3, 11L)
tf_unnest(d, f)
Translate a geom_spaghetti/geom_meatballs/geom_errorband layer for tf_ggplot
Description
Remaps old-style y/ymin/ymax tf aesthetics to tf/tf_ymin/tf_ymax and substitutes the appropriate standard ggplot2 geom.
Usage
translate_old_tf_layer(layer, e1)
Arguments
layer |
A LayerInstance using StatTf or StatErrorband |
e1 |
The tf_ggplot object (may be modified to clean up plot-level y) |
Value
A list with plot_obj (possibly modified e1) and layers (list of
new layers to add)
Format tidy functional data for tibbles
Description
Summarize tidy functional data for tibble
Usage
## S3 method for class 'tf'
type_sum(x, ...)
## S3 method for class 'tf'
obj_sum(x)
## S3 method for class 'tf'
pillar_shaft(x, ...)
Arguments
x |
object of class tf |
... |
ignored |