【问题标题】:How to color arrows based on direction in geom_path如何根据 geom_path 中的方向为箭头着色
【发布时间】:2018-06-19 21:57:43
【问题描述】:

我正在尝试使用 ggplot2 中的 geom_path 根据方向对箭头进行颜色编码。我在单独的行中有多年的数据,并且想要一个从最早的数据点指向最近的数据点的箭头,用颜色描绘方向,使其更容易解释。这些点按 Lake_ID 分组,因为每年都有多行,所以我遇到了麻烦。

这是基本数据:

    Lake_ID Year    Gear    psd BioVolume
    16  2012    EF  0.370967742 0.095585695
    16  2012    GN  1   0.095585695
    16  2012    TN  0   0.095585695
    16  2012    Whole   0.375   0.095585695
    16  2017    EF  0.214285714 0.100623115
    16  2017    GN  1   0.100623115
    16  2017    TN  0   0.100623115
    16  2017    Whole   0.179487179 0.100623115

    ggplot(blg.dup.year,aes(BioVolume,psd,group=Lake_ID,label=Year))+
        geom_point(aes(BioVolume,psd,colour=Year))+theme_bw()+
        facet_grid(Gear~.,scales = "free_y")+
        labs(title="Bluegill PSD",y="PSD",x="Littoral BioVolume")+
        geom_path(arrow=arrow(angle=30,length=unit(0.1,"inches"),
            type="closed"),aes(group=Lake_ID))

谢谢!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    假设您所说的方向是指箭头是向上还是向下,这是一种解决方案。 将您的原始图分配给一个变量:

    p1 <- ggplot(blg.dup.year,aes(BioVolume,psd,group=Lake_ID,label=Year))+
      geom_point(aes(BioVolume,psd,colour=Year))+theme_bw()+
      facet_grid(Gear~.,scales = "free_y")+
      labs(title="Bluegill PSD",y="PSD",x="Littoral BioVolume")+
      geom_path(arrow=arrow(angle=30,length=unit(0.1,"inches"),
                            type="closed"),aes(group=Lake_ID))
    

    从图层获取数据(您的箭头是第二个几何图形):

    arrow_data <- layer_data(p1, 2) 
    

    从 y 轴获取方向,然后根据符号分配颜色:

    directions <- diff(arrow_data$y)[seq(1,nrow(arrow_data),2)]
    colors <- case_when(sign(directions) == -1 ~ "red",
                        sign(directions) == 0 ~ "orange",
                        sign(directions) == -1 ~ "green", 
                        TRUE ~ "grey50")
    

    您需要将颜色加倍,因为 ggplot 需要颜色作为起点和终点。

    colors <- rep(colors,each = 2)
    

    最后只为几何图形添加颜色:

    ggplot(blg.dup.year,aes(BioVolume,psd,group=Lake_ID,label=Year))+
       geom_point(aes(BioVolume,psd,colour=Year))+theme_bw()+
       facet_grid(Gear~.,scales = "free_y")+
       labs(title="Bluegill PSD",y="PSD",x="Littoral BioVolume") +
       geom_path(arrow=arrow(angle=30,length=unit(0.1,"inches"),
                             type="closed"),aes(group=Lake_ID), color = colors)
    

    所以对于你的例子,你会得到:

    【讨论】:

    • 理想情况下,颜色将描绘 x 轴上的增加或减少。我稍微调整了您给我的代码,并且在大多数情况下它运行良好。有一组有四个箭头段,它们没有正确着色。这是调整后的代码:
    • 'point_data
    • 我认为问题的根源是有单点,因此采取顺序的差异是行不通的。
    • 426 2014 EF 0.155555556 0.590394261 -8.163782743 426 2014 0.2 GN 0.590394261 -8.163782743 426 2014 0 TN 0.590394261 -8.163782743 426 2014全0.13559322 0.590394261 -8.163782743 426 2015 EF 0.401709402 0.269850811 -8.295077669 426 2015 GN 1 0.269850811 -8.295077669 426 2015 TN 0.057142857 0.269850811 -8.295077669 426 2015全0.326797386 0.269850811 -8.295077669 426 2016 EF 0.136363636 0.439568743 -7.106322148 426 2016 GN 1 0.439568743 -7.106322148 426 2016 TN 0.129032258 0.439568743 -7.106322148 426 2016全0.148148148 0.439568743 -7.106322148 跨度>
    • 另外,很抱歉格式不正确,我不知道如何在评论中发布代码。
    猜你喜欢
    • 2020-11-30
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2016-01-30
    • 1970-01-01
    相关资源
    最近更新 更多