You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何消除ggpubr中stat_cor()手动标签逗号前的空格?

解决ggpubr中stat_cor()拼接标签时逗号前后的空格问题

方法1:使用paste0结合parse参数

stat_cor()的label参数支持解析表达式,直接用paste0无缝拼接标签与逗号,再开启parse = TRUE即可消除空格:

library(ggpubr)
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  stat_cor(
    label = "paste0(..rr.label.., ',', ..p.label..)",
    parse = TRUE
  )

paste0会直接将元素无空格连接,parse = TRUE让ggplot正确解析该表达式文本。

方法2:用表达式连接符*构造无空格拼接

在ggplot的表达式语法中,*代表无空格拼接元素,直接用它连接标签和逗号即可:

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  stat_cor(
    label = "..rr.label.. * ',' * ..p.label.."
  )

这种方式无需额外开启parse参数,stat_cor默认会解析label中的表达式。

内容的提问来源于stack exchange,提问作者einGlasRotwein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 07:30:54