| Title: | Tidy Functional Data Wrangling and Visualization |
|---|---|
| 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. |
| Authors: | Fabian Scheipl [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-8172-3603>), Jeff Goldsmith [aut], Julia Wrobel [aut] (ORCID: <https://orcid.org/0000-0001-6783-1421>), Maximilian Mücke [ctb] (ORCID: <https://orcid.org/0009-0000-9432-9795>) |
| Maintainer: | Fabian Scheipl <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.2 |
| Built: | 2026-05-24 07:14:56 UTC |
| Source: | https://github.com/tidyfun/tidyfun |
Add layers to tf_ggplot objects
## S3 method for class 'tf_ggplot' e1 + e2## S3 method for class 'tf_ggplot' e1 + e2
e1 |
A tf_ggplot object |
e2 |
A ggplot2 layer, scale, theme, etc. |
A modified tf_ggplot object.
tf objectsConvenient 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().
## S3 method for class 'tf' autoplot(object, ...) ## S3 method for class 'tf' autolayer(object, ...)## S3 method for class 'tf' autoplot(object, ...) ## S3 method for class 'tf' autolayer(object, ...)
object |
a |
... |
passed to |
A tf_ggplot() object for autoplot(), a ggplot2::layer() object for autolayer().
Other tidyfun visualization:
ggcapellini,
gglasagna(),
ggspaghetti
library(ggplot2) f <- tf_rgp(5) autoplot(f) ggplot() + autolayer(f) tf_ggplot() + autolayer(f)library(ggplot2) f <- tf_rgp(5) autoplot(f) ggplot() + autolayer(f) tf_ggplot() + autolayer(f)
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.
chf_dfchf_df
A tibble with 329 rows and 8 columns:
(numeric) Subject identifier.
(character) "Male" or "Female".
(numeric) Age in years.
(numeric) Body mass index.
(numeric) Week of cardiac event.
(character) Type of cardiac event.
(ordered factor) Day of the week (Mon < Tue < ... < Sun).
(tfd_reg) Minute-by-minute activity counts over
a 24-hour period (arg domain 1–1440).
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.
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
dti_df for another example dataset,
vignette("x04_Visualization", package = "tidyfun") for usage examples.
Other tidyfun datasets:
dti_df
chf_df library(ggplot2) chf_df |> dplyr::filter(id %in% 1:5) |> gglasagna(activity, order_by = mean)chf_df library(ggplot2) chf_df |> dplyr::filter(id %in% 1:5) |> gglasagna(activity, order_by = mean)
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.
dti_dfdti_df
A tibble with 382 rows and 6 columns:
(numeric) Subject identifier.
(integer) Visit number.
(factor) "male" or "female".
(factor) "control" or "MS" (multiple sclerosis status).
(tfd_irreg) FA tract profiles for the corpus callosum
(up to 93 evaluation points, domain 0–1).
(tfd_irreg) FA tract profiles for the right corticospinal
tract (up to 55 evaluation points, domain 0–1).
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."
Data are from the Johns Hopkins University and the Kennedy-Krieger Institute. Also available in a different format as refund::DTI.
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
chf_df for another example dataset,
vignette("x04_Visualization", package = "tidyfun") for usage examples.
Other tidyfun datasets:
chf_df
dti_df library(ggplot2) dti_df |> dplyr::filter(visit == 1) |> tf_ggplot(aes(tf = cca, color = case)) + geom_line(alpha = 0.3)dti_df library(ggplot2) dti_df |> dplyr::filter(visit == 1) |> tf_ggplot(aes(tf = cca, color = case)) + geom_line(alpha = 0.3)
tf objectsPlots 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.
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 )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 )
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 |
A ggplot2::layer() object for use in a ggplot.
tf aestheticMandatory. Used to designate a column of class tf to be visualized as glyphs.
Other tidyfun visualization:
autoplot.tf(),
gglasagna(),
ggspaghetti
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 )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 )
tf objects as boundsPlots a shaded region between tf-objects ymax and ymin.
This is primarily intended to help with plotting confidence bands
although other purposes are possible.
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, ... )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, ... )
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
|
A ggplot2::layer() object for use in a ggplot.
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)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)
tf objectsDraw functional boxplots based on functional depth rankings.
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 )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 )
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 |
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.
A ggplot2 layer.
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")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")
tfs using ggplot2
Lasagna plots show one color bar for each function.
gglasagna( data, tf, order = NULL, label = NULL, arg = NULL, order_by = NULL, order_ticks = TRUE )gglasagna( data, tf, order = NULL, label = NULL, arg = NULL, order_by = NULL, order_ticks = TRUE )
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
|
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()).
a ggplot2 object
Other tidyfun visualization:
autoplot.tf(),
ggcapellini,
ggspaghetti
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)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
## S3 method for class 'tf_ggplot' ggplot_build(plot, ...)## S3 method for class 'tf_ggplot' ggplot_build(plot, ...)
plot |
A tf_ggplot object |
... |
Additional arguments passed through from |
A built ggplot object (class ggplot_built).
tf objectsPlots 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.
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, ... )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, ... )
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. |
"Flipped" aesthetics are not implemented for these geoms.
A ggplot2::layer() object for use in a ggplot.
y aestheticMandatory. Used to designate a column of class tf to be visualized.
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()
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)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
is_tf_ggplot(x)is_tf_ggplot(x)
x |
Object to test |
TRUE if x inherits from "tf_ggplot", FALSE otherwise.
p <- tf_ggplot(data.frame(x = 1)) is_tf_ggplot(p) is_tf_ggplot(ggplot2::ggplot())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
parse_tf_aesthetics(mapping, data = NULL)parse_tf_aesthetics(mapping, data = NULL)
mapping |
An aesthetic mapping created with |
data |
Data frame to evaluate expressions against. |
A list with components tf_aes, scalar_tf_aes, and regular_aes.
parse_tf_aesthetics(ggplot2::aes(tf = f, color = group)) parse_tf_aesthetics(ggplot2::aes(x = x, y = y))parse_tf_aesthetics(ggplot2::aes(tf = f, color = group)) parse_tf_aesthetics(ggplot2::aes(x = x, y = y))
Print method for tf_ggplot
## S3 method for class 'tf_ggplot' print(x, ...)## S3 method for class 'tf_ggplot' print(x, ...)
x |
A tf_ggplot object |
... |
Additional arguments |
x, invisibly. Called for its side effect of printing the plot.
tfs inside a data.frame
Evaluate tfs inside a data.frame
## S3 method for class 'data.frame' tf_evaluate(object, ..., arg)## S3 method for class 'data.frame' tf_evaluate(object, ..., arg)
object |
a |
... |
optional: a selection of |
arg |
optional evaluation grid (vector or list of vectors).
Defaults to |
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.
Replaces tf-columns with list columns of
smaller data.frames containing the functions' arguments (arg) and
evaluations (value) and returns the modified nested dataframe.
Other tidyfun data wrangling functions:
tf_gather(),
tf_nest(),
tf_spread(),
tf_unnest()
d <- dplyr::tibble(id = 1:3) d$f <- tf_rgp(3, 11L) str(tf_evaluate(d))d <- dplyr::tibble(id = 1:3) d$f <- tf_rgp(3, 11L) str(tf_evaluate(d))
tfd-objectSimilar 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 ....
tf_gather( data, ..., key = ".tfd", arg = NULL, domain = NULL, evaluator = tf_approx_linear )tf_gather( data, ..., key = ".tfd", arg = NULL, domain = NULL, evaluator = tf_approx_linear )
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 |
a modified data.frame with a tfd column replacing the ....
Other tidyfun data wrangling functions:
tf_evaluate.data.frame(),
tf_nest(),
tf_spread(),
tf_unnest()
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))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))
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.
tf_ggplot(data = NULL, mapping = aes(), ..., arg = NULL, interpolate = TRUE)tf_ggplot(data = NULL, mapping = aes(), ..., arg = NULL, interpolate = TRUE)
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. |
tf_ggplot supports the following tf-specific aesthetics:
tf: Maps a tf object to y aesthetic (shorthand for tf_y)
tf_x: Maps a tf object to x aesthetic
tf_y: Maps a tf object to y aesthetic
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
A tf_ggplot object that inherits from ggplot
# 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)))# 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)))
tf-objectsSimilar 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.
tf_nest( data, ..., .id = "id", .arg = "arg", domain = NULL, evaluator = "tf_approx_linear" )tf_nest( data, ..., .id = "id", .arg = "arg", domain = NULL, evaluator = "tf_approx_linear" )
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 |
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.
a data frame with (at least) .id and tfd columns
tf::tfd() for details on domain and evaluator.
Other tidyfun data wrangling functions:
tf_evaluate.data.frame(),
tf_gather(),
tf_spread(),
tf_unnest()
d <- dplyr::tibble(id = rep(1:3, each = 5), arg = rep(1:5, 3), value = rnorm(15)) tf_nest(d, .id = id, .arg = arg)d <- dplyr::tibble(id = rep(1:3, each = 5), arg = rep(1:5, 3), value = rnorm(15)) tf_nest(d, .id = id, .arg = arg)
tf-column into many columns representing the
function evaluations.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.
tf_spread(data, value, arg, sep = "_", interpolate = FALSE)tf_spread(data, value, arg, sep = "_", interpolate = FALSE)
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 |
|
a wider dataframe with the tf-column spread out into many columns
each containing the functional measurements for one arg-value.
Other tidyfun data wrangling functions:
tf_evaluate.data.frame(),
tf_gather(),
tf_nest(),
tf_unnest()
d <- dplyr::tibble(g = 1:3) d$f <- tf_rgp(3, 11L) tf_spread(d, f) tf_spread(d, -g) tf_spread(d)d <- dplyr::tibble(g = 1:3) d$f <- tf_rgp(3, 11L) tf_spread(d, f) tf_spread(d, -g) tf_spread(d)
tf-objects / list columns into "long" tables.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().
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", ... )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", ... )
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 |
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.
a "long" data frame with tf-columns expanded into arg, value-
columns.
Other tidyfun data wrangling functions:
tf_evaluate.data.frame(),
tf_gather(),
tf_nest(),
tf_spread()
d <- dplyr::tibble(id = 1:3) d$f <- tf_rgp(3, 11L) tf_unnest(d, f)d <- dplyr::tibble(id = 1:3) d$f <- tf_rgp(3, 11L) tf_unnest(d, f)
Summarize tidy functional data for tibble
## S3 method for class 'tf' type_sum(x, ...) ## S3 method for class 'tf' obj_sum(x) ## S3 method for class 'tf' pillar_shaft(x, ...)## S3 method for class 'tf' type_sum(x, ...) ## S3 method for class 'tf' obj_sum(x) ## S3 method for class 'tf' pillar_shaft(x, ...)
x |
object of class tf |
... |
ignored |