R语言 几何线绘图顺序

czq61nw1  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(102)

我有一系列有序的点,如下所示:

然而,当我尝试用一条线连接这些点时,我得到以下输出:

该图将26连接到1,25连接到9和10(一些错误),而不是按照顺序。绘制点的代码如下所示:

p<-ggplot(aes(x = x, y = y), data = spat_loc)
p<-p + labs(x = "x Coords (Km)", y="Y coords (Km)") +ggtitle("Locations")
p<-p + geom_point(aes(color="Red",size=2)) + geom_text(aes(label = X))
p + theme_bw()

绘图代码:

p + 
geom_line((aes(x=x, y=y)),colour="blue") +
theme_bw()

包含位置的文件具有以下结构:

X    x    y
1    210  200 
.
.
.

其中X是数字ID,x和y是坐标对。
我需要做些什么来使这条线遵循点的顺序?

lymnna71

lymnna711#

geom_path()将按照原始顺序连接点,因此您可以按照希望连接的方式对数据进行排序,然后只需执行+ geom_path()。这里有一些虚拟数据:

dat <- data.frame(x = sample(1:10), y = sample(1:10), order = sample(1:10))
ggplot(dat[order(dat$order),], aes(x, y)) + geom_point() + geom_text(aes(y = y + 0.25,label = order)) +
  geom_path()

相关问题