需要的示例:使用带有ggplot2的arrow()

Bra*_*sen 11 r ggplot2

我想创建一个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()
Run Code Online (Sandbox Code Playgroud)

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

如果您可以告诉我如何平滑从路径中的一个元素到下一个元素的线条,则需要额外的标记.

rcs*_*rcs 16

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")))
Run Code Online (Sandbox Code Playgroud)

library(grid)arrow()功能需要,请看这里.

  • `geom_path`(或`geom_line`)不会在每段中绘制箭头,最后一个点的位置只有一个箭头。 (2认同)