Mukesh Kumar Singh
Mukesh Kumar Singh

Reputation: 653

How to fix width of bar in plotly R

Plotly is one of the most useful plotting packages in R, but I am facing a very basic issue with it.

When I am drawing a barplot , it looks ok in a small screen, but as soon as I resize the window , it covers the whole window, making it ugly.

As my requirement is dynamic , number of bars on the plot changes, but I want to keep the bar width look decent.

enter image description here

But when it gets resized

the code which I am using to plot the barplot is simple and I hope need not be explained . The width option doesnt make any impact on the graph.

Am I missing anything?

 ds <- as.data.frame(matrix(c('some_name',2300),nrow = 1,ncol=2))
colnames(ds) <- c('name','value')

plot_ly(ds,x=name,y=value,type='bar') %>% layout(width = 0.1)

Upvotes: 8

Views: 10312

Answers (4)

DunderChief
DunderChief

Reputation: 726

If you want to change the width of the bars, just use the width argument inside of add_trace or add_bars

ds <- data.frame(name = "some_name", value = 2300)
plot_ly(ds) |>
  add_trace(x=~name, y=~value, type = 'bar', width = .1)

If you are looking to change width of the plot itself, add width inside the plot_ly function

ds <- data.frame(name = "some_name", value = 2300)

plot_ly(ds, width = 200) |>
  add_bars(x=~name, y=~value, type = 'bar', width = .1)

Upvotes: 0

Chaitanya Nekkalapudi
Chaitanya Nekkalapudi

Reputation: 1077

There is no such option called bar size in ploty, but you can use bargap option in the layout.

Upvotes: 2

amberv
amberv

Reputation: 251

Yes, you need to set the autosize=F. So correctly it should be :

plot_ly(ds,x=name,y=value,type='bar') %>% layout(width = 100, autosize=F)

Another option is to set width and height in the plotlyOutput (keeping the autosize=T): plotlyOutput("plot", height="700px", width="200px")))

Upvotes: 1

Feng Jiang
Feng Jiang

Reputation: 1945

I could not find the answer either, but I found an alternative solution, which is to use dot plot, instead of bar chart.

See: https://plot.ly/r/dot-plots/

Upvotes: 0

Related Questions