Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make plotly axes display only integer numbers

Tags:

r

plotly

I have a plotly barplot in a shiny app, showing factor levels on X axis. But on some number of inputs (2,3,4) it shows marks like 1.5, 2.5 etc (see an image). So there is a question: can I somehow make the X axis marks show only integer values?

sample

like image 661
Claud H Avatar asked Jun 06 '17 13:06

Claud H


People also ask

How do you get rid of the grid on Plotly?

How do you remove gridlines in Plotly? Toggling Axis grid lines Axis grid lines can be disabled by setting the showgrid property to False for the x and/or y axis.

How much data can Plotly handle?

Short answer is about 250,000 x-y points.

How do you hide your legend Plotly?

In this example, we are hiding legend in Plotly with the help of method fig. update(layout_showlegend=False), by passing the showlegend parameter as False.


2 Answers

plotly is guessing your axis types since your cluster labels are numeric. You can fix this by coercing your x var to a factor before/when plotting.

library(plotly)
library(dplyr)
mtcars_by_gear <- count(mtcars,gear)

plot_ly(mtcars_by_gear,
        x=~as.factor(gear), 
        y=~n)

If you want further control over axis labels you can use the layout() function's tick arguments, but the factor option seems better for your case.

plot_ly(mtcars_by_gear,
        x=~gear, 
        y=~n, 
        type="bar") %>% 
  layout(xaxis=list(tickvals=~gear,ticktext=~gear))
like image 125
Adam Spannbauer Avatar answered Sep 23 '22 20:09

Adam Spannbauer


tickformat=',d'

Supposing the attributes are the same as in Python where I tested :-), you can set:

layout(xaxis=list(tickformat=',d'))

where the valid values are D3 formats.

See also: https://community.plot.ly/t/restrict-axis-ticks-labels-to-only-show-int-values/3503/4