1

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

ラベルが付けられ、それらを結ぶ線を持つ点を含む単一のインタラクティブなプロットを作成する方法を誰かに教えてもらえますか?

4

1 に答える 1