我有一系列的有序点,如下所示:
但是,当我尝试通过线连接点时,会得到以下输出:
该图将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是一对坐标。
我需要怎么做才能使线遵循点的顺序?
最佳答案
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()
关于r - ggplot2线图顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19020532/