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

如何让ggplot中使用geom_curve绘制的箭头线段更平滑?

解决ggplot2曲线箭头锯齿问题的方案

1 调高曲线插值点数

geom_curve默认插值点数仅为50,大曲率场景下很容易出现锯齿,手动调高n参数即可大幅提升平滑度:

ggplot(mtcars, aes(cyl, mpg)) +
  geom_point() + 
  geom_curve(
    aes(y = 18, yend = 15, x = 4, xend = 3.75), 
    arrow = arrow(length = unit(0.1, "cm"), type = 'closed'),
    curvature = -0.85, 
    color = '#e91d82', 
    lineend = 'round',
    linejoin = 'round', # 优化线段连接处平滑度
    n = 300 # 插值点数,数值越大曲线越平滑,可根据实际效果调整
  )

2 优化导出渲染效果

如果是导出图片后查看有锯齿:

  • 优先导出svg、pdf等矢量格式,不会存在分辨率相关的锯齿问题
  • 若需导出png等位图格式,将分辨率调高至300dpi以上即可,推荐使用ragg设备渲染,效果优于R默认的图形设备:
library(ragg)
# 导出高清位图示例
agg_png("平滑箭头效果图.png", width = 6, height = 4, units = "in", res = 300)
print(
  ggplot(mtcars, aes(cyl, mpg)) +
    geom_point() + 
    geom_curve(
      aes(y = 18, yend = 15, x = 4, xend = 3.75), 
      arrow = arrow(length = unit(0.1, "cm"), type = 'closed'),
      curvature = -0.85, 
      color = '#e91d82', 
      lineend = 'round',
      linejoin = 'round',
      n = 300
    )
)
dev.off()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 15:09:00