在标记的位置点之间添加线
Add lines between labelled location points
我在使用 R 中的 tmap 包将标记的位置点与线连接时遇到困难。
Session 信息:
R version: 3.4.3 (2017-11-30)
Package versions: maptools_0.9-2 maps_3.2.0 stringr_1.2.0 bindrcpp_0.2 tmap_1.11 sp_1.2-7 dplyr_0.7.4 plyr_1.8.4 ggmap_2.6.1 ggplot2_2.2.1
我的目标是能够绘制轨迹,然后在保存后将其可视化,使用带有 save_tmap
功能的交互选项。
我可以绘制位置并标记它们,或者只绘制线条:
library(ggmap)
library(plyr)
library(dplyr)
library(sp)
library(tmap)
library(maps)
eg <- data.frame(longitude = c(-38.04434, -38.07048, -38.38147, -38.98167, -39.51479, -39.68861, -40.10173, -40.69829, -41.06727, -41.45114),
latitude = c(-54.00679, -53.98419, -53.82259, -53.73171, -53.77248, -53.78981, -53.71934, -53.64412, -53.53544, -53.51021),
PointNum=1:10,track=1)
head(eg,2)
# Create spatial points data frame
sp.point <- SpatialPointsDataFrame(eg[,c("longitude","latitude")],eg[,3:4],proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
## Base plot (raw data)
plot(latitude~longitude, data=sp.point, asp=1, type="l") ## Note, asp helps with lat/long plots.
text(latitude~longitude, labels=PointNum, data=eg, cex= 0.7)
## Base plot (sp object)
plot(sp.point)
## Using tmap package for points with labels (sp object)
track.point <- tm_shape(sp.point) + tm_dots("red") +tm_text("PointNum")
track.point
## Create spatial lines object
## Use the 'points_to_line' function from this post:
https://rpubs.com/walkerke/points_to_line
head(eg,2)
sp.lines <- points_to_line(data = eg,
long = "longitude",
lat = "latitude",
sort_field = "PointNum")
proj4string(sp.lines) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
track.line <- tm_shape(sp.lines) +tm_lines()
track.line
## Save as interactive map
getwd()
save_tmap(tm=track.point,filename="track_point.html")
save_tmap(tm=track.line,filename="track_line.html")
## Ideally, I would only want to save one plot which would be a combination of the two above
谁能告诉我如何制作一个交互式图,其中包含:标记的点和连接它们的线?
您需要将 + 运算符的两个 tm_shape
调用加入到单个 tmap 对象。
我正在复制您的示例,其中包含您链接的 RPubs 页面中的 points_to_line
函数,因此它会起作用,并且只打印 tmap
对象而不是保存它;这应该不会影响结果。
这是结果(线条的颜色和点的大小是我选择的)
library(ggmap)
library(plyr)
library(dplyr)
library(sp)
library(tmap)
library(maps)
points_to_line <- function(data, long, lat, id_field = NULL, sort_field = NULL) {
# Convert to SpatialPointsDataFrame
coordinates(data) <- c(long, lat)
# If there is a sort field...
if (!is.null(sort_field)) {
if (!is.null(id_field)) {
data <- data[order(data[[id_field]], data[[sort_field]]), ]
} else {
data <- data[order(data[[sort_field]]), ]
}
}
# If there is only one path...
if (is.null(id_field)) {
lines <- SpatialLines(list(Lines(list(Line(data)), "id")))
return(lines)
# Now, if we have multiple lines...
} else if (!is.null(id_field)) {
# Split into a list by ID field
paths <- sp::split(data, data[[id_field]])
sp_lines <- SpatialLines(list(Lines(list(Line(paths[[1]])), "line1")))
# I like for loops, what can I say...
for (p in 2:length(paths)) {
id <- paste0("line", as.character(p))
l <- SpatialLines(list(Lines(list(Line(paths[[p]])), id)))
sp_lines <- spRbind(sp_lines, l)
}
return(sp_lines)
}
}
eg <- data.frame(longitude = c(-38.04434, -38.07048, -38.38147, -38.98167, -39.51479, -39.68861, -40.10173, -40.69829, -41.06727, -41.45114),
latitude = c(-54.00679, -53.98419, -53.82259, -53.73171, -53.77248, -53.78981, -53.71934, -53.64412, -53.53544, -53.51021),
PointNum=1:10,track=1)
head(eg,2)
# Create spatial points data frame
sp.point <- SpatialPointsDataFrame(eg[,c("longitude","latitude")],eg[,3:4],proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
sp.lines <- points_to_line(data = eg,
long = "longitude",
lat = "latitude",
sort_field = "PointNum")
proj4string(sp.lines) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
## Using tmap package for points with labels (sp object)
myTmap <- tm_shape(sp.point) + tm_dots(col = "red", size = 5) + tm_text("PointNum") +
tm_shape(sp.lines) + tm_lines(col = "grey80")
print(myTmap)
我在使用 R 中的 tmap 包将标记的位置点与线连接时遇到困难。
Session 信息:
R version: 3.4.3 (2017-11-30)
Package versions: maptools_0.9-2 maps_3.2.0 stringr_1.2.0 bindrcpp_0.2 tmap_1.11 sp_1.2-7 dplyr_0.7.4 plyr_1.8.4 ggmap_2.6.1 ggplot2_2.2.1
我的目标是能够绘制轨迹,然后在保存后将其可视化,使用带有 save_tmap
功能的交互选项。
我可以绘制位置并标记它们,或者只绘制线条:
library(ggmap)
library(plyr)
library(dplyr)
library(sp)
library(tmap)
library(maps)
eg <- data.frame(longitude = c(-38.04434, -38.07048, -38.38147, -38.98167, -39.51479, -39.68861, -40.10173, -40.69829, -41.06727, -41.45114),
latitude = c(-54.00679, -53.98419, -53.82259, -53.73171, -53.77248, -53.78981, -53.71934, -53.64412, -53.53544, -53.51021),
PointNum=1:10,track=1)
head(eg,2)
# Create spatial points data frame
sp.point <- SpatialPointsDataFrame(eg[,c("longitude","latitude")],eg[,3:4],proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
## Base plot (raw data)
plot(latitude~longitude, data=sp.point, asp=1, type="l") ## Note, asp helps with lat/long plots.
text(latitude~longitude, labels=PointNum, data=eg, cex= 0.7)
## Base plot (sp object)
plot(sp.point)
## Using tmap package for points with labels (sp object)
track.point <- tm_shape(sp.point) + tm_dots("red") +tm_text("PointNum")
track.point
## Create spatial lines object
## Use the 'points_to_line' function from this post:
https://rpubs.com/walkerke/points_to_line
head(eg,2)
sp.lines <- points_to_line(data = eg,
long = "longitude",
lat = "latitude",
sort_field = "PointNum")
proj4string(sp.lines) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
track.line <- tm_shape(sp.lines) +tm_lines()
track.line
## Save as interactive map
getwd()
save_tmap(tm=track.point,filename="track_point.html")
save_tmap(tm=track.line,filename="track_line.html")
## Ideally, I would only want to save one plot which would be a combination of the two above
谁能告诉我如何制作一个交互式图,其中包含:标记的点和连接它们的线?
您需要将 + 运算符的两个 tm_shape
调用加入到单个 tmap 对象。
我正在复制您的示例,其中包含您链接的 RPubs 页面中的 points_to_line
函数,因此它会起作用,并且只打印 tmap
对象而不是保存它;这应该不会影响结果。
这是结果(线条的颜色和点的大小是我选择的)
library(ggmap)
library(plyr)
library(dplyr)
library(sp)
library(tmap)
library(maps)
points_to_line <- function(data, long, lat, id_field = NULL, sort_field = NULL) {
# Convert to SpatialPointsDataFrame
coordinates(data) <- c(long, lat)
# If there is a sort field...
if (!is.null(sort_field)) {
if (!is.null(id_field)) {
data <- data[order(data[[id_field]], data[[sort_field]]), ]
} else {
data <- data[order(data[[sort_field]]), ]
}
}
# If there is only one path...
if (is.null(id_field)) {
lines <- SpatialLines(list(Lines(list(Line(data)), "id")))
return(lines)
# Now, if we have multiple lines...
} else if (!is.null(id_field)) {
# Split into a list by ID field
paths <- sp::split(data, data[[id_field]])
sp_lines <- SpatialLines(list(Lines(list(Line(paths[[1]])), "line1")))
# I like for loops, what can I say...
for (p in 2:length(paths)) {
id <- paste0("line", as.character(p))
l <- SpatialLines(list(Lines(list(Line(paths[[p]])), id)))
sp_lines <- spRbind(sp_lines, l)
}
return(sp_lines)
}
}
eg <- data.frame(longitude = c(-38.04434, -38.07048, -38.38147, -38.98167, -39.51479, -39.68861, -40.10173, -40.69829, -41.06727, -41.45114),
latitude = c(-54.00679, -53.98419, -53.82259, -53.73171, -53.77248, -53.78981, -53.71934, -53.64412, -53.53544, -53.51021),
PointNum=1:10,track=1)
head(eg,2)
# Create spatial points data frame
sp.point <- SpatialPointsDataFrame(eg[,c("longitude","latitude")],eg[,3:4],proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
sp.lines <- points_to_line(data = eg,
long = "longitude",
lat = "latitude",
sort_field = "PointNum")
proj4string(sp.lines) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
## Using tmap package for points with labels (sp object)
myTmap <- tm_shape(sp.point) + tm_dots(col = "red", size = 5) + tm_text("PointNum") +
tm_shape(sp.lines) + tm_lines(col = "grey80")
print(myTmap)