Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change thousands separator for blank in R plotly?

Tags:

r

graphics

plotly

I would like to change the big number separator from comma to blank in plotly. I can do this in ggplot2, but not in plotly. It should probably be done through tickformat option (https://plotly.com/r/reference/#layout-xaxis-tickformat), but after trying some d3-format as specified here : https://github.com/d3/d3-format/blob/master/README.md I don't get it.

For exemple, in the following graph :

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

I don't want to have "50K", "40K" on the y axis, but "50 000", "40 000".

If I add

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

I get "50,000", "40,000" ...

If I add

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

I get "50000","40000"...

But I can't get a blank space separator.

layout(yaxis=list(tickformat=","))
like image 227
Malta Avatar asked Oct 17 '25 14:10

Malta


1 Answers

Try this approach formating labels and text with tickvals and ticktext in layout. You can create your breaks with a sequence and then formating it to have the desired outcome. Here the code:

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

Output:

enter image description here

like image 193
Duck Avatar answered Oct 20 '25 04:10

Duck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!