【问题标题】:Drawing arbitrary lines on a highchart map in R (library highcharter)在 R 中的 highchart 地图上绘制任意线(图书馆 highcharter)
【发布时间】:2019-01-29 11:17:09
【问题描述】:

我正在使用 R 中的 highcharter 包绘制一个 highcharts 地图。我已经添加了一些点(城市),并希望通过使用世界地图坐标绘制一条附加直线来链接它们。

我已经设法绘制直线,首先绘制地图,然后将鼠标悬停在显示绘图坐标的城市上,然后使用上述绘图坐标重新绘制绘图。 (注意:我使用了 PLOT 坐标,而我的目标是直接使用 WORLD MAP 坐标。) 如果你只有一两个城市,那没什么大不了的。但是如果你有 100 个城市/点,那就很烦人了。我想答案会是这样的:Is it possible to include maplines in highcharter maps?

谢谢!

这是我的代码:

library(highcharter)
library(tidyverse)    

# cities with world coordinates
ca_cities <- data.frame(
  name = c("San Diego", "Los Angeles", "San Francisco"),
  lat = c(32.715736, 34.052235, 37.773972), # world-map-coordinates
  lon = c(-117.161087, -118.243683, -122.431297) # world-map-coordinates
)

# path which I create AFTER the first drawing of the map as I get the 
# plot-coordinates when I hover over the cities.
path <- "M669.63,-4963.70,4577.18,-709.5,5664.42,791.88"
# The goal: the path variable above should be defined using the WORLD-
# coordinates in ca_cities and not using the PLOT-coordinates.

# information for drawing the beeline
ca_lines <- data.frame(
  name = "line",
  path = path, 
  lineWidth = 2
)

# construct the map
map <- hcmap("countries/us/us-ca-all", showInLegend = FALSE) %>%
  hc_add_series(data = ca_cities, type = "mappoint", name = "Cities") %>%
  hc_add_series(data = ca_lines, type = "mapline", name = "Beeline", color = "blue") 
map

See picture here

【问题讨论】:

    标签: r r-highcharter


    【解决方案1】:

    几个小时后,我找到了问题的答案。也许有更简单的方法,但我将使用 rgdal-package 发布我的版本。 我们的想法是首先将世界地图坐标转换为特定地图的坐标系 (ESRI),然后从 highcharts 反向转换所有调整:

    library(highcharter)
    library(tidyverse)    
    library(rgdal) # you also need rgdal
    
    # cities with world coordinates
    ca_cities <- data.frame(
      name = c("San Diego", "Los Angeles", "San Francisco"),
      lat = c(32.715736, 34.052235, 37.773972), 
      lon = c(-117.161087, -118.243683, -122.431297) 
    )
    
    # pre-construct the map
    map <- hcmap("countries/us/us-ca-all", showInLegend = FALSE) 
    
    # extract the transformation-info
    trafo <- map$x$hc_opts$series[[1]]$mapData$`hc-transform`$default
    
    # convert to coordinates
    ca_cities2 <- ca_cities %>% select("lat", "lon")
    coordinates(ca_cities2) <- c("lon", "lat")
    
    # convert world geosystem WGS 84 into transformed crs
    proj4string(ca_cities2) <- CRS("+init=epsg:4326") # WGS 84
    ca_cities3 <- spTransform(ca_cities2, CRS(trafo$crs)) # 
    
    # re-transform coordinates according to the additionnal highcharts-parameters
    image_coords_x <- (ca_cities3$lon - trafo$xoffset) * trafo$scale * trafo$jsonres + trafo$jsonmarginX
    image_coords_y <- -((ca_cities3$lat - trafo$yoffset) * trafo$scale * trafo$jsonres + trafo$jsonmarginY)
    
    # construct the path
    path <- paste("M",
                  paste0(paste(image_coords_x, ",", sep = ""), 
                  image_coords_y, collapse = ","), 
                  sep = "")
    
    # information for drawing the beeline
    ca_lines <- data.frame(
      name = "line",
      path = path, 
      lineWidth = 2
    )
    
    # add series
    map <- map %>%
      hc_add_series(data = ca_cities, type = "mappoint", name = "Cities") %>%
      hc_add_series(data = ca_lines, type = "mapline", name = "Beeline", color = "blue") 
    
    map
    

    【讨论】:

      猜你喜欢
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多