我想创建一个geom_path(),它的箭头指向路径中的下一个位置。

我可以毫无问题地获得绘图的路径,例如:

df <- (x=1:12, y=20:31, z=1:12)
p <- ggplot(df, aes(x=x, y=y))
p + geom_point() + geom_path()

现在,我想做的是绘制从路径中的一个元素到下一个元素的指向箭头。

如果可以告诉我如何平滑从路径中的一个元素到下一个元素的线条,则可以加分。

最佳答案

geom_segment有一个arrow参数。这是一个简短的示例:

library(grid) # needed for arrow function

p <- ggplot(df, aes(x=x, y=y)) +
     geom_point() +
     geom_segment(aes(xend=c(tail(x, n=-1), NA), yend=c(tail(y, n=-1), NA)),
                  arrow=arrow(length=unit(0.3,"cm")))
library(grid)函数需要arrow(),请参阅here

关于r - 示例需要: Using arrow() with ggplot2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3421331/

10-12 17:36