有没有办法让我的 highcharter 图表在 y 轴和工具提示上有千位分隔符?不确定是否有单独的方法可以做到这一点,或者是否有全局选项。工具提示数据点的格式已设置为逗号所在的位置有一个空格。我用谷歌搜索了很多,但无法弄清楚这一点。如有任何帮助,我们将不胜感激。
代码:
hc <- jobs %>% hchart(
'line', hcaes(x = new_time, y = employment), color = "#34657F") %>%
hc_title(
text = "Virginia",
align="left",
style = list(fontFamily = "Montserrat")) %>%
hc_subtitle(text= "Employment (in thousands) since March 2018 <br>",
align="left",
style = list(fontFamily = "Montserrat")) %>%
hc_xAxis(title = list(enabled = FALSE)) %>%
hc_yAxis(title = list(enabled = FALSE)) %>%
hc_chart(style = list(fontFamily = "Open Sans"))
hc
谢谢!
软件包作者 jbkunst 在这里提供了这个答案:
library(highcharter)
library(magrittr)
# set options
hcoptslang <- getOption("highcharter.lang")
hcoptslang$thousandsSep <- ","
options(highcharter.lang = hcoptslang)
# plot with commas
highchart() %>%
hc_add_series(data = round(rnorm(100)*10000))
您必须设置全局选项(如 Rich Pauloo 答案中所述)
library(highcharter)
library(magrittr)
# set options
hcoptslang <- getOption("highcharter.lang")
hcoptslang$thousandsSep <- ","
options(highcharter.lang = hcoptslang)
然后按如下方式设置轴标签:
# plot with commas and axis
highchart() %>%
hc_add_series(data = round(rnorm(100)*10000))%>%
hc_yAxis(title = list(text = "YourAxis"), #new part for axis labelling
labels=list(format="{value:,f}")) #new part for axis separator
这将为您提供工具提示和轴:
您可以使用 API 函数来实现此目的,并根据需要自定义此工具提示: https://api.highcharts.com/highcharts/tooltip.formatter
示例:
hc_tooltip(pointFormat = "<b>{point.name}</b>:<br>",
formatter = JS("function(){ return point.value * 100 + '%'; }"))
在这里您可以找到一篇文章,它也可能有助于解释如何在 R 中使用 Highcharts JavaScript 语法:https://www.highcharts.com/blog/tutorials/working-with-highcharts-javascript-syntax-in- r/?fbclid=IwAR3RJo3gPURMhJCtcs5o8LqAzOhT8EN957Vijo2njd41YocX-oYv0LPijSA
感谢分享这个答案,在空白分隔符中停留了几天后,终于完成了这个案例