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

ggplot2中用after_stat()替代..dx..的正确语法咨询

解决ggplot2中..dx../..dy..弃用后的替代问题

你遇到的问题是对after_stat()的用法有误——正确的做法是把整个计算表达式放在after_stat()内部,而非单独包裹每个变量。

原代码中sqrt(..dx..^2 + ..dy..^2)的正确替代写法是after_stat(sqrt(dx^2 + dy^2))。新版ggplot2要求用after_stat()引用stat层生成的变量,且需将基于这些变量的计算逻辑整体放在after_stat()参数中,让ggplot2在完成stat计算后再执行表达式。

修正后的完整代码如下:

ggplot(data = europe_wind_stream) +
  geom_sf(
    data = europe_sf,
    fill = NA,
    color = "grey40",
    size = .25,
    alpha = .99
  ) +
  metR::geom_streamline(
    aes(
      x = lon,
      y = lat,
      dx = u,
      dy = v,
      color = after_stat(sqrt(dx^2 + dy^2)),
      alpha = after_stat(sqrt(dx^2 + dy^2)),
      linewidth = after_stat(sqrt(dx^2 + dy^2))
    ),
    L = 2,
    res = 10,
    n = 60,
    arrow = NULL,
    lineend = "round",
    inherit.aes = F
  )

注:代码中最后一个美学映射后的逗号建议去掉,避免出现语法警告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 12:38:10