Home > Software engineering >  How to resize the width of the bar graph in R plotly
How to resize the width of the bar graph in R plotly

Time:04-22

How can I ensure that the 'gap' between 1st and 3rd Jan is equal to the size of the bar in 1st Jan?

The current plot is misleading to readers as the gap for 2 Jan should be as wide as the bar in 1st Jan.

library(plotly)
library(lubridate)

dat  = data.frame(a =c( dmy("1/1/22") , dmy("3/1/22"),dmy("6/1/22")) , b = c(1,1,4))

> dat
           a b
1 2022-01-01 1
2 2022-01-03 1
3 2022-01-06 4

plot_ly(dat , x = ~a, y = ~b , type = "bar")

 

enter image description here

CodePudding user response:

You can set bargap (between or equal to 0 and 1) in layout() to adjust the gap between bars.

dat %>%
  plot_ly(x = ~a, y = ~b, type = "bar") %>%
  layout(bargap = 0.5)

enter image description here

  • Related