定义符号并绘制地图轨迹R的悬浮文本

时间:2018-09-24 17:29:31

标签: r plotly mapbox

我正在使用plotly在地图上绘制数据。该图被初始化为mapbox图:

plt <- plot_mapbox(data = usa) %>% 
  layout(
    plot_bgcolor = '#191A1A', paper_bgcolor = 'white',
    mapbox = list(style = 'dark',
                  zoom = 3.75, 
                  center = list(lat = 38.5, lon = -98.35)),
    margin = list(l = 0, r = 0,
                  b = 0, t = 0,
                  pad = 0),
    showlegend = TRUE)

其中usa是我的数据集,其中包含坐标和一些其他信息。我使用add_fun向情节添加标记:

layerFunc <- function(plotname, person){
  plotname %>% 
    filter(statename %in% areas[people == person, unique(state)]) %>%
    add_markers(
      x = ~LONGITUDE, y = ~LATITUDE, name = tools::toTitleCase(person), size = ~mw,
      visible = 'legendonly',
      showlegend = TRUE)
}

u <- plt
sapply(areas$people, function(z){
  u <<- u %>% add_fun(layerFunc, z)
})

问题1 上面的方法很好,因为我可以在地图上看到标记,并且大小是列(mw)的函数。但是,当我尝试添加时:

text = ~paste(areacodes, statename, sep = '<br />', hoverinfo = 'text'

输出没有任何悬停信息。

问题2 我想在地图上显示其他数据。我正在使用以下代码:

u <- u %>% 
  add_markers(
    data = tsm, 
    x = ~lng, y = ~lat, name = 'TSM Locations', 
    marker = list(symbol = 'triangle-up'), 
    # text = ~Name, hoverinfo = 'text',
    hoverinfo = 'Name', size = 8, opacity = 0.8, visible = 'legendonly'
  )

这不起作用。 triangle-up显示为跟踪名称的一部分,我在地图上看不到任何内容。如果我未定义symbol,则默认为circle,而没有任何悬停信息。我尝试先定义text,然后再定义hoverinfo = 'text',这同样行不通。

我想使用上面的代码(问题2)将多个数据集添加到地图中,但是它们都显示为圆圈,这使得读取地图变得困难。 hoverinfo的缺席令人沮丧,但我可以忍受。

我是否没有向plot_mapboxadd_markers提供正确的信息? R plotly的文档对于必须如何提供参数不是很有帮助。

数据 以下数据只是我实际问题中的一小部分。 usa具有7000行,tsm为20,而areas具有190

# tsm
structure(list(lat = c(29.782069, 45.2168, 33.223887, 32.75041, 
33.428605, 30.143517, 33.361106, 41.664759), lng = c(-95.567965, 
-75.68265, -117.310383, -89.75036, -84.664446, -95.39606, -111.816541, 
-88.20742), Name = c("Brock Hamlin", "David Flake", "Jeremy Wirtjes", 
"Jim Benson", "Matthew Watson", "Hans Ambrosia", "Erik Fulk", 
"Brian Hanneman")), .Names = c("lat", "lng", "Name"), row.names = c(NA, 
-8L), class = c("data.table", "data.frame"))

# areas
dput(areas[, .(people = subregion, state)])
structure(list(people = c("W2", "E3", "E3", "E3", "E3", "E3", 
"E3", "E3", "E3", "E3", "N", "E2", "E2", "E2", "E2", "E2", "E2", 
"E2", "E2", "C5", "C5", "C5", "C3", "E5", "E4", "E4", "MW3", 
"MW3", "MW3", "MW3", "W2", "C2", "C1", "MW1", "MW1", "MW1", "MW1", 
"MW1", "MW1", "MW1", "MW1", "MW1", "S1", "S1", "S1", "S1", "S1", 
"S1", "W2", "W2", "W2", "W2", "W3", "W3", "W3", "W3", "W3", "W3", 
"W3", "C4", "E1", "D"), state = c("California", "Connecticut", 
"Delaware", "Maine", "Massachusetts", "New Hampshire", "New Jersey", 
"New York", "Rhode Island", "Vermont", "Alaska", "Alabama", "Georgia", 
"Mississippi", "North Carolina", "South Carolina", "Tennessee", 
"Virginia", "West Virginia", "New Brunswick", "New Foundland", 
"Nova Scotia", "Saskatchewan", "Pennsylvania", "Maryland", "Washington DC", 
"Indiana", "Kentucky", "Michigan", "Ohio", "Nevada", "Alberta", 
"Alberta", "Illinois", "Minnesota", "Wisconsin", "Iowa", "Kansas", 
"Missouri", "Nebraska", "North Dakota", "South Dakota", "Texas", 
"Arkansas", "Oklahoma", "Louisiana", "Texas", "Texas", "California", 
"Oregon", "Washington", "Nevada", "Arizona", "Colorado", "New Mexico", 
"Utah", "Idaho", "Montana", "Wyoming", "Ontario", "Florida", 
"Puerto Rico")), .Names = c("people", "state"), row.names = c(NA, 
-62L), class = c("data.table", "data.frame"))

# usa
structure(list(LATITUDE = c(39.866944, 42.542949, 33.794848, 
45.600297, 34.839982, 30.37644, 40.439304, 70.34446, 38.191704, 
39.810329, 47.785648, 39.5293, 43.67946, 32.607693, 27.76736, 
40.7825, 40.648611, 41.809461, 42.383176, 28.049116), LONGITUDE = c(-77.685833, 
-70.928314, -118.235332, -93.206995, -79.741256, -83.18064, -74.344391, 
-150.927127, -82.605235, -75.428115, -116.921093, -87.42355, 
-85.48363, -85.097016, -91.51609, -73.8964, -74.020833, -72.254281, 
-87.814198, -81.922944), areacodes = c("717", "351,978", "323", 
"612,763", "910", "386", "848,732", "907", "606", "610", "208", 
"812", "231", "334", "not found", "718", "718,917,347,929", "not found", 
"847,224", "863"), statename = c("Pennsylvania", "Massachusetts", 
"California", "Minnesota", "North Carolina", "Florida", "New Jersey", 
"Alaska", "Kentucky", "Pennsylvania", "Idaho", "Indiana", "Michigan", 
"Alabama", "NA", "New York", "New York", "Connecticut", "Illinois", 
"Florida")), .Names = c("LATITUDE", "LONGITUDE", "areacodes", 
"statename"), row.names = c(NA, -20L), class = c("data.table", 
"data.frame")

0 个答案:

没有答案