kmm
kmm

Reputation: 51

R highcharter() global thousand separator?

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!

Upvotes: 3

Views: 1021

Answers (3)

Sascha
Sascha

Reputation: 239

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

Upvotes: 2

Rich Pauloo
Rich Pauloo

Reputation: 8392

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

Upvotes: 3

madepiet
madepiet

Reputation: 884

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

Upvotes: 0

Related Questions