r - 如何在 R 中为悬停文本配置千位分隔符?

标签 r charts plotly r-plotly digit-separator

这看起来是一个简单的任务,但经过几个小时的寻找,我找不到合适的解决方案。 我正在使用plotly 绘制折线图和条形图,但在格式化悬停数字时遇到问题。 我想要以下内容:

  • 小数分隔符“,”
  • 千位分隔符“”

我可以通过将区域设置设置为 FR 来设置小数点分隔符。我还可以获得刻度标签的空格分隔符。 但是我根本无法添加空格作为千​​位分隔符。

我想通过设置hovertemplate = '%{x} | 来找到一个优雅的解决方案%{y:.0f}' ,但任何其他解决方案都适合。

这是我的 plotly 的代码:

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

这是我的数据示例:

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

我不敢相信没有简单的方法可以做到这一点。即使tickvals和ticktext解决方案看起来也过于复杂,我相信有更好的方法来做到这一点。

预先感谢您的帮助!

最佳答案

它可以工作,但它不是原生的 Plotly。我所做的是添加参数 hovertext,然后用它来格式化您在 y = Salariesy = Equivalent 中使用的值。然后我将 hovertemplate 中的 y 替换为 hovertext

(我只注释掉了调色板,因为我没有这些对象的数据。)

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

其余代码保持不变。

enter image description here enter image description here

关于r - 如何在 R 中为悬停文本配置千位分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72267216/

相关文章:

r - 多面ggplot中轴文本的条件格式

删除 R 中的特殊撇号

javascript - Highcharts - 堆积面积图中系列之间的差距

javascript - 如果列只有一项,如何删除标签

javascript - 使用选项卡时调整图表高度大小

python - 在 Plotly Express 散点图中将所有标记设置为相同的固定大小

perl - 存储和访问数据以供将来分析运算符(operator)内部和运算符(operator)之间的可变性

r - 如何在R ggplot2中限制x轴后自动重新计算y轴比例

javascript - 在 Chart.js 中单击饼图上的事件

r - 使用 R/plotly 在轴标题中写入符号摄氏度