R Plotly - xaxis labels ever other tick, change to all

I am trying to get all the labels to appear on every tick on the x-axis, but as you can see in the picture below they printed everyone tick.

This is the code I currently have

    fig <- plot_ly(width = 1000 )
    fig <- fig %>% 
      add_bars(data=filter(mbt, ANIMAL == 'CAT'),
               x = ~as.Date(paste0("01-",TITLE),format = "%d-%b-%y"), y = ~count, type = 'bar', text = ~count,
               textposition = 'auto', marker = list(color = 'rgb(113,88,143)'), name = "Cat", yaxis = "y2")
    fig <- fig %>% 
      add_bars(data=filter(mbt, ANIMAL == 'DOG'),
               x = ~as.Date(paste0("01-",TITLE),format = "%d-%b-%y"), y = ~count, type = 'bar', text = ~count,
               textposition = 'auto',marker = list(color = 'rgb(65,152,175)'), name = "Dog",yaxis = "y2")
    fig <- fig %>%
      add_lines(data=filter(mbt, ANIMAL == 'CAT'),
                x = ~as.Date(paste0("01-",TITLE),format = "%d-%b-%y"),y=~percentage, line = list(color = 'rgb(113,88,143)'),name = "P - Cats", yaxis ="y")
    fig <- fig %>%
      add_lines(data=filter(mbt, ANIMAL == 'DOG'),
                x = ~as.Date(paste0("01-",TITLE),format = "%d-%b-%y"),y=~percentage,  line = list(color = 'rgb(65,152,175)'),name = "P - Dogs", yaxis ="y")
    fig <- fig %>%
      add_lines(data=filter(mbt, ANIMAL == 'Total'),
                x = ~as.Date(paste0("01-",TITLE),format = "%d-%b-%y"),y=~percentage,  line = list(color = 'rgb(255,0,0)'), name = "Total", yaxis ="y")
    fig <- fig %>%
      add_lines(data=mbt,
                x = ~as.Date(paste0("01-",TITLE),format = "%d-%b-%y"),y=~percent_target, name="Target",yaxis ="y",line = list(color = 'rgb(0, 0, 0)')) 
    fig <- fig %>%
      layout( barmode = 'stack',
              yaxis2 = list(title ="", side = "right", tickprefix="<b>",ticksuffix = "</b>", range=c(0,24000),dtick = 2000, tickformat = "digits",showgrid = FALSE, zeroline = FALSE),
              yaxis = list(title = "", tickformat= ".1f", tickprefix="<b>",ticksuffix = "%</b>", overlaying = "y2",side = 'left',range = c(97,100.01)),
              xaxis = list(title = "", tickmode = "array", tickvals = ~as.Date(paste0("01-",TITLE),format = "%d-%b-%y"), tickformat = "%b-%y",tickfont = list(size = 10), tickprefix="<b>",ticksuffix = "</b>"),
              legend = list(orientation = "h", xanchor="center", x = 0.5),
              margin = list(l = 20, r = 50, b = 10, t = 100),
              annotations=list(text="Cat/Dog",xref="paper",x=0.5,
                               yref="paper",y=1,yshift=100,showarrow=FALSE, 
                               font=list(size=26))) %>%
      config(displayModeBar = FALSE)
    
    fig

Does anyone have any ideas?

Try out combinations of modifying the dtick argument inside the axis definition, or alternative, you can supply custom tick values. The below should be a helpful resource for both of these.

I could not get any to work. If I drag the x-axis so fewer bars on the plot the all the ticks show up. Just can't get them to all show when there are 14 months on the plot.

I'd try changing them from dates to factors.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.