问题
I have a dataframe with US States and their counties like the one below:
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)
My goal is to create a State-County drill-down map like this highchart map. After finding the summary of Murder
from every State I created a dataframe like the one below:
data("USArrests", package = "datasets")
USArrests <- add_rownames(USArrests, "state")
(I use USArrests
because it is like my orginal dataset in its form in order to use it as example)
Then I use this highcharter github method in order to create the drilldown map
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)
)
)
The state map may be created but there is no drill down ability and my assumption is because there is no connection between my initial dataframe with the county-level data with the new one. Does anybody know how to make it work?
来源:https://stackoverflow.com/questions/54876449/highcharter-map-with-drill-down-ability