Extensions to 'ggplot2' respecting the grammar of graphics paradigm. Specialization of method ggplot(): accept and convert on the fly time series data. Geom: "table", adds tables to plots. Statistics: locate and tag peaks and valleys in 2D plots; count observations in different quadrants of a plot; select observations based on 2D density; label with the equation of a polynomial fitted with lm() 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' are supported.
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’.
library(ggplot2)library(ggpmisc)
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","df","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:
install.packages("ggspectra")
Installation of the current unstable version from Bitbucket:
# install.packages("devtools")devtools::install_bitbucket("aphalo/ggspectra")
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:
citation("ggpmisc")#>#> 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.
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.
Non-CRAN version with additional functionality, but requiring the development version of 'ggplot2'.
Non-CRAN version with additional functionality, but requiring the development version of 'ggplot2' >= 2.2.1.9000 (>= commit of 2017-02-09) from Github. Visit
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.
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.
Fix bug in stat_poly_equation() eq.x.rhs argument ignored when using expressions.
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().
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.
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.
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.
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().
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.
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.
Add support for user-supplied lhs and for user-supplied rhs-variable name in the equation label in stat_poly_eq().
Remove one example to remove a package dependency.
Improve handling of time zones in try_data_frame(). Revise documentation and vignette.
stat_poly_eq() changed to include the lhs (left hand side) of the equation by default.
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.
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).
First version. Add stat_poly_eq() Add stat_debug()