在标记的位置点之间添加线条

时间:2018-01-23 11:34:44

标签: r tmap

我很难使用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

是否有人能够向我展示如何制作一个包含以下内容的交互式地块:标记的点和连接它们的线?

1 个答案:

答案 0 :(得分:0)

您需要通过+运算符将两个tm_shape调用连接到单个tmap对象。

我正在复制您的示例,其中包含来自您链接的RPubs页面的points_to_line函数,因此它将起作用,只打印tmap对象而不是保存它;这不应该影响结果。

这是结果(线的颜色和点的大小是我的选择) enter image description here

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)