[英]How to add points on SpatialLinesDataFrame using ggplot2?
I have a spatial lines data frame and some points on it.我有一个空间线数据框和一些点。 The points have 3rd column some numbers let's say average roadkills (animal killed by cars, aka hit-run) at x and y coordinates.
这些点在第 3 列有一些数字,比方说在 x 和 y 坐标处的平均道路杀戮(被汽车杀死的动物,又名撞车)。 I want to plot which part of the road these numbers are high but I cannot place points on the.network yet.
我想要 plot 这些数字高的道路的哪一部分,但我还不能在 .network 上放置点。 An example code I get it from ready to use spatial lines data frame example .
我从ready to use spatial lines data frame example获得的示例代码。
PS: I am okay to use other plots as long as it looks good. PS:我用其他的plot只要好看就行。 Please send me suggestions if you have any.
如果您有任何建议,请给我发送建议。 PS2: I would love to put it on the real map but I cannot figure how to get the map working with x and y coordinates
PS2:我很想把它放在真正的 map 上,但我不知道如何让 map 使用 x 和 y 坐标
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)
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,4096415,4095605)
poiY=c(2302896,2301336,2299596)
poiZ=c(.3,.2,.43)
poii=cbind(poiX,poiY,poiZ)
plot(mySLDF)
points(poii,col="red")
I tried this code but did not work我试过这段代码但没有用
library(sp)
mySLDF_fortify <- fortify(mySLDF)
ggplot(mySLDF_fortify, aes(x=long, y=lat, group=group)) +
geom_path() +
theme_classic()
ggplot(mySLDF_fortify, aes(x=long, y=lat, group=z)) +
geom_path() + geom_point(poii, aes(x=ooiX, y=poiY,color=poiZ))+
scale_color_gradient2(low="yellow",mid = "darkblue",high="red")
I made some changes to get ggplot
to work.我做了一些更改以使
ggplot
工作。 I made poii
a data frame with a group
column and I modified the geom_point
call with explicit named parameters as well as correcting the names of the data frame columns being passed.我使
poii
成为一个带有group
列的数据框,并使用显式命名参数修改了geom_point
调用,并更正了传递的数据框列的名称。
library(sp)
library(ggplot2)
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)
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,4096415,4095605)
lat=c(2302896,2301336,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, y=lat, color=poiZ, group=group)) +
scale_color_gradient2(low="yellow", mid="darkblue", high="red") +
theme_classic()
The result looks like this.结果看起来像这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.