r - ggplot 带虚线的箭头

标签 r ggplot2

考虑以下轨迹:

test = data.frame(x = c(9500, 25500, 47500), y = c(10.03, 7.81, 0.27))

我想将此绘制为 ggplot2 的路径,带有标识方向的箭头:
ggplot(test) + aes(x = x, y = y) +
  geom_path(size = 1, 
    arrow = arrow(type = "open", angle = 30, length = unit(0.1, "inches")))

这看起来不错。但是,如果我想使用虚线,箭头也用虚线绘制,看起来很糟糕:
ggplot(test) + aes(x = x, y = y) +
  geom_path(linetype ="dashed", size = 1, 
    arrow = arrow(type = "open", angle = 30, length = unit(0.1, "inches")))

有没有办法排除 linetype来自箭头定义本身的美学?

最佳答案

我们可以通过使用具有实线的单独几何图形绘制箭头来解决此问题。像这样的东西:

geom_end_arrow = function(path, arrow, ...) {
  path = tail(path,2)
  x.len = diff(path$x)/1000
  y.len = diff(path$y)/1000
  path$x[1] = path$x[2] - x.len
  path$y[1] = path$y[2] - y.len
  geom_path(data = path, mapping = aes(x=x, y=y), arrow=arrow, ...)
}

ggplot(test) + aes(x = x, y = y) +
  geom_path(linetype ="dashed", size = 1) +
  geom_end_arrow(test, size = 1, arrow = arrow(type = "open", angle = 30, length = unit(0.1, "inches")))

enter image description here

关于r - ggplot 带虚线的箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48650464/

相关文章:

r - 如何使用不同的 y 轴文本长度获得相同的面板宽度

r - 将误差线设置为 ggplot2 条形图上的标准差

r - 绘图条形图中的颜色

r - ggplot2 - 打印绘图气球内存

r - as.glht : Cannot convert an object of class ‘list’ to a ‘glht’ object

r - 检测每个单词的第一个字母是否为大写字母

r - 如何在 R 中为 ggplot2 增加 geom_smooth 中的评估点数

r - 在 ggplot 中注释分组箱线图 - 在箱线图下方添加观察数量

r - 检查是否有任何值在范围内

r - 从 ggmap 对象获取边界框