Black Lives Matter. Please consider donating to Black Girls Code today.

Bar width in bar charts not changing

Hi,

I am new to Plotly. I would like to make bar charts with very thin bars (almost vertical lines). I was hoping the bar width can be changed using the “width” attribute. However, the bar width seems to remain unchanged by changing the width. My R code looks like the following:

x<-c(300, 500, 600, 800, 850, 900, 950, 1000)
y<-c(10, 20, 0, 100, 0, 70, 0, 30)
data <- data.frame(x, y)
p <- plot_ly(data, x = ~x, y = ~y, type = ‘bar’,width = 1)
chart_link = plotly_POST(p, filename=“bar_text”)
chart_link

The chart looks like the following whether I use:
“p <- plot_ly(data, x = ~x, y = ~y, type = ‘bar’,width = 1)”
or
"p <- “plot_ly(data, x = ~x, y = ~y, type = ‘bar’,width = 100)”:

Just wondering if I’m missing anything or if there is another way to change the bar width. Thanks very much!

Hey @pkhatra,

use as a percentage between 0 and 1:

p <- plot_ly(data) %>%
  add_trace(x = ~x, y = ~y, type = 'bar', width = .05) %>%
  add_trace(x = ~x, y = ~y, type = 'bar', width = .15) %>%
  add_trace(x = ~x, y = ~y, type = 'bar', width = .2) %>%
  layout(bargap = 0.4)
1 Like

That works! Thanks @bcd!!

Is there anyway to give a bar graph absolute width? Especially in terms of time/dates?