highcharts R中的HighchartMap,带有Map点序列和标记聚类模块

0vvn1miw  于 9个月前  发布在  Highcharts
关注(0)|答案(1)|浏览(75)

我想在R中做一个Map,当缩小时,它会聚集点。我使用highcharter包。
这是一个截图,我的目标,从这个演示。

在Highcharts中添加此功能的模块称为marker-clusters,我可以使用hc_add_dependency()加载。但是我在弄清楚为这个添加的模块设置值的语法时遇到了麻烦。我一直在尝试使用marker-clusters模块的an example jsFiddle,但我不确定如何将JS语法转换为R。
这是我目前所拥有的,尽管它根本没有呈现聚类。它正确显示Map点和标签,但放大/缩小不会转换为带有计数的群集气泡。

library(highcharter)

hcmap("custom/world-highres2", showInLegend = FALSE) |>
  hc_add_dependency("modules/marker-clusters.js") |> 
  hc_add_series(
    data = data, 
    type = "mappoint",
    name = "Research groups"
  ) |>
  hc_plotOptions(cluster = list(
    enabled = TRUE,
    allowOverlap = TRUE,
    animation = list(duration = 450),
    layoutAlgorithm = list(type = "grid",
                           gridSize = 10),
    zones = c(list(from = 1, to = 4, marker = list(radius = 13))))) |> 
  hc_mapNavigation(enabled = TRUE)

这是我得到的输出:

我发现了一些使用hc_add_dependency()的R示例,看起来来自所添加模块的任何附加选项都可以作为附加参数直接包含到hc_add_series()中。但当我尝试时,Map只是完全停止渲染点。因此,我目前尝试将集群选项放在单独的hc_plotOptions()调用中,但这也不起作用。
这是我的位置数据的一个子集,用于data数据集。

data <- structure(list(name = c("University of Vermont", "Imperial College London", 
"University of York", "The Open University, UK", "Texas State University", 
"Duke University", "Manchester Metropolitan University", "Northwestern University", 
"University of Tennessee"), address = c("Burlington, VT 05405, USA", 
"Exhibition Rd, South Kensington, London SW7 2BX, UK", "Heslington, York YO10 5DD, UK", 
"Walton Hall, Kents Hill, Milton Keynes MK7 6AA, UK", "601 University Dr, San Marcos, TX 78666, USA", 
"Durham, NC 27708, USA", "All Saints Building, All Saints, Manchester M15 6BH, UK", 
"633 Clark St, Evanston, IL 60208, USA", "Knoxville, TN 37996, USA"
), lat = c(44.4778528, 51.4988222, 53.9461089, 52.0249295, 29.888411, 
36.0014258, 53.4703485, 42.0564594, 35.9544013), lon = c(-73.1964637, 
-0.1748735, -1.0517718, -0.7081895, -97.938351, -78.9382286, 
-2.2392999, -87.675267, -83.9294564)), row.names = c(NA, -9L), spec = structure(list(
    cols = list(name = structure(list(), class = c("collector_character", 
    "collector")), address = structure(list(), class = c("collector_character", 
    "collector")), lat = structure(list(), class = c("collector_double", 
    "collector")), lon = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x5637c9f857d0>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

以下是我一直试图复制的jsFiddle的设置:

plotOptions: {
  mappoint: {
    cluster: {
      enabled: true,
      allowOverlap: false,
      animation: {
        duration: 450
      },
      layoutAlgorithm: {
        type: 'grid',
        gridSize: 70
      },
      zones: [{
        from: 1,
        to: 4,
        marker: {
          radius: 13
        }
      }, {
        from: 5,
        to: 9,
        marker: {
          radius: 15
        }
      }, {
        from: 10,
        to: 15,
        marker: {
          radius: 17
        }
      }, {
        from: 16,
        to: 20,
        marker: {
          radius: 19
        }
      }, {
        from: 21,
        to: 100,
        marker: {
          radius: 21
        }
      }]
    }
  }
}

注意:我不确定这个子集中是否有足够的点来很好地演示缩小的聚类效果,但我希望有人能告诉我正确的语法,我可以从那里开始。
我想这是一个简单的修复,但我是新的工作与Highcharts在R,任何帮助将不胜感激!

vd2z7a6w

vd2z7a6w1#

**免责声明:下面的答案开始是ChatGPT对我的问题的回答,但我已经完成了具体工作,并将其变成了我自己的。

如何使用ChatGPT:
我在最初的帖子中给了ChatGPT来自jsFiddle的JavaScript,并要求它使用highcharter包将其转换为R代码。它花了几个来回铁了一些扭结,但我只是给它的错误信息,我得到和它自我纠正。这种模糊的跨语言语法转换似乎是代码感知LLM的一个很好的用例。
我缺少的基本内容是用mappoints标志启动plotOptions配置。但还有一些其他的东西可能对其他人有用:

  • hc_add_series()中的dataLabels设置群集圆的计数。
  • hc_tooltip根据用户是否悬停在群集或特定点上来确定悬停行为。
  • hc_colorAxis定义簇渐变颜色方案
hc <- hcmap("custom/world-highres2")
hc |> 
  hc_add_dependency("modules/marker-clusters.js") |>
  hc_title(text = 'Research Organizations') |> 
  hc_add_series(
    type = "mappoint",
    enableMouseTracking = TRUE,
    colorKey = "clusterPointsAmount",
    name = "Organizations",
    color = "#5899E2",
    data = data,
    dataLabels = list(
      enabled = TRUE,
      format = "{point.clusterPointsAmount}",
      allowOverlap = TRUE,
      align = "center",
      verticalAlign = "middle",
      color = "white"
    )
  ) |> 
  hc_mapNavigation(enabled = TRUE) |>
  hc_tooltip(formatter = JS(
    "function () {
      if (this.point.clusteredData) {
          return 'Clustered points: ' + this.point.clusterPointsAmount;
      }
      return '<b>' + this.key + '</b><br>Lat: ' + this.point.lat.toFixed(2) + ', Lon: ' + this.point.lon.toFixed(2);
    }")) |>
  hc_colorAxis(
      min = 1,
      max = 120,
      type = 'linear',  # or 'logarithmic'
      minColor = '#B4B8C5',  # Start color (e.g., white)
      maxColor = '#335C81',  # End color (e.g., dark blue)
      stops = list(
        list(0, '#B4B8C5'),
        list(0.5, '#5899E2'),
        list(1, '#335C81')
      )  # Gradient stops (optional; can be used for multi-color gradient)
    ) |> 
  hc_plotOptions(
    mappoint = list(
      cluster = list(
        enabled = TRUE,
        allowOverlap = FALSE,
        animation = list(duration = 450),
        layoutAlgorithm = list(type = 'grid', gridSize = 70),
        zones = list(
          list(from = 1, to = 4, marker = list(radius = 13)),
          list(from = 5, to = 9, marker = list(radius = 15)),
          list(from = 10, to = 15, marker = list(radius = 17)),
          list(from = 16, to = 20, marker = list(radius = 19)),
          list(from = 21, to = 100, marker = list(radius = 21)),
          list(from = 101, to = 150, marker = list(radius = 24))
        )
      )
    )
  )

  • 图中包含的数据点多于OP中包含的样本子集

相关问题