代码之家  ›  专栏  ›  技术社区  ›  Brandon Bertelsen

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

  •  11
  • Brandon Bertelsen  · 技术社区  · 14 年前

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

    我可以得到绘制的路径,无需发布,例如:

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

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

    额外的标记,如果你能告诉我如何平滑从一个元素到下一个元素的路径。

    1 回复  |  直到 10 年前
        1
  •  16
  •   slhck    10 年前

    geom_segment has an arrow argument.下面是一个简短的例子:

    箭头功能所需的 P<-ggplot(df,aes(x=x,y=y))。+ GeoMangPooPoT()+ geom_段(aes(xend=c(tail(x,n=-1),na),yend=c(tail(y,n=-1),na)), 箭头=箭头(长度=单位(0.3,“cm”)) < /代码>

    library(grid) is needed for arrow() function,see here

    争论。下面是一个简短的例子:

    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 .