我很难使用 R 中的 tmap 包将标记的位置点与线连接起来。
会话信息:
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
谁能告诉我如何制作一个包含以下内容的交互式绘图:标记的点以及连接它们的线?