3

Is there a way to have my highcharter graph have a thousands separator on the y axis and the tooltip? Not sure if there are separate ways of doing this, or if there is a global option. The tooltip data point is formatted so there is a space where a comma should be. I've googled a bunch and just can't figure this out. Any help is appreciated.

enter image description here

code:

    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

Thanks!

kmm
  • 51
  • 3

3 Answers3

3

The package author jbkunst provides this answer here:

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))

enter image description here

Rich Pauloo
  • 7,734
  • 4
  • 37
  • 69
  • After spending days trying to answer this question, finally found the answer. Thank you! How would I add a currency symbol in front of the number? – Jimmy Cloutier Jun 09 '22 at 03:03
2

You have to set the global options (as mentioned in Rich Pauloo answer)

library(highcharter)
library(magrittr)

# set options
hcoptslang <- getOption("highcharter.lang")
hcoptslang$thousandsSep <- ","
options(highcharter.lang = hcoptslang)

and than set the axis labes as follows:

# 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

That will give you both, the tooltip and the axis:

A screenshot from R showing that now axis and tooltip have separators

Sascha
  • 194
  • 7
  • I just found out that you can format the decimal places by adding `.#f` to `format ="{point.y: ,f .2f}"`. – T-T Jun 13 '22 at 16:45
0

You can use API functions for this and customize this tooltip as you like: https://api.highcharts.com/highcharts/tooltip.formatter

Example:

  hc_tooltip(pointFormat = "<b>{point.name}</b>:<br>",
             formatter = JS("function(){ return point.value * 100 + '%'; }"))

Here you can find an article that may also be helpful explaining how to work with Highcharts JavaScript syntax in R: https://www.highcharts.com/blog/tutorials/working-with-highcharts-javascript-syntax-in-r/?fbclid=IwAR3RJo3gPURMhJCtcs5o8LqAzOhT8EN957Vijo2njd41YocX-oYv0LPijSA

madepiet
  • 859
  • 1
  • 5
  • 7
  • thanks - I've seen this documentation. Do you have any more specific examples that could help me? I don't know JavaScript and am having a hard time understanding. Ideally, there would be some kind of option where the whole chart would have thousand separators. If not, then then thousand separators would suffice. I just can't figure out how to write the arguments in hc_tooltip for that to happen. – kmm May 25 '21 at 16:00