How to configure thousand separator for hovertext in R plotly?

1k Views Asked by At

It looks like a simple task, but after hours looking aroung I'm not able to find a suitable solution. I'm using plotly to plot line and bar charts, and I have trouble formatting hover numbers. I want the following :

  • decimal separator ','
  • thousand separator ' '

I'm able to setup the decimal separator by setting the locale to FR. I'm also able to get space separator for the ticklabeld. However I'm not able at all to add space as thousand separator.

I would like to find an elegant solution by setting hovertemplate = '%{x} | %{y:.0f}' , but any other solution would fit.

This is the code for my plot :

data <- EMPL1019[,c(1,2,4)]
colnames(data) <- c('Date','Salaries','Equivalent')

ticklabels <- seq(from=0, to=round(max(data$Salaries)), by=5000)
ticktexts <- c(0,paste(ticklabels[-1]/1000, " 000", sep=""))

EMP.g1 <- plot_ly(data = data,
              name = 'Salariés',
              line = list(color = ispfPalette[1]),
              x = ~Date,
              y = ~Salaries,
              hovertemplate = '%{x} | %{y:.0f}<extra></extra>',
              type = 'scatter',
              mode = 'line')
EMP.g1 <- EMP.g1 %>% add_trace(y = ~Equivalent,
                       hovertemplate = '%{x} - %{y:.2f}<extra></extra>',
                       name = 'Equ. temps plein',
                       line = list(color = ispfPalette[9]))
EMP.g1 <- EMP.g1 %>% layout(yaxis = list(separatethousands = T,tickformat = '.0f'))
EMP.g1 <- EMP.g1 %>% layout(yaxis=list(tickvals = ticklabels,
                                       ticktext = ticktexts))
EMP.g1 <- EMP.g1 %>% layout(yaxis = yaxis_template, 
                            xaxis = xaxis_template,
                            legend = list(orientation = 'h'))
EMP.g1 <- EMP.g1 %>% config(locale = 'fr')

And this is a sample of my data :

structure(list(Date = structure(c(1635724800, 1633046400, 1630454400, 
1627776000, 1625097600, 1622505600, 1619827200, 1617235200, 1614556800, 
1612137600, 1609459200, 1606780800, 1604188800, 1601510400, 1598918400, 
1596240000, 1593561600, 1590969600, 1588291200, 1585699200, 1583020800, 
1580515200, 1577836800, 1575158400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), `Nombre de salariés` = c(67124L, 66347L, 
65629L, 66937L, 66503L, 65780L, 64963L, 64820L, 64320L, 63978L, 
64320L, 65139L, 64726L, 64824L, 64349L, 64252L, 63802L, 63302L, 
60612L, 58449L, 66069L, 66680L, 66934L, 67530L), `Masse salariale` = structure(c(0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000938992338891789, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000909115204071465, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000088773284923459, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000896660284134519, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000906967890625601, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000916330649236409, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000889642613151166, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000854022123447163, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000855530315648643, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000848916147880622, 
0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008974157736486, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110317720806686, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000870705472791466, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000859874685909506, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000857318642824282, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000085576196400845, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000840277121133498, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000843657217346959, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000082555304014476, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000801471849889434, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000900019006376449, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000904107403400091, 
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000979945565570618, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000116166151318982
), class = "integer64"), `Effectif équivalent temps plein` = c(58126.7732710957, 
57206.3988279215, 55259.2352879171, 55479.1570677117, 56688.511680619, 
55841.9537494603, 54526.0767610181, 53368.5979797748, 53134.6513287857, 
52946.3782282233, 53251.3191244597, 54829.7587531314, 53915.6804815763, 
54306.2860265348, 54170.5949118788, 53649.4815305165, 52445.0726773908, 
51356.6072581076, 50193.6687158648, 48360.6793136537, 56294.0759669762, 
57709.3582162786, 57246.9744898572, 58543.1814151947)), class = c("data.table", 
"data.frame"), row.names = c(NA, -24L), .internal.selfref = <pointer: 0x000001d4019765b0>)

I can't trust that there is no easy way to do it. Even tickvals and ticktext solution looks overcomplex and I'm sure there is a better way to do it.

Thanks in advance for your help !

1

There are 1 best solutions below

3
On

It works, but it's not native Plotly. What I did was add the parameter hovertext, I then used this to format the values you used in y = Salaries and y = Equivalent. Then I replaced y in your hovertemplate with hovertext.

(I've only commented out the palettes because I don't have the data for these objects.)

EMP.g1 <- plot_ly(data = data,
                  name = 'Salariés',
                  line = list(color = 'darkred'), #ispfPalette[1]),
                  x = ~Date,
                  y = ~Salaries,
                  hovertext = ~formatC(Salaries, big.mark = " ",
                                       format = "d"),   # integer (no decimal)
                  hovertemplate = '%{x} | %{hovertext}<extra></extra>',
                  type = 'scatter',
                  mode = 'line')
EMP.g1 <- EMP.g1 %>% 
  add_trace(y = ~Equivalent,
            hovertext = ~formatC(Equivalent, 
                                 big.mark = " ", # space separator
                                 format = "d"),  # integer (no decimal)
            hovertemplate = '%{x} - %{hovertext}<extra></extra>',
            name = 'Equ. temps plein',
            line = list(color = 'black')) #ispfPalette[9])) 

The rest of your code is unchanged.

enter image description here enter image description here