将序列号放在 R Plot 中的数据点上
Putting sequence number on the data points in a R Plot
我正在绘制不同颜色的经度和纬度值。有什么方法可以将序列号(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
试试这个。关键是 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
我正在绘制不同颜色的经度和纬度值。有什么方法可以将序列号(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
试试这个。关键是 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