0

I'm trying to adjust an equation formatting which is shown while using ggplot2 (ver. 3.3.5) and ggpmisc (ver. 0.5.1) with stat_poly_eq. All works just fine but for the case when the coefficients in the equation became small: the "minus" sign in the power of ten is shown in the upper position relative to the rest part of the exponent.

The ReprEx can be as below:

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

v1 <- c(0.04, 0.04, 0.07, 0.07, 0.08, 0.09, 0.1, 0.1, 0.1, 0.11, 0.11, 0.12, 0.12, 0.12, 0.12, 0.13, 0.13, 0.13, 0.13, 0.14)
v2 <- c(2e-06, 1e-05, 4e-06, 2.2e-05, 1.6e-05, 1e-05, 1.8e-05, 2.6e-05, 3.4e-05, 1.7e-05, 2.8e-05, 1.4e-05, 2e-05, 2.4e-05, 2.8e-05, 2.6e-05, 3.4e-05, 3.4e-05, 4.6e-05, 2.6e-05)
ddd <- data.frame(speed = v1, dist = v2)
ggplot(ddd, aes(speed, dist)) +
    geom_point() +
    stat_poly_line(formula = y ~ x) +
    stat_poly_eq(use_label(c("eq", "adj.R2")), formula = y ~ x)

The resulting equation I get is on the image: enter image description here

How can this problem be solved?

I was trying to use geom_text with

m <- lm(y ~ x, data = ttt)
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
                   list(a = format(coef(m)[1], digits = 4),
                        b = format(coef(m)[2], digits = 4),
                        r2 = format(summary(m)$r.squared, digits = 3)))
  dftext <- data.frame(x = 0, y = 0, eq = as.character(as.expression(eq)))

and

geom_text(aes(label = eq), data = dftext, parse = TRUE) +

but this piece of code didn't show anything.

The goal is to adjust symbols in an exponent of the equation coefficients.

densz
  • 31
  • 3
  • When I run your code, I get an empty plot. – Quinten Jan 27 '23 at 15:43
  • @Quinten, without any errors and warnings? Did you run this code in RStudio? – densz Jan 29 '23 at 08:57
  • Hi @densz, yes without any errors in Rstudio – Quinten Jan 29 '23 at 09:05
  • Hi @Quinten, may be there is no such a data set in your environment. I've just updated my example, try it, please – densz Jan 29 '23 at 09:41
  • Now it works! When I run your code the minus is in the lower part. That is strange. Maybe it has something to do with graphics device output settings. – Quinten Jan 29 '23 at 09:48
  • What you see could be a problem with the graphics device or the fonts used by default. This could be operating system specific. What graphic device are you using? For most kinds of output there are alternatives. Check whether the default devices from R are in use or if package 'ragg' is in use (as with recent versions of RStudio). For best possible output to PDF you can use the `tikzdevice` and process with `LaTeX`. `stat_poly_eq()` can output `LaTeX` encoded equations if you pass `type = "LaTeX"`. The LateX option has been used and tested less thoroughly than R expressions, but should work. – Pedro J. Aphalo Jan 30 '23 at 13:01
  • @PedroJ.Aphalo, how can I find out what graphic device is used by default? – densz Feb 01 '23 at 10:59
  • @denz In R you can open a device, print and close it. See for example, `help(pdf)`. If you are using a recent version of RStudio, check the menue under Tools > Global options... > General and then select the Graphics tab. You can change there the graphic device "familiy" used by RStudio. I you are Exporting with the icons in the Plots window, the dialogue box includes an option to use Cairo devices. In Rmarkdown, there is a knitr option device that can be used to set the device used. If you just save the plots with `ggsave()` you can set the device used by passing an argument to `device`. – Pedro J. Aphalo Feb 01 '23 at 16:32
  • @PedroJ.Aphalo, thank you for pointing on that settings. In my environment there are 4 options in the list of devices: "Default", "Cairo", "Cairo PNG" and "AGG". Only AGG influences on the resulting plot: the "minus" sign appears lower and closer to the following number than in the case of the other devices from options, but it seems to be using some kind of monotype fonts. – densz Feb 02 '23 at 09:31

0 Answers0