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

y轴含负值时stat_cor无法定位到底部的解决方法咨询

解决stat_cor标签位置异常的问题

你的问题出在ggplot全局映射中设置了color=nlk,导致stat_cor默认按nlk分组计算相关系数并生成标签,多个标签叠加后看起来停留在绘图中间。要将全局相关系数标签放到右下角,可通过以下两种方式解决:

方法一:忽略全局美学映射

在stat_cor中设置inherit.aes = FALSE,单独指定全局的x、y映射,再调整位置参数:

ggplot(df, aes(x= nlk_percent ,y= c_percent, color=nlk)) +
  geom_point(position = position_jitter(width = 0.05, height = 0), size=7, alpha=.3) + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
  theme_classic() +  
  stat_cor(
    method = "pearson", 
    inherit.aes = FALSE,
    aes(x = nlk_percent, y = c_percent),
    label.x.npc = "right",
    label.y.npc = "bottom"
  )

方法二:强制全局分组计算

在stat_cor的美学映射中设置group=1,强制按整体数据集计算相关系数:

ggplot(df, aes(x= nlk_percent ,y= c_percent, color=nlk)) +
  geom_point(position = position_jitter(width = 0.05, height = 0), size=7, alpha=.3) + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
  theme_classic() +  
  stat_cor(
    method = "pearson", 
    aes(group=1),
    label.x.npc = "right",
    label.y.npc = "bottom"
  )

两种方法都能生成全局的Pearson相关系数标签,并将其定位到绘图右下角。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 10:51:36