我无法使用Highcharter在R中重新创建this answer,以使条形图中的条形成可点击的网址。答案Here is the Javascript code。 Highcharter有vignette关于重新创建我试图遵循的Javascript。这是到目前为止所尝试的内容。它没有显示任何条形。
library(tidyverse)
library(highcharter)
highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = "Click points to go to URL") %>%
hc_xAxis(type = "category") %>%
hc_plotOptions(series = list(cursor = "pointer"),
point =
list(events = list(
click = JS(
"function () {
location.href = 'https://en.wikipedia.org/wiki/' +
this.options.key;
}"
)
))) %>%
hc_series(
list(name = "USA", key = "United_States", y = 29.9),
list(name = "Canada", key = "Canada", y = 71.5),
list(name = "Mexico", key = "Mexico", y = 106.4)
)
答案 0 :(得分:2)
安德鲁,
你有一些(2)错误复制了这个例子:
point
参数与cursor
参数中series
的深度相同。您的代码的固定版本是:
highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = "Click points to go to URL") %>%
hc_xAxis(type = "category") %>%
hc_plotOptions(
series = list(
cursor = "pointer",
point = list(
events = list(
click = JS( "function () { location.href = 'https://en.wikipedia.org/wiki/' + this.options.key; }")
)
)
)
) %>%
hc_series(
list(
data = list(
list(name = "USA", key = "United_States", y = 29.9),
list(name = "Canada", key = "Canada", y = 71.5),
list(name = "Mexico", key = "Mexico", y = 106.4)
)
)
)
添加数据的更好版本是:
dat <- data.frame(
country = c("USA", "Canada", "Mexico"),
url = c("United_States", "Canada", "Mexico"),
value = c(29.9, 71.5, 106.4)
)
highchart() %>%
hc_xAxis(type = "category") %>%
hc_plotOptions(
series = list(
cursor = "pointer",
point = list(
events = list(
click = JS( "function () { location.href = 'https://en.wikipedia.org/wiki/' + this.options.key; }")
)
)
)
) %>%
hc_add_series(data = dat, type = "column", mapping = hcaes(name = country, key = url, y = value))
希望它的帮助