如何在 `geom_textabline` 的标签中使用替代?
How can I use substitude within `geom_textabline`'s label?
我正在尝试使用 substitute
将一些值替换到 geom_textabline
的标签文本中。但是,它不断给我以下错误:
Error in check_aesthetics()
: ! Aesthetics must be either length 1 or
the same as the data (1): label
我尝试将标签中的表达式转换为字符,或使用粘贴连接等,但 none 成功了。
如何在 geom_textabline
的标签中使用替代品?
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
注意:以上代码是更大更复杂代码的简化版本。
substitute
生成的标签被解释为长度为 3。解决方法是 deparse
替代并使用 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)
由 reprex package (v2.0.1)
于 2022-05-04 创建
我正在尝试使用 substitute
将一些值替换到 geom_textabline
的标签文本中。但是,它不断给我以下错误:
Error in
check_aesthetics()
: ! Aesthetics must be either length 1 or the same as the data (1): label
我尝试将标签中的表达式转换为字符,或使用粘贴连接等,但 none 成功了。
如何在 geom_textabline
的标签中使用替代品?
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
注意:以上代码是更大更复杂代码的简化版本。
substitute
生成的标签被解释为长度为 3。解决方法是 deparse
替代并使用 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)
由 reprex package (v2.0.1)
于 2022-05-04 创建