【问题标题】:how to change thousands separator for blank in R plotly?如何更改 R 中空白的千位分隔符?
【发布时间】:2020-09-23 09:27:30
【问题描述】:

我想在情节中将大数字分隔符从逗号更改为空白。我可以在 ggplot2 中执行此操作,但不能在 plotly 中执行此操作。它可能应该通过 tickformat 选项 (https://plotly.com/r/reference/#layout-xaxis-tickformat) 来完成,但是在尝试了此处指定的一些 d3 格式后:https://github.com/d3/d3-format/blob/master/README.md 我不明白。

例如,在下图中:

diamonds %>%
plot_ly(x=~clarity,y=~~depth*10000) %>%
add_bars()

我不想在 y 轴上有“50K”、“40K”,而是“50 000”、“40 000”。

如果我添加

layout(yaxis=list(tickformat=","))

我得到“50,000”、“40,000”...

如果我添加

layout(yaxis=list(tickformat=" "))

我得到“50000”、“40000”...

但我无法获得空格分隔符。

layout(yaxis=list(tickformat=","))

【问题讨论】:

    标签: r graphics plotly


    【解决方案1】:

    尝试这种方法,在layout 中使用tickvalsticktext 格式化标签和文本。您可以使用序列创建中断,然后对其进行格式化以获得所需的结果。代码如下:

    library(plotly)
    #Labels
    ticklabels <- seq(from=0, to=round(max(diamonds$depth*10000)), by=10000)
    ticktexts <- c(0,paste(ticklabels[-1]/1000, " 000", sep=""))
    #Code
    diamonds %>%
      plot_ly(x=~clarity,y=~~depth*10000) %>%
      add_bars() %>%
      layout(yaxis=list(tickvals = ticklabels,
                        ticktext = ticktexts
                        ))
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 2021-08-09
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      相关资源
      最近更新 更多