Miscellaneous Extensions to 'ggplot2'

Extensions to 'ggplot2' respecting the grammar of graphics paradigm. Statistics: locate and tag peaks and valleys; label plot with the equation of a fitted polynomial or other types of models; labels with P-value, R^2 or adjusted R^2 or information criteria for fitted models; label with ANOVA table for fitted models; label with summary for fitted models. Model fit classes for which suitable methods are provided by package 'broom' and 'broom.mixed' are supported. Scales and stats to build volcano and quadrant plots based on outcomes, fold changes, p-values and false discovery rates.

cranversion cranchecks

Package ‘ggpmisc’ (Miscellaneous Extensions to ‘ggplot2’) is a set of extensions to R package ‘ggplot2’ (>= 3.0.0) with emphasis on annotations and highlighting related to fitted models and data summaries. To complement these, the widely useful geom_table() and stat_fmt_tb() are defined as well as ggplot constructors for time series objects. The provided ggplot.ts() and ggplot.xts() use try_tibble() which is also exported and accepts objects of additional classes as input.

Statistics useful for highlighting and/or annotating individual data points in regions of plot panels with high/low densities of observations. These stats are designed to work well together with geom_text_repel() and geom_label_repel() from package ‘ggrepel’.

Note: Functions for the manipulation of layers in ggplot objects and statistics and geometries that echo their data input to the R console, earlier included in this package are now in package ‘gginnards’.



In the first example we plot a time series using the specialized version of ggplot() that converts the time series into a tibble and maps the x and y aesthetics automatically. We also highlight and label the peaks using stat_peaks.

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") +
  expand_limits(y = 8000)

In the second example we add the equation for a fitted polynomial plus the adjusted coefficient of determination to a plot showing the observations plus the fitted curve and confidence band. We use stat_poly_eq().

formula <- y ~ x + I(x^2)
ggplot(cars, aes(speed, dist)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(aes(label =  paste(..eq.label.., ..adj.rr.label.., sep = "~~~~")),
               formula = formula, parse = TRUE)

The same figure as in the second example but this time annotated with the ANOVA table for the model fit. We use stat_fit_tb() which can be used to add ANOVA or summary tables.

formula <- y ~ x + I(x^2)
ggplot(cars, aes(speed, dist)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_fit_tb(method = "lm",
              method.args = list(formula = formula),
              tb.type = "fit.anova",
              tb.vars = c(Effect = "term", 
                          "M.S." = "meansq", 
                          "italic(F)" = "statistic", 
                          "italic(P)" = "p.value"),
               label.y.npc = "top", label.x.npc = "left",
              parse = TRUE)
#> Warning in seq.default(along = x): partial argument match of 'along' to
#> 'along.with'


Installation of the most recent stable version from CRAN:


Installation of the current unstable version from Bitbucket:

# install.packages("devtools")


HTML documentation is available at (http://docs.r4photobiology.info/ggpmisc/), including a User Guide.

News on updates to the different packages of the ‘r4photobiology’ suite are regularly posted at (https://www.r4photobiology.info/).


Please report bugs and request new features at (https://bitbucket.org/aphalo/ggpmisc/issues). Pull requests are welcome at (https://bitbucket.org/aphalo/ggpmisc).

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.


If you use this package to produce scientific or commercial publications, please cite according to:

#> To cite ggpmisc in publications, please use:
#>   Pedro J. Aphalo. (2016) Learn R ...as you learnt your mother
#>   tongue. Leanpub, Helsinki.
#> A BibTeX entry for LaTeX users is
#>   @Book{,
#>     author = {Pedro J. Aphalo},
#>     title = {Learn R ...as you learnt your mother tongue},
#>     publisher = {Leanpub},
#>     year = {2016},
#>     url = {https://leanpub.com/learnr},
#>   }


© 2016-2018 Pedro J. Aphalo ([email protected]). Released under the GPL, version 2 or greater. This software carries no warranty of any kind.


ggpmisc 0.3.0 (2018-07-16)

Version requiring 'ggplot2' (>= 3.0.0), now in CRAN. Low level manipulation and debug methods and functions moved to new package 'gginnards' available through CRAN.

  • Remove debug stats and geoms -> 'gginnards'.
  • Remove layer manipulation functions -> 'gginnards'.
  • Add support for "weight" aesthetic in stat_poly_eq() (fixing bug reported by S.Al-Khalidi).
  • Add support for column selection and renaming to stat_fit_tb().
  • Add new statistic stat_fmt_tb() for formatting of tibbles for addition to plots as tables.
  • Rename stat_quadrat_count() into stat_quadrant_count() (miss-spelling).
  • Revise vignette.


Non-CRAN version with additional functionality, but requiring the development version of 'ggplot2'.

  • Track code breaking change in 'ggplot2' commit #2620 (2018-05-17).


Non-CRAN version with additional functionality, but requiring the development version of 'ggplot2' >= (>= commit of 2017-02-09) from Github. Visit

  • geom_table(), a geom for adding a layer containing one or more tables to a plot panel.
  • stat_fit_tb() a stat that computes a tidy tabular version of the summary or ANOVA table from a model fit.

ggpmisc 0.2.17

CRAN version

  • Add stat_quadrat_count() a stat that computes the number of observations in each quadrat of a plot panel ignoring grouping.

  • Fix bugs, one of which is code breaking: the names of returned parameter estimates have changed in stat_fit_tidy() now pasting "_estimate" to avoid name clashes with mapped variables.

ggpmisc 0.2.16

  • Revise stat_fit_tidy() so that it returns p-values for parameters, in addition to estimates and their standard errors.
  • BUG FIX: Revise geom_debug() adding missing default arguments.
  • Add functions for manipulation of layers in ggplot objects: delete_layers(), append_layers(), move_layers(), shift_layers(), which_layers(), extract_layers(), num_layers(), top_layer() and bottom_layer().

ggpmisc 0.2.15

Add stat_fit_tidy() implemented using broom::tidy(). Makes it possible to add the fitted equation for any fitted model supported by package 'broom', as long as the user supplies within aes() the code to build a label string. Update user guide.

ggpmisc 0.2.14

Fix bug in stat_poly_equation() eq.x.rhs argument ignored when using expressions.

ggpmisc 0.2.13

Fix bugs in try_tibble() and try_data_frame() which made them fail silently with some objects of class "ts" in the case of numeric (decimal date) index for time. In addition lack of special handling for classes "yearmon" and "yearqrt" from package 'zoo', lead to erroneous date shifts by a few days. Add methods ggplot.ts() and ggplot.xts().

ggpmisc 0.2.12

Change default value for parameter label.fill in stat_dens2d_labels() from NA to "". Improve documentation using current 'ggrepel' version, which implements changes that make stat_dens2d_labels() useful.

ggpmisc 0.2.11

Add stat_dens2d_labels(), a statistic that resets label values to NA by default, or any character string supplied as argument, in regions of a panel with high density of observations. Add stat_den2d_filter(), a statistic that filters-out/filters-in observations in regions of a panel with high density of observations. These two statistics are useful for labelling or highlighting observations in regions of a panel with low density. Both stats use a compute_panel function. Add stat_den2d_filter_g(), a statistic that filters-out/filters-in observations in regions of a group with high density of observations. This statistics is useful for highlighting observations. It uses a compute_group function. They use internally MASS:kde2d to estimate densities and default values for parameters are adjusted dynamically based on the number of observations.

ggpmisc 0.2.10

Add user-requested feature: allow user to specify number 'digits' used in formatting numbers in labels in stat_poly_eq(). Update try_data_frame() to return an object of class "tibble" and add try_tibble() as synonym. Update documentation and start using package 'staticdocs' to build a documentation web site.

ggpmisc 0.2.9

Add support for tikz in stat_poly_eq(). Fix bug in stat_poly_eq(). Fix bug in geom_debug(). Fix bug in stat_fit_augment().

ggpmisc 0.2.8

Enhance stat_poly_eq() so that 1) position of labels according to npc (relative positions using normalized coordinates), as well as by named positions "top", "bottom", "right", "left" and "center" is now implemented; 2) when grouping is present, suitable vjust values are computed to automatically position the labels for the different groups without overlap. Default label positions are now relative to the range of each panel's $x$ and $y$ scales, eliminating in most cases the need to manually tweak label positions.

Add stat_fit_glance() uses package 'broom' for maximum flexibility in model function choice when wanting to add labels based on information from a model fit, at the expense of very frequently having to explicitly set aesthetics, and always having to add code to do the formatting of the values to be used in labels. Label position is as described above for stat_poly_eq().

Add stat_fit_deviations() for highlighting residuals in plots of fitted models. This statistic currently supports only lm() fits. By default geom "segment" is used to highlight the deviations of the observations from a fitted model.

Add stat_fit_residuals() for plotting residuals from a fitted model on their own in plots matching plots of lm fits plotted with stat_smooth() even with grouping or facets. This statistic currently supports only lm() fits. By default geom "point" is used to plot the residual from a fitted model.

Add preliminary version of stat_fit_augment(), which uses package 'broom' for maximum flexibility in model function choice, to augment the data with additional columns of values derived from a model fit.

ggpmisc 0.2.7

Add support for AIC and BIC labels to stat_poly_eq(). Add pretty-printing of parameter values expressed in engineering notation in stat_poly_eq(). Add support for user-supplied label coordinates in stat_poly_eq(). Improve stat_debug_panel() and stat_debug_group() so that they can optionally print to the console a summary of the data received as input. Add geom_debug(), a geom that summarizes its data input to the console, and produces no visible graphical output.

ggpmisc 0.2.6

Add support for user-supplied lhs and for user-supplied rhs-variable name in the equation label in stat_poly_eq().

ggpmisc 0.2.5

Remove one example to remove a package dependency.

ggpmisc 0.2.4

Improve handling of time zones in try_data_frame(). Revise documentation and vignette.

ggpmisc 0.2.3

stat_poly_eq() changed to include the lhs (left hand side) of the equation by default.

ggpmisc 0.2.2

Add function try_data_frame() to convert R objects including time series objects of all classes accepted by try.xts() into data frames suitable for plotting with ggplot().

Update stat_peaks() and stat_valleys() to work correctly when the x aesthetic uses a Date or Datetime continuous scale such as ggplot() sets automatically for POSIXct variables mapped to the x aesthetic.

ggpmisc 0.2.1

Rename stat_debug() as stat_debug_group() and add stat_debug_panel(). Add stat_peaks() and stat_valleys() (these are simpler versions of ggspectra::stat_peaks() and ggspectra::stat_valleys() for use with any numerical data (rather than light spectra).

ggpmisc 0.1.0

First version. Add stat_poly_eq() Add stat_debug()

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.4.3 by Pedro J. Aphalo, 2 months ago

https://docs.r4photobiology.info/ggpmisc/, https://github.com/aphalo/ggpmisc

Report a bug at https://github.com/aphalo/ggpmisc/issues

Browse source code at https://github.com/cran/ggpmisc

Authors: Pedro J. Aphalo [aut, cre] , Kamil Slowikowski [ctb] , Samer Mouksassi [ctb]

Documentation:   PDF Manual  

GPL (>= 2) license

Imports grid, stats, ggplot2, scales, rlang, generics, MASS, polynom, quantreg, lmodel2, splus2R, tibble, plyr, dplyr, lubridate

Depends on ggpp

Suggests knitr, rmarkdown, ggrepel, broom, broom.mixed, nlme, gginnards

Imported by forestmangr, ggquickeda, neutralitytestr, visa.

Suggested by MOSS.

See at CRAN