Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading