'How can I use substitude within `geom_textabline`'s label?
I am trying to use substitute to substitute some values into the label text of geom_textabline. However, it is constantly giving me the following error:
Error in
check_aesthetics(): ! Aesthetics must be either length 1 or the same as the data (1): label
I tried to convert the expression in the label to characters, or use paste to join etc. but none worked.
How can I use substitude within geom_textabline's label?
dSlope <- 2
gg <- ggplot(mtcars, aes(x=mpg, y=cyl)) +
#facet_wrap(Y~., scales = "free")+
geom_textabline(label = substitute("r = -("*x~ frac(mu*m,s)*") t", list(x = sprintf("%.3f", abs(dSlope))))
, slope = dSlope, intercept = 0
, size = 0.36*fontSize, color = "black", hjust = 0.8, vjust = -0.2, show.legend = FALSE, parse = TRUE)
gg
Note: the above code is a simplied versions of a larger and more complicated code.
Solution 1:[1]
The label generated by substitute is being interpreted as having length 3. A workaround is to deparse the substitute and use parse = TRUE:
library(geomtextpath)
#> Loading required package: ggplot2
dSlope <- 2
fontSize <- 16
ggplot(mtcars, aes(mpg, cyl)) +
geom_textabline(label = deparse(substitute("r = -("*x~ frac(mu*m,s)*") t",
list(x = sprintf("%.3f", abs(dSlope))))),
slope = dSlope, intercept = 0,
size = 0.36*fontSize, color = "black", parse = TRUE,
hjust = 0.4, vjust = -0.2, show.legend = FALSE)

Created on 2022-05-04 by the reprex package (v2.0.1)
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 |
