如何解决ggcurve闭合箭头出现缺口的问题?
解决geom_curve大尺寸箭头两端缺失的问题
当使用geom_curve设置较大size参数时,箭头与曲线衔接处出现空缺,是因为箭头默认位置和粗曲线末端存在间隙,以下是几种可行的解决方法:
方法1:调整箭头的gap参数
在arrow()函数中添加gap参数,设置为负数值让箭头向曲线方向偏移,填补间隙。可根据线宽微调数值:
library(ggplot2) library(geomtextpath) ggplot() + scale_x_continuous(name = "rMZ", limits = c(-0.05, 1.1), breaks = seq(0,1,0.25)) + scale_y_continuous(name = "rDZ", limits = c(-0.05, 1), breaks = seq(0,1,0.25)) + coord_fixed() + geom_curve( aes(x = 1.03, y = -0.01, xend = 0.65, yend = 0.88), arrow = arrow(length = unit(0.05, "npc"), ends="both", type="closed", gap = unit(-0.01, "npc")), size = 1.8, curvature = 0.25 ) + geom_labelabline(intercept = 0, slope = .5, label = "AE model", hjust = 0.85)
方法2:分层绘制曲线与箭头
先绘制一条无箭头的粗曲线作为底层,再在同一位置绘制细一点的带箭头曲线,让箭头覆盖在粗线上,完全避免衔接空缺:
library(ggplot2) library(geomtextpath) ggplot() + scale_x_continuous(name = "rMZ", limits = c(-0.05, 1.1), breaks = seq(0,1,0.25)) + scale_y_continuous(name = "rDZ", limits = c(-0.05, 1), breaks = seq(0,1,0.25)) + coord_fixed() + # 底层粗曲线(无箭头) geom_curve( aes(x = 1.03, y = -0.01, xend = 0.65, yend = 0.88), size = 1.8, curvature = 0.25, color = "black" ) + # 上层带箭头的细曲线 geom_curve( aes(x = 1.03, y = -0.01, xend = 0.65, yend = 0.88), arrow = arrow(length = unit(0.05, "npc"), ends="both", type="closed"), size = 0.5, curvature = 0.25, color = "black" ) + geom_labelabline(intercept = 0, slope = .5, label = "AE model", hjust = 0.85)
方法3:换用ggforce的geom_curve
ggforce包的geom_curve渲染逻辑与原生ggplot不同,对大尺寸箭头的衔接支持更好,直接替换即可:
library(ggplot2) library(geomtextpath) library(ggforce) ggplot() + scale_x_continuous(name = "rMZ", limits = c(-0.05, 1.1), breaks = seq(0,1,0.25)) + scale_y_continuous(name = "rDZ", limits = c(-0.05, 1), breaks = seq(0,1,0.25)) + coord_fixed() + ggforce::geom_curve( aes(x = 1.03, y = -0.01, xend = 0.65, yend = 0.88), arrow = arrow(length = unit(0.05, "npc"), ends="both", type="closed"), size = 1.8, curvature = 0.25 ) + geom_labelabline(intercept = 0, slope = .5, label = "AE model", hjust = 0.85)
内容的提问来源于stack exchange,提问作者Mohan
相关产品推荐
相关产品推荐

