2017-10-18 11 views
1

私は、さまざまな色で経度と緯度の値をプロットしています。プロットされたポイントにシーケンス番号(1,2,3 ...)を入れることができる方法はありますか?私はある緯度と経度のペアから他の一つへの人の移行を見たいと思います。Rプロット内のデータポイントにシーケンス番号を付ける

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

答えて

0

これを試してみてください。キーは、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 
+0

ありがとうございました。 – Saara

関連する問題