Learn R Programming

ggpmisc (version 0.2.17)

ggpmisc-package: ggpmisc: Miscellaneous Extensions to 'ggplot2'

Description

Extensions to 'ggplot2' respecting the grammar of graphics paradigm. Provides new statistics to locate and tag peaks and valleys in 2D plots, a statistics to add a label with the equation of a polynomial fitted with lm(), or R^2 or adjusted R^2 or information criteria for any model fitted with function lm(). Additional statistics give access to functions in package 'broom'. Provides a function for flexibly converting time series to data frames suitable for plotting with ggplot(). In addition provides statistics and ggplot geometries useful for diagnosing what data are passed to compute_group() and compute_panel() functions and to geometries.

Arguments

Acknowledgements

We thank Kamil Slowikowski not only for contributing ideas and code examples to this package but also for adding new features to his package 'ggrepel' that allow new use cases for stat_dens2d_labels from this package.

Details

The new facilities for cleanly defining new stats and geoms added to 'ggplot2' in version 2.0.0 have made this package easy to code. However, this means that this package requires version 2.0.0 or later of ggplot2.

Extensions provided:

  • Function for conversion of time series data into tibbles that can be plotted with ggplot.

  • ggplot() method for time series data.

  • Stats for locating and tagging "peaks" and "valleys" (local or global maxima and minima).

  • Stat for generating labels from a lm() model fit, including formatted equation. By default labels are expressions but tikz device is supported optionally with LaTeX formatted labels.

  • Stats for extracting information from a any model fit supported by package 'broom'.

  • Stats for filtering-out/filtering-in observations in regions of a panel or group where the density of observations is high.

  • Geom for annotating plots with tables.

  • "Debug" stats and a "debug" geom that print to the console a summary of their data input.

The stats for peaks and valleys are coded so as to work correctly both with numeric and POSIXct variables mapped to the x aesthetic. Special handling was needed as text labels are generated from the data.

References

Package suite 'r4photobiology' web site at https://www.r4photobiology.info/ Package 'ggplot2' web site at http://ggplot2.org/ Package 'ggplot2' documentation at http://docs.ggplot2.org/ Package 'ggplot2' source code at https://github.com/hadley/ggplot2

See Also

Useful links:

Examples

Run this code
# NOT RUN {
library(ggplot2)

ggplot(lynx, as.numeric = FALSE) + geom_line() +
stat_peaks(colour = "red") +
  stat_peaks(geom = "text", colour = "red", angle = 66,
             hjust = -0.1, x.label.fmt = "%Y") +
  ylim(NA, 8000)

formula <- y ~ poly(x, 2, raw = TRUE)
ggplot(cars, aes(speed, dist)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(aes(label = ..eq.label..), formula = formula,
               parse = TRUE)

# }

Run the code above in your browser using DataLab