At the moment support for math typesetting in interactive plots is very limited in R. ‘plotly’, at least in older versions, did support rendering of \(\LaTeX\)-encoded labels with ‘mathjax’. Based on issues at GitHub and the very sparse documentation on the subject, this no longer can be expected to work out of the box, if at all. At least the examples in the ‘plotly’ documentation no longer render as expected.
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.
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’.
Artificial data.
Code
set.seed(1)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
df$idx <- paste("id", df$x, sep = "-")2 Using ‘plotly’
R package ‘plotly’ can convert a regular ggplot into an interactive plot that can be embedded in HTML output. Thus the usual Grammar of Graphics familiar to ‘ggplot2’ users can be used to create interactive plots. The disadvantage is that only a rather small subset of existing geoms are supported. The reason is that ‘plotly’ is implemented as a parsing engine that recognizes only known ‘ggplot2’ layer functions.
The geometries defined in R package ‘ggpp’, used by default in several of the stats in ‘ggpmisc’ are not supported. These stats can still be used together with geoms from ‘ggplot2’ with some limitations. The main one of these is that ‘plotly’ does not support plotmath expressions. The stats from ‘ggpmisc’ can generate text strings mapped to the label aesthetic encoded as plain text but the resulting annotations are not as nice as when rendering plotmath expressions using R graphic devices.
Recent versions of ‘quarto’ do support this approach.
Code
my.formula <- y ~ x
p1 <- ggplot(data = df, aes(x = x, y = y)) +
stat_poly_line(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(p1)Warning in eval(expr[[3]], env): restarting interrupted promise evaluation
Works with Quarto >= 1.4.0
‘ggpmisc’ (>= 0.7.0) fully supports encoding labels using \(\LaTeX\) markup, potentially opening and additional route to nicely rendered fitted model equations. However, this feature seems to be broken in ‘plotly’ as the example in the help page plotly::TeX() also fails to render correctly. Has \(\LaTeX\) support with ‘mathjax’ been removed? or is this a setup problem?
Code
my.formula <- y ~ x
p2 <- ggplot(data = df, aes(x = x, y = y)) +
stat_poly_line(method = "lm",
se=FALSE,
color="black",
formula = my.formula) +
stat_poly_eq(geom = "text",
output.type = "latex",
formula = my.formula,
mapping = aes(label = after_stat(TeX(eq.label))),
label.x = 20) +
geom_point()
ggplotly(p2)Does not work with Quarto 1.9.x!
3 Using ‘ggiraph’
R package ‘ggiraph’ seems not to render layers with data computed by stats, preventing both model equations and the prediction line from being rendered. Although this example uses stats from ‘ggpmisc’ the example in the Appendix that relies purely on ‘ggplot2’ fails in the same way.
Code
my.formula <- y ~ x
p3 <- ggplot(data = df, aes(x = x, y = y,
tooltip = round(y, 0), data_id = idx)) +
stat_poly_line(method = "lm",
se = FALSE,
color="black",
formula = my.formula) +
stat_poly_eq(formula = my.formula,
mapping = use_label(c("eq", "R2"), sep = ", "),
label.x = 20) +
geom_point_interactive()
girafe(ggobj = p3)Equation and fitted model line are not displayed!
3.1 Test
Example not using ‘ggpmisc’ also fails.
Code
detach(package:ggpmisc)Code
p4 <- ggplot(data = df, aes(x = x, y = y,
tooltip = round(y, 0), data_id = idx)) +
stat_smooth(method = "lm",
formula = y ~ x) +
geom_point_interactive()
girafe(ggobj = p4)Fitted model line and confidence band are not displayed!
The possibilities remain rather limted at the moment for the creation of interactive plots with ‘ggpmisc’.