【问题标题】:Putting sequence number on the data points in a R Plot将序列号放在 R 图中的数据点上
【发布时间】:2018-03-30 07:35:57
【问题描述】:

我正在用不同的颜色绘制经度和​​纬度值。有什么方法可以将序列号(1,2,3 ... 如下图所示)放在已绘制的点上。我想看到一个人从一个经度和纬度对到另一个的过渡。

 require(ggplot2)
 df <- data.frame(x = rpois(10, 5), y = rpois(10, 5), group = 1:10)
 g <- ggplot(df, aes(x = x, y = y, color = as.factor(group)))
 g <- g + geom_segment(aes(xend=c(tail(x, n=-1), NA),
                      yend=c(tail(y, n=-1), tail(y, n = 1))),
                  arrow=arrow(length = unit(0.5, "cm")))
 g <- g + theme_bw() + theme(legend.position="none") + xlab("longitude") + 
 ylab("latitude") 
 g

plot

【问题讨论】:

    标签: r plot gps


    【解决方案1】:

    试试这个。关键是label 美学和geom_text。我还使用了jitter函数来避免箭头和文本之间的重叠

    library(ggplot2)
    df <- data.frame(x = rpois(10, 5), y = rpois(10, 5), group = 1:10)
    g <- ggplot(df, aes(x = x, y = y, color = as.factor(group),label = group))
    g <- g + geom_segment(aes(xend=c(tail(x, n=-1), NA),
                              yend=c(tail(y, n=-1), tail(y, n = 1))),
                          arrow=arrow(length = unit(0.5, "cm")))
    g <- g + theme_bw() + theme(legend.position="none") + xlab("longitude") +
      ylab("latitude") + geom_text(aes(x = jitter(x), y = jitter(y)))
    g
    

    【讨论】:

    • 感谢您的快速回复:)
    猜你喜欢
    • 2023-01-01
    • 1970-01-01
    • 2013-12-09
    • 2021-04-11
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    相关资源
    最近更新 更多