plotly / plotly/plotly.R

Is there a way to fix bar height of horizontal bar charts in R Plotly?

Open
#1,993 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

Not too sure if this question is asked before. I have been trying to look for solutions but to no avail.

I am trying to create a shiny dashboard with plotly. The thing is that I have earmarked a fixed space for the chart. And in this chart, it is a horizontal bar charts for many category.

Here is the minimally reproducible code.

library(tidyverse)
library(plotly)

tibble(Category = str_c("Category ", seq(1, 20, 1)),
       Value = runif(20)) %>%
  plot_ly(
    y = ~ Category,
    x = ~ Value,
    type = "bar",
    hoverinfo = "text",
    hovertemplate = ~paste0(
      "Category: ", Category, "<br>",
      "Value: ", Value, 
      "<extra></extra>"
    ),
    marker = list(
      line = list(color = "#000000",
                  width = 1)
    )
  ) %>%
  layout(
    autosize = FALSE,
    showlegend = FALSE, 
    xaxis = list(
      title = "Value"
    ),
    yaxis = list(
      title = ""
    )
  ) 

The issue is that sometimes the number of categories varies. It can be 20 in this case, it can go up to 100.

What I would like to have is to fix the height of the horizontal bar regardless of the number of categories. So if there are more categories, the height of each bar will stay constant and the vertical scroll will appear to accommodate the extra bars.

Rplot

So far the solution prescribed online is to write a fixed formula for height and let Plotly auto-size itself. I took the cue from this site.

https://community.plotly.com/t/how-to-add-vertical-scroll-bar-on-horizontal-bar-chart/12342/2

The work-around suggests writing a formula on height based on the fixed formula

plot_height <- 500 + 10*nrow(df)

But this is not ideal. Sometimes I have only 1 category and the bar height is excessive. Sometimes I have 1000 categories and each of them looks like a stick.

I don't know. I thought having a fixed bar height for horizontal bar charts should be something quite fundamental, but it turns out exceedingly hard in Plotly.

Would appreciate any suggestions here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducible R example, especially the plot_ly() and layout() entry points, and review the linked Plotly community workaround. Determine whether Plotly supports fixed horizontal bar sizing with scrolling without calculating chart height. Done means a documented or implemented approach that keeps bar height consistent for varying category counts.

Written by the indexing model from the issue text.

Assessment

Tech stack
plotly, r
Domain
data-visualization
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.