如何解决如何使用ggplot2在SpatialLinesDataFrame上添加点?
我有一个空间线数据框及其上的一些点。这些点在第三列,有些数字可以说是x和y坐标上的平均道路杀伤力(被汽车杀死的动物,又名撞车)。我想绘制这些数字在道路的哪一部分,但是我还不能在网络上放置点。我从ready to use spatial lines data frame example获得了一个示例代码。
PS:只要看起来不错,我可以使用其他地块。如果有任何建议,请给我发送建议。 PS2:我很想将它放在真实地图上,但是我无法弄清楚如何使用x和y坐标来处理地图
Coords <- matrix(c(4095215,2303286,4095275,2303226,2303196,4095395,2303076,4095425,4095485,2303016,2302896,4095545,2302836,2302806,4095575,2302776,2302746,4095635,2302686,2302656,4095665,2302626,2302596,4095695,2302566,2302536,4095725,2302506,2302476,4095755,2302446,4095785,4095815,4095845,4095875,4095965,2302356,2302296,4096055,2302206,2302146,4096085,2302116,2302086,4096205,2301966,2301906,4096295,2301816,2301666,4096325,2301636,2301516,4096385,2301456,2301426,4096445,2301366,4096415,2301336,2301276,2301246,2301156,2301096,2301066,2300886,2300856,2300826,4096355,2300796,2300766,2300736,2300706,4096265,2300616,2300556,2300496,4096235,2300466,2300436,2300406,2300376,2300346,4096175,2300286,4096115,2300256,2300226,4095995,2300136,2300106,2299986,2299956,4095905,2299926,2299896,2299866,2299806,2299776,4095605,2299596,2299566,2299536,4095515,2299566),ncol = 2,byrow = T)
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))
poiX=c(4095485,4095605)
poiY=c(2302896,2299596)
poiZ=c(.3,.2,.43)
poii=cbind(poiX,poiY,poiZ)
plot(mySLDF)
points(poii,col="red")
我尝试了这段代码,但是没有用
library(sp)
mySLDF_fortify <- fortify(mySLDF)
ggplot(mySLDF_fortify,aes(x=long,y=lat,group=group)) +
geom_path() +
theme_classic()
ggplot(mySLDF_fortify,group=z)) +
geom_path() + geom_point(poii,aes(x=ooiX,y=poiY,color=poiZ))+
scale_color_gradient2(low="yellow",mid = "darkblue",high="red")
解决方法
我进行了一些更改以使ggplot
正常工作。我用一个poii
列为group
一个数据框,并用显式的命名参数修改了geom_point
调用,并更正了要传递的数据框列的名称。
library(sp)
library(ggplot2)
Coords <- matrix(c(4095215,2303286,4095275,2303226,2303196,4095395,2303076,4095425,4095485,2303016,2302896,4095545,2302836,2302806,4095575,2302776,2302746,4095635,2302686,2302656,4095665,2302626,2302596,4095695,2302566,2302536,4095725,2302506,2302476,4095755,2302446,4095785,4095815,4095845,4095875,4095965,2302356,2302296,4096055,2302206,2302146,4096085,2302116,2302086,4096205,2301966,2301906,4096295,2301816,2301666,4096325,2301636,2301516,4096385,2301456,2301426,4096445,2301366,4096415,2301336,2301276,2301246,2301156,2301096,2301066,2300886,2300856,2300826,4096355,2300796,2300766,2300736,2300706,4096265,2300616,2300556,2300496,4096235,2300466,2300436,2300406,2300376,2300346,4096175,2300286,4096115,2300256,2300226,4095995,2300136,2300106,2299986,2299956,4095905,2299926,2299896,2299866,2299806,2299776,4095605,2299596,2299566,2299536,4095515,2299566),ncol = 2,byrow = T)
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))
long=c(4095485,4095605)
lat=c(2302896,2299596)
poiZ=c(.3,.2,.43)
group = c(1.1,1.1,1.1)
poii=data.frame(long,lat,poiZ,group)
mySLDF_fortify <- fortify(mySLDF)
ggplot(mySLDF_fortify,aes(x=long,y=lat,group=group)) +
geom_path() +
geom_point(data=poii,mapping=aes(x=long,color=poiZ,group=group)) +
scale_color_gradient2(low="yellow",mid="darkblue",high="red") +
theme_classic()
结果看起来像这样。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。