我想在使用 highcharter 创建的地图上添加交互式地图点。在我添加这些点之前,一切看起来都很好。
虽然点出现在正确的位置,但颜色和图例完全改变。
这是我的示例代码。
这些是可重现示例的数据框。
#Data frame with patients per state
abbr <- c("AK", "GA", "CA", "CT", "HI", "IA", "KY", "LA", "NJ", "NM", "WA", "UT")
fips <- c("02", "13", "06", "09", "15", "19", "21", "22", "34", "35", "53", "49")
V2 <- c(4, 22, 77, 16, 0, 5, 6, 9, 15, 3, 13, 22)
States<- data.frame(abbr, fips, V2)
#Data frame with center locations
name <- c("UCLA", "Stanford")
lon <- c(-118.26, -122.17)
lat <- c(34.04, 37.43)
VHL_centers <- data.frame(lon, lat, name)
这是代码中可以正常工作的部分。
hcmap("https://code.highcharts.com/mapdata/countries/us/us-all.js", #Construct map
data = States,
name = "Patient number",
value = "V2",
joinBy = c("postal-code", "abbr"),
borderWidth = 1,
borderColor = "darkred",
dataLabels = list(enabled = TRUE, format = "{point.properties.postal-code}"),
tooltip = list()) |>
hc_legend(layout = "horizontal",
verticalAlign = "bottom",
align = "center",
valueDecimals = 0,
valueSuffix = "") |>
hc_title(text = "VHL patients per state") |> #Add title
hc_subtitle(text = "Source: SEER database") |> #Add subtitle
hc_colorAxis(stops = color_stops(colors = viridisLite::inferno(10, begin = 0.1)))
这是我尝试添加中心位置时的代码。
hcmap("https://code.highcharts.com/mapdata/countries/us/us-all.js", #Construct map
data = States,
name = "Patient number",
value = "V2",
joinBy = c("postal-code", "abbr"),
borderWidth = 1,
borderColor = "darkred",
dataLabels = list(enabled = TRUE, format = "{point.properties.postal-code}"),
tooltip = list()) |>
hc_legend(layout = "horizontal",
verticalAlign = "bottom",
align = "center",
valueDecimals = 0,
valueSuffix = "") |>
hc_title(text = "VHL patients per state") |> #Add title
hc_subtitle(text = "Source: SEER database") |> #Add subtitle
hc_colorAxis(stops = color_stops(colors = viridisLite::inferno(10, begin = 0.1))) |> #Change color scale
hc_add_series(data = VHL_centers,
type = "mappoint")
理想情况下,我希望只有单一颜色的点(假设是黑色),只有当我将鼠标悬停在它们上方而不是直接在地图上时才会出现名称。
感谢您的帮助!
解决问题的一个选项是为您的点分配不同的
colorAxis
,例如colorAxis=1
,以便第一个 colorAxis
(又名索引 0
)仍应用于您的地图。
library(highcharter)
hcmap("https://code.highcharts.com/mapdata/countries/us/us-all.js", # Construct map
data = States,
name = "Patient number",
value = "V2",
joinBy = c("postal-code", "abbr"),
borderWidth = 1,
borderColor = "darkred",
dataLabels = list(
enabled = TRUE,
format = "{point.properties.postal-code}"
),
tooltip = list(),
) |>
hc_legend(
layout = "horizontal",
verticalAlign = "bottom",
align = "center",
valueDecimals = 0,
valueSuffix = ""
) |>
hc_title(text = "VHL patients per state") |> # Add title
hc_subtitle(text = "Source: SEER database") |> # Add subtitle
hc_colorAxis(stops = color_stops(
colors = viridisLite::inferno(10, begin = 0.1)
)) |> # Change color scale
hc_add_series(
data = VHL_centers,
type = "mappoint",
colorAxis = 1,
color = "black"
)