plot_ly bar plot bar width increases when there are missing x values

When plotting bar charts with missing data for some x values the bars of the plot are increasing in with to cover multiple missing x values rather than remaining at the correct width ( eg centred ofver a singe integer x value)
How do I keep the width of bars uniform where there are missing columns?

twenty bars each centred above a single x value:

df<-data.frame(x=1:20,y=sample( 1:5,20,replace = T))

df%>%plot_ly(
  x =  ~ x,
  y =  ~ y,
  type = "bar")%>%
  layout(xaxis = list(range = c(0,21)),
         title = "example 1")

This produces wide bars first one spread x= 0-6 second x= 8-14
expected two column same width as in example 1 centred above x=3 and x=11

df[c(3,11),]%>%plot_ly(
  x =  ~ x,
  y =  ~ y,
  type = "bar")%>%
  layout(xaxis = list(range = c(0,21)),
         title = "example 2 column width expands across multiple x values")

Curiously when only a single bar is present it is again the correct width - in this case centred above x=3

df[3,]%>%plot_ly(
  x =  ~ x,
  y =  ~ y,
  type = "bar")%>%
  layout(xaxis = list(range = c(0,21)),
         title = "example 3 correct column width again")

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.