Problems with barcharts with barmode='overlay' and Date objects
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 2.7k
- Forks
- 641
- PR merge metrics
- No merged PRs in 30d
Description
I need to plot several barcharts with Date objects on the x-axis with barmode = 'overlay' for a whole year. When there are enough values to plot, everything works fine, as I would like the bars to have equal widths along all traces.
But when one trace has few elements, the width of those traces are expanded.
And when one trace only has 1 value, it is not shown at all.
I could overcome those problems by creating fictional Dates with 0 y-values for the rest of the year, but I was hoping there is a better way of solving that.
The variable n in the example can be used to determine the amount of bars for the problematic
trace.
library(plotly)
n = 3
## All days of 2019 ###############
Days2019 <- data.frame(
date = seq(as.Date("2019-01-01"), as.Date("2019-12-31"), by = "days")
)
## Sample n Days of 2019 ###############
df <- data.frame(
date = sort(sample(Days2019$date, size = n, replace = F)),
val = sample(1:100, n, replace = T)
)
## Merge fictional 0 y-Values #################
# df = merge(df, Days2019, by="date", all=T)
# df[is.na(df$val),]$val=0
## Sample 30 Days of 2019 ###############
holiday1 <- data.frame(
date = sort(sample(Days2019$date, size = 30, replace = F))
)
## Plot Plotly ###############
maxY = max(df$val)
plot_ly() %>%
add_trace(data = holiday1, x = ~date, y = maxY, marker = list(color = "gray"), type="bar", name="Holidays") %>%
add_trace(data = df, x = ~date, y = ~val, marker = list(color = "red"), type="bar", name="Values") %>%
plotly::layout(autosize = F,
barmode = 'overlay',
bargap = 0.05,
xaxis = list(
type = "date",
range = c(as.Date("2019-01-01")-1,
as.Date("2019-12-31")+1),
autorange = FALSE,
tickmode = "auto",
nticks = 14L,
automargin = F)
)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied R example with n=3, then vary n and compare the bar widths and visibility in overlay mode across the two traces. Trace the plotly.R bar/date handling used by this example and consider the issue done when sparse Date traces retain appropriate widths and a single value is rendered without requiring fictional zero-valued dates.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100