用ggplot2绘制SpatialLinesDataFrame [英] Plot SpatialLinesDataFrame with ggplot2

查看:77
本文介绍了用ggplot2绘制SpatialLinesDataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个使用功能 fortify 调用 ggplot 的SpatialLinesDataFrame.使用 fortify 时遇到的问题是,我的点之间出现了不希望有的线.我认为这与情节顺序有关

I would like to plot a SpatialLinesDataFrame calling ggplot with function fortify. The problem I have when using fortify is that I get undesirable lines between my points. I think it has something to do with the plot order

以下是我的数据示例:

Coords <- matrix(c(4095215,2303286,4095275,2303226,4095275,2303196,4095395,2303076,
4095425,2303076,4095485,2303016,4095485,2302896,4095545,2302836,4095545,2302806,
4095575,2302776,4095575,2302746,4095635,2302686,4095635,2302656,4095665,2302626,
4095665,2302596,4095695,2302566,4095695,2302536,4095725,2302506,4095725,2302476,
4095755,2302446,4095785,2302446,4095815,2302476,4095845,2302446,4095875,2302446,
4095965,2302356,4095965,2302296,4096055,2302206,4096055,2302146,4096085,2302116,
4096085,2302086,4096205,2301966,4096205,2301906,4096295,2301816,4096295,2301666,
4096325,2301636,4096325,2301516,4096385,2301456,4096385,2301426,4096445,2301366,
4096415,2301336,4096415,2301276,4096445,2301246,4096445,2301156,4096385,2301096,
4096415,2301066,4096415,2300886,4096385,2300856,4096385,2300826,4096355,2300796,
4096385,2300766,4096355,2300736,4096355,2300706,4096265,2300616,4096265,2300556,
4096205,2300496,4096235,2300466,4096205,2300436,4096205,2300406,4096235,2300376,
4096235,2300346,4096175,2300286,4096115,2300286,4096085,2300256,4096085,2300226,
4095995,2300136,4095995,2300106,4095875,2299986,4095875,2299956,4095905,2299926,
4095905,2299896,4095875,2299866,4095875,2299806,4095845,2299776,4095815,2299806,
4095605,2299596,4095605,2299566,4095575,2299536,4095545,2299566,4095515,2299536,
4095485,2299566), ncol = 2, byrow = T)

然后创建我的SpatialLinesDataFrame:

I then create my SpatialLinesDataFrame:

myLine <- Line(Coords)
myLines <- Lines(list(myLine), ID = 1)
mySL <- SpatialLines(list(myLines), proj4string = CRS("+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs"))
mySLDF <- SpatialLinesDataFrame(mySL, data = data.frame(ID = 1))

当我使用 plot 函数时.

现在使用 ggplot 函数:

mySLDF_fortify <- fortify(mySLDF)
ggplot(mySLDF_fortify, aes(x=long, y=lat, group=group)) + 
    geom_line() +
    theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

这是结果

你知道我在做什么错吗?如何更正打印顺序?

Do you know what I'm doing wrong ? How can I correct the plot order?

推荐答案

geom_line 使用x轴的顺序连接这些点. geom_path 使用 data.frame 中值的顺序.

geom_line uses the order of the x-axis to connect the points. geom_path uses the order of the values in the data.frame.

library(sp)

ggplot(mySLDF_fortify, aes(x=long, y=lat, group=group)) + 
  geom_path() +
  theme_classic()

这篇关于用ggplot2绘制SpatialLinesDataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆