2017-10-18 82 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