Fitted-model labels with ‘ggpmisc’ and ‘plotly’

Interactive ggplots with model equations, R2, F, P, etc.

Plotting examples
Author

Pedro J. Aphalo

Published

2023-06-25

Modified

2023-06-26

Abstract

Example R code for interactive plots based on package ggplot2 using geometries defined in package ggpp and statistics from package ggpmisc together with package plotly. This page is a draft and currently contains a single plot example.

Keywords

ggplot2 pkg, ggpp pkg, ggpmisc pkg, plotly pkg, plot annotations, model equations

Warning

To get the output of ‘plotly’ included by Quarto in the HTML output, currently the pre-release of Quarto 1.4.x is required.

It is possible to combine ‘ggpmisc’ and ‘plotly’ in the same plot but only by using the most basic formatting of the equation. I give for the time being a single example.

Tip

In this page code chunks are “folded” so as to decrease the clutter when searching for examples. Above each plot you will find a small triangle followed by “Code”. Clicking on the triangle “unfolds” the code chunk making visible the R code used to produce the plot. Except for the loading of packages shown in section Preliminaries code examples are in most cases self contained. When they are not, this is indicated by a comment.

For simplicity, whenever possible I use base R functions instead of contributed R packages. For those packages used only in specific examples I use colon notation to indicate the ‘package’.

All “words” defined in base R or in extension packages are linked to the corresponding HTML-rendered help pages.

The code in the chunks can be copied by clicking on the top right corner, where an icon appears when the mouse cursor hovers over the code listing.

Important

One needs to always check that annotations do not occlude anything significant, such as observations in the base plot. This needs special care when using annotations together with batch plotting. Either ensure that the scale limits of the base plot are expanded to avoid overlap or that the layer with the equations is the lowest one, i.e., added to the plot first.

1 Preliminaries

We first load the packages we will use.

When package ‘ggpmisc’ is loaded and attached, packages ‘ggpp’ and ‘ggplot2’ are also attached. The only function from ‘ggplot2’ that is redefined by ‘ggpp’ is annotate(), which remains backwards compatible with ‘ggplot2’.

2 Using ‘plotly’

Plotly can convert a regular ggplot into an interactive plot that can be embedded in HTML output. The advantage is that one uses the usual layer functions, and the disadvantage is that possible control of output is rather limited. It is implemented as a parsing engine that recognizes only some ‘ggplot2’ layer functions. As can be seen in the example below this approach makes rendering math as R expressions not possible and text labels marked to be parsed into expressions are displayed as unparsed text.

Several of the statistics from ‘ggpmisc’ can still be used as they also support the generation of text labels as plain text but the resulting annotations are not as nice as when using R graphic devices.

Code
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)

Even though this example works without problems at the R console, I was not able to get it to work under Quarto 1.3.x. Recently, I ungraded the building of the web site to the pre-release of Quarto 1.4.x and now ‘plotly’ works, as can be seen below!

Code
my.formula <- y ~ x
p <- ggplot(data = df, aes(x = x, y = y)) +
  geom_smooth(method = "lm", se=FALSE, color="black", formula = my.formula) +
  stat_poly_eq(geom = "text",
               output.type = "text",
               formula = my.formula,
               mapping = use_label(c("eq", "R2"), sep = ",  "),
               label.x = 20) +
  geom_point()

ggplotly(p)

Works with Quarto 1.4.x!

2.1 Alternatives

Tip

I have not yet tested ‘ggigraph’ together with ‘ggpmisc’, but I plan to, as it seems to allow finer grained control of the interaction. On the other hand, it could be incompatible.