具有深入分析功能的高位图

时间:2019-02-25 23:45:28

标签: r r-highcharter

我与美国各州及其县都有一个数据框,如下所示:

State<-c("Alabama","Alabama","Alaska","Alaska")
StateCode<-c("AL","AL","AK","AK")
County<-c("AUTAUGA","BALDWIN","ANCHORAGE","BETHEL")
CountyCode<-c("AL001","AL003","AK020","AK050")
Murder<-c(5,6,7,8)
d<-data.frame(State,StateCode,County,CountyCode, Num)

我的目标是创建像this highchart map这样的州县下钻地图。在从每个州找到Murder的摘要后,我创建了一个数据框,如下所示:

data("USArrests", package = "datasets")
USArrests <- add_rownames(USArrests, "state")

(我使用USArrests是因为它就像我的原始数据集一样,以用作示例)

然后我使用this highcharter github method来创建向下钻取图

library("highcharter")
library("purrr")
library("dplyr")
library("viridisLite")

data("USArrests", package = "datasets")
data("usgeojson")
data("uscountygeojson")

names(usgeojson)
names(usgeojson$features[[1]])

usgeojson$features <- usgeojson$features %>% 
  map(function(x){
    x$properties$code <- gsub("us-", "", x$properties$code)
    x$properties$name <- x$properties$woename
    x$drilldown <- x$properties[["code"]]
    x$value <- ceiling(abs(rnorm(1)*2))
    x$properties$value <- x$value

    x
  })

names(usgeojson$features[[1]])

names(usgeojson$features[[1]]$properties)
usgeojson$features[[1]]$properties

USArrests <- add_rownames(USArrests, "state")

fn <- JS("function(e){  if (!e.seriesOptions) {  alert(e.point.drilldown) } }")
fn2 <- JS("function () { this.setTitle(null, { text: 'USA' });  }")

highchart(type = "map") %>%
  hc_chart(
    events = list(
      drilldown = fn,
      drillup = fn2
      )
    ) %>% 
  hc_colorAxis(min = 0, minColor = "#FAFAFA", maxColor = "#2D2D2D") %>% 
  hc_series(
    list(
      data = USArrests %>% select(name = state, value = Murder) %>% list_parse(),
      mapData = usgeojson,
      joinBy = "name",
      # data = usgeojson,
      # type = "map",
      name = "USA",
      dataLabels = list(
        enabled = TRUE,
        format = "{point.properties.code}"
      )
    )
  ) %>% 
  hc_drilldown(
    activeDataLabelStyle = list(
      color = '#FFFFFF',
      textDecoration = 'none',
      textShadow = '0 0 3px #000000'
    ),
    drillUpButton = list(
      relativeTo =  'spacingBox',
      position = list(x = 0, y = 60)
    )
  ) 

可以创建状态图,但是没有向下钻取功能,我的假设是因为我的初始数据框与县级数据与新数据之间没有任何联系。有人知道如何使它工作吗?

0 个答案:

没有答案