R:如何始终在 Plotly Choropleth 地图上显示美国州名?

R: How to display US State names ON a Plotly Chloropleth Map at all times?

我创建了一个非常简单的 U.S。使用 plotly 的 plot_geo() 函数进行映射,并希望将标签的永久显示添加到各州。我至少花了半天时间搜索 Stack Overflow、Plotly 文档甚至 Stack Exchange GIS,我找到的最接近的解决方案是这个问题,,但讨论的解决方案是针对 Python,不是 R.

听起来我需要为每个州访问 plotly 内置的中心纬度和经度点,并向这些点添加文本或注释文本,但我找不到如何访问 plotly 内置的中心纬度和经度长点。我走在正确的轨道上吗?或者还有其他方法吗?

我当前的代码:

#In R (not Python)
state_ISO <- c("US-AL", "US-AK", "US-AZ", "US-AR", "US-CA", "US-CO", "US-CT", "US-DE", "US-FL", "US-GA", "US-HI", "US-ID", "US-IL", "US-IN", "US-IA", "US-KS", "US-KY", "US-LA", "US-ME", "US-MD", "US-MA", "US-MI", "US-MN", "US-MS", "US-MO", "US-MT", "US-NE", "US-NV", "US-NH", "US-NJ", "US-NM", "US-NY", "US-NC", "US-ND", "US-OH", "US-OK", "US-OR", "US-PA", "US-RI", "US-SC", "US-SD", "US-TN", "US-TX", "US-UT", "US-VT", "US-VA", "US-WA", "US-WV", "US-WI", "US-WY", "US-DC")
state_ISO2 <- c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY", "DC")
sate_name <- c("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming", "District of Columbia")
state_name_lower <- c("alabama", "alaska", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "florida", "georgia", "hawaii", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts", "michigan", "minnesota", "mississippi", "missouri", "montana", "nebraska", "nevada", "new hampshire", "new jersey", "new mexico", "new york", "north carolina", "north dakota", "ohio", "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina", "south dakota", "tennessee", "texas", "utah", "vermont", "virginia", "washington", "west virginia", "wisconsin", "wyoming", "district of columbia")
type <- c("state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "state", "district")
regions_census_main <- c("South", "West", "West", "South", "West", "West", "Northeast", "South", "South", "South", "West", "West", "Midwest", "Midwest", "Midwest", "Midwest", "South", "South", "Northeast", "South", "Northeast", "Midwest", "Midwest", "South", "Midwest", "West", "Midwest", "West", "Northeast", "Northeast", "West", "Northeast", "South", "Midwest", "Midwest", "South", "West", "Northeast", "Northeast", "South", "Midwest", "South", "South", "West", "Northeast", "South", "West", "South", "Midwest", "West", "South")
regions_census_main_value <- c(3, 1, 1, 3, 1, 1, 2, 3, 3, 3, 1, 1, 4, 4, 4, 4, 3, 3, 2, 3, 2, 4, 4, 3, 4, 1, 4, 1, 2, 2, 1, 2, 3, 4, 4, 3, 1, 2, 2, 3, 4, 3, 3, 1, 2, 3, 1, 3, 4, 1, 3)
center_latitude <- c(32.7794, 64.0685, 34.2744, 34.8938, 37.1841, 38.9972, 41.6219, 38.9896, 28.6305, 32.6415, 20.2927, 44.3509, 40.0417, 39.8942, 42.0751, 38.4937, 37.5347, 31.0689, 45.3695, 39.0550, 42.2596, 44.3467, 46.2807, 32.7364, 38.3566, 47.0527, 41.5378, 39.3289, 43.6805, 40.1907, 34.4071, 42.9538,, 35.5557, 47.4501, 40.2862, 35.5889, 43.9336, 40.8781, 41.6762, 33.9169, 44.4443, 35.8580, 31.4757, 39.3055, 44.0687, 37.5215, 47.3826, 38.6409,, 44.6243, 42.9957, 38.9101)
center_longitude <- c(-86.8287, -152.2782, -111.6602, -92.4426, -119.4696, -105.5478, -72.7273, -75.5050, -82.4497, -83.4426, -156.3737, -114.6130, -89.1965, -86.2816, -93.4960, -98.3804, -85.3021, -91.9968, -69.2428, -76.7909, -71.8083, -85.4102, -94.3053, -89.6678, -92.4580, -109.6333, -99.7951, -116.6312, -71.5811, -74.6728, -106.1126, -75.5268, -79.3877, -100.4659, -82.7937, -97.4943, -120.5583, -77.7996, -71.5562, -80.8964, -100.2263, -86.3505, -99.3312, -111.6703, -72.6658, -78.8537, -120.4472, -80.6227, -89.9941, -107.5512, -77.0147)

dataus <- as.data.frame(cbind(sate_ISO, sate_ISO2, state_name, state_name_lower, type,
regions_census_main, regions_census_main_value,
center_latitude, center_longitude), stringsAsFactors = FALSE)

g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = toRGB('white')
)

plot_geo(dataus, locationmode = 'USA-states') %>%
  add_trace(
    z = ~regions_census_main_value, locations = ~state_ISO2,
    color = ~regions_census_main_value, colors = 'Blues'
  ) %>%
  colorbar(title = "Regions") %>%
  layout(
    title = 'United States by Regions',
    geo = g
  )

到目前为止,我的代码几乎与 Plotly 文档相同(https://plotly.com/r/choropleth-maps/ - “自定义等值线图”部分)。

编辑: 在他们的文档中(“Choropleth Inset Map”部分),您似乎可以通过附加 add_text() 参数来添加永久文本。我在上面的数据代码中添加了中心纬度和经度,并修改了 Plotly add_text() 代码以满足我的情况。

add_text(
    x = ~center_longitude, 
    y = ~center_latitude, 
    text = ~state_name, 
    showlegend = False,
  )

不幸的是,这与上面的代码没有任何不同,我现在收到一条警告消息:

Warning Message: 
'choropleth' objects don't have these attributes: 'x', 'y', 'mode'
Valid attributes include:
'type', 'visible', 'legendgroup', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'locations', 'locationmode', 'z', 'geojson', 'featureidkey', 'text', 'hovertext', 'marker', 'selected', 'unselected', 'hoverinfo', 'hovertemplate', 'showlegend', 'zauto', 'zmin', 'zmax', 'zmid', 'colorscale', 'autocolorscale', 'reversescale', 'showscale', 'colorbar', 'coloraxis', 'geo', 'idssrc', 'customdatasrc', 'metasrc', 'locationssrc', 'zsrc', 'textsrc', 'hovertextsrc', 'hoverinfosrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

或者,解决方案可能是使指定的 hover/interactive 文本成为静态的选项。通过将 text = ~state_name 添加到 add_trace(),我可以告诉它显示什么作为 hover/interactive 文本(在 https://plotly.com/r/choropleth-maps/ 中找到类似的示例)。是否碰巧有一个选项可以使该文本静态而不是动态?

所以我想出了如何添加永久标签。该解决方案并不完美,但它确实满足了我在上面 post 中的要求。

使用 post 中编码的相同数据,我不得不从使用 plot_geo() 更改为原来的 plot_ly(),然后添加 2 个 add_trace() 函数,其中一个是类型设置为“choropleth”,另一个类型设置为“scattergeo”。它是允许您添加所需标签的“scattergeo”跟踪。

plot_ly(dataus2, type = "choropleth", locationmode = 'USA-states') %>%
    layout(geo = g, title = "United States by Census Major Regions") %>%
    add_trace(type = "choropleth", 
              z = ~regions_census_main_value, locations = ~state_ISO2,
              color = ~regions_census_main_value, autocolorscale = FALSE,
              colorscale = 'Blues', showscale = FALSE,
              text = ~state_name)  %>%
    colorbar(title = "Regions") %>%
    add_trace(type = 'scattergeo',
              locations = ~state_ISO2, text = ~state_ISO2, 
              mode = "text", 
              textfont = list(color=rgb(0,0,0), size = 12),
              showlegend = FALSE) 

不幸的是,它没有使用与 plot_geo 相同的蓝调颜色范围,并且色标以某种方式反转,但可以将状态标签添加到 ploty_ly 地图。

请注意,我选择使用州代码而不是完整的州名称,plot_ly 标签的自动定位使得在较小的州使用完整的州名称有点混乱。

如果您想扩展显示的永久文本,请将 text = ~state_ISO2 替换为 text = paste0(dataus2$state_ISO2, "\n", dataus2$regions_census_main_value) 之类的内容。 (我从@Skaqqs 的回答中得到了这个提示。)仅供参考,“\n”是“换行符”的正则表达式代码。

有关如何将其应用于世界地图的示例,请参阅