'Syntax of `sep =` as separator

I would like to understand how is the syntax of the command sep =. Below, I report an example code:

library(ggpmisc)
library(ggplot2)


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)

I understand that sep = "*\", \"*" is aiming to add a comma between eq.label and adj.rr.label. There is no reference on the guide of stat_poly_eq, I would like to understand the meaning of "*\" and \"*", perhaps learning something more to change this configuration.



Solution 1:[1]

The labels you create in stat_poly_eq are character strings. They are parsed into expressions, which in turn are converted into plotmath symbols. You can get a feel for how this works if you do:

plot(1:10, type = 'n')
text(5, 5, label = expression(paste(y~`=`~3*italic(x)^2*", "*R^2)))

enter image description here

All the sep is doing in your case is providing a separating comma and space between the formula and the R squared. You can change this to anything you like as long as it parses correctly to a valid plotmath expression:

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 = "*\" is the formula, and the R squared is \"*")),
               formula = formula)

enter image description here

See ?plotmath to find out all the things your can do with plotmath expressions

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Allan Cameron