我正在用不同的颜色绘制经度和纬度值。有没有办法可以将序列号(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
答案 0 :(得分: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