Learn R Programming

⚠️There's a newer version (0.6.0) of this package.Take me there.

ggpmisc

Purpose

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. Data summaries shown as text, tables or equations are implemented. Package ‘ggpmisc’ continues to give access to extensions moved to package ‘ggpp’. New geoms support insets in ggplots. The grammar of graphics is extended to support native plot coordinates (npc) so that annotations can be easily positioned using special geometries and scales. New position functions facilitate the labeling of observations by nudging data labels away or towards curves or a focal virtual center.

Extended Grammar of Graphics

Please, see the documentation of package ‘ggpp’.

Aesthetics and scales

Scales scale_x_logFC() and scale_y_logFC() are suitable for plotting of log fold change data. Scales scale_x_Pvalue(), scale_y_Pvalue(), scale_x_FDR() and scale_y_FDR() are suitable for plotting p-values and adjusted p-values or false discovery rate (FDR). Default arguments are suitable for volcano and quadrant plots as used for transcriptomics, metabolomics and similar data.

Scales scale_colour_outcome(), scale_fill_outcome() and scale_shape_outcome() and functions outome2factor(), threshold2factor(), xy_outcomes2factor() and xy_thresholds2factor() used together make it easy to map ternary numeric outputs and logical binary outcomes to color, fill and shape aesthetics. Default arguments are suitable for volcano, quadrant and other plots as used for genomics, metabolomics and similar data.

Statistics

Statistics stat_peaks() and stat_valleys() can be used to highlight and/or label maxima and minima in a plot.

Statistics that help with reporting the results of model fits are stat_poly_eq(), stat_fit_residuals(), stat_fit_deviations(), stat_fit_glance(), stat_fit_augment(), stat_fit_tidy() and stat_fit_tb().

MIGRATED

Several extensions formerly included in package ‘ggpmisc’ before version 0.4.0 were migrated to package ‘ggpp’. They are still available when ‘ggpmisc’ is loaded, but the documentation now resides in the new package ‘ggpp’.

Functions for the manipulation of layers in ggplot objects, together with statistics and geometries useful for debugging extensions to package ‘ggplot2’, included in package ‘ggpmisc’ before version 0.3.0 are now in package ‘gginnards’.

Examples

library(ggpmisc)
library(ggrepel)
library(broom)

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") +
  stat_peaks(geom = "rug", colour = "red", sides = "b") +
  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, deviations and confidence band. We use stat_poly_eq().

formula <- y ~ x + I(x^2)
ggplot(cars, aes(speed, dist)) +
  geom_point() +
  stat_fit_deviations(formula = formula, colour = "red") +
  stat_poly_line(formula = formula) +
  stat_poly_eq(aes(label =  paste(stat(eq.label), stat(adj.rr.label), sep = "*\", \"*")),
               formula = formula)

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"),
              tb.params = c(x = 1, "x^2" = 2),
              label.y.npc = "top", label.x.npc = "left",
              size = 2.5,
              parse = TRUE)
#> Dropping params/terms (rows) from table!

The same figure as in the second example but this time using quantile regression, median in this example.

formula <- y ~ x + I(x^2)
ggplot(cars, aes(speed, dist)) +
  geom_point() +
  stat_quant_line(formula = formula, quantiles = 0.5) +
  stat_quant_eq(aes(label = paste(stat(grp.label), stat(eq.label), sep = "*\": \"*")),
               formula = formula, quantiles = 0.5)

Band highlighting the region between both quartile regressions and a line for the median regression.

formula <- y ~ x + I(x^2)
ggplot(cars, aes(speed, dist)) +
  geom_point() +
  stat_quant_band(formula = formula)

A quadrant plot with counts and labels, using geom_text_repel() from package ‘ggrepel’.

ggplot(quadrant_example.df, aes(logFC.x, logFC.y)) +
  geom_point(alpha = 0.3) +
  geom_quadrant_lines() +
  stat_quadrant_counts() +
  stat_dens2d_filter(color = "red", keep.fraction = 0.02) +
  stat_dens2d_labels(aes(label = gene), keep.fraction = 0.02, 
                     geom = "text_repel", size = 2, colour = "red") +
  scale_x_logFC(name = "Transcript abundance after A%unit") +
  scale_y_logFC(name = "Transcript abundance after B%unit")

Installation

Installation of the most recent stable version from CRAN:

install.packages("ggpmisc")

Installation of the current unstable version from GitHub:

# install.packages("devtools")
devtools::install_github("aphalo/ggpmisc")

Documentation

HTML documentation for the package, including help pages and the User Guide, is available at (https://docs.r4photobiology.info/ggpmisc/).

News about updates are regularly posted at (https://www.r4photobiology.info/).

Chapter 7 in Aphalo (2020) explains both basic concepts of the gramamr of graphics as implemented in ‘ggplot2’ as well as extensions to this grammar including several of those made available by packages ‘ggpp’ and ‘ggpmisc’.

Contributing

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

Citation

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

citation("ggpmisc")
#> 
#> To cite package 'ggpmisc' in publications use:
#> 
#>   Pedro J. Aphalo (2021). ggpmisc: Miscellaneous Extensions to
#>   'ggplot2'. https://docs.r4photobiology.info/ggpmisc/,
#>   https://github.com/aphalo/ggpmisc.
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Manual{,
#>     title = {ggpmisc: Miscellaneous Extensions to 'ggplot2'},
#>     author = {Pedro J. Aphalo},
#>     year = {2021},
#>     note = {https://docs.r4photobiology.info/ggpmisc/,
#> https://github.com/aphalo/ggpmisc},
#>   }

References

Aphalo, Pedro J. (2020) Learn R: As a Language. The R Series. Boca Raton and London: Chapman and Hall/CRC Press. ISBN: 978-0-367-18253-3. 350 pp.

License

© 2016-2021 Pedro J. Aphalo (pedro.aphalo@helsinki.fi). Released under the GPL, version 2 or greater. This software carries no warranty of any kind.

Copy Link

Version

Install

install.packages('ggpmisc')

Monthly Downloads

16,315

Version

0.4.3

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Pedro Aphalo

Last Published

September 3rd, 2021

Functions in ggpmisc (0.4.3)

outcome2factor

Convert numeric ternary outcomes into a factor
coef.lmodel2

Extract Model Coefficients
find_peaks

Find local maxima or global maximum (peaks)
ggpmisc-package

ggpmisc: Miscellaneous Extensions to 'ggplot2'
Moved

Moved to package 'gginnards'
FC_name

Fold change- axis labels
FC_format

Formatter for fold change tick labels
scale_x_logFC

Position scales for log fold change data
scale_shape_outcome

Shape scale for ternary outcomes
quadrant_example.df

Example gene expression data
scale_colour_outcome

Colour and fill scales for ternary outcomes
stat_fit_augment

Augment data with fitted values and statistics
stat_fit_glance

One row summary data frame for a fitted model
stat_fit_deviations

Residuals from model fit as segments
scale_y_Pvalue

Covenience scale for P-values
stat_fit_residuals

Residuals from a model fit
reverselog_trans

Reverse log transformation
stat_quant_band

Compute predicted line from quantile regression fit
stat_poly_line

Predicted line from model fit
stat_ma_eq

Equation, p-value, R^2 of major axis regression
stat_ma_line

Predicted line from major axis linear fit
stat_quant_line

Predicted line from quantile regression fit
swap_xy

Swap x and y in a formula
symmetric_limits

Expand a range to make it symmetric
stat_quant_eq

Equation, p-value, R^2, AIC or BIC from quantile regression
stat_peaks

Local maxima (peaks) or minima (valleys)
volcano_example.df

Example gene expression data
xy_outcomes2factor

Convert two numeric ternary outcomes into a factor
stat_poly_eq

Equation, p-value, R^2, AIC or BIC of fitted polynomial
stat_fit_tidy

One row data frame with fitted parameter estimates
stat_fit_tb

Model-fit summary or ANOVA
predict.lmodel2

Model Predictions
fit_tb_compute_panel_fun

Stat* Objects
confint.lmodel2

Confidence Intervals for Model Parameters