How to make plot axes only display integers

I have a graphic stroke in a shiny app showing the levels of factors on the X-axis. But on a number of inputs (2,3,4), it shows marks like 1.5, 2.5, etc. (See Image). So the question is, can I somehow make the X-axis labels only integer values?

sample

+3


source to share


2 answers


plotly

guesses the types of your axis since the cluster labels are numeric. You can fix this by coercing your x var to a pre / post ratio. In the meantime, there is no need to know about it. โ€

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

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

      



If you need more control over the axis labels, you can use the arguments layout()

to the tick function, but the coefficient option seems better for your case.

plot_ly(mtcars_by_gear,
        x=~gear, 
        y=~n, 
        type="bar") %>% 
  layout(xaxis=list(tickvals=~gear,ticktext=~gear))

      

+4


source


tickformat=',d'

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

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

      



where D3 formats are valid.

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

+2


source







All Articles