plotly / plotly/plotly.R

Problems with barcharts with barmode='overlay' and Date objects

Aperta
#1,516 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
R
Stelle
2.7k
Fork
641
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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)
  )

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo l’esempio R fornito con n=3, quindi varia n e confronta le larghezze delle barre e la visibilità in modalità overlay tra le due tracce. Analizza la gestione di barre e Date in plotly.R utilizzata da questo esempio e considera conclusa l’issue quando le tracce Date sparse mantengono larghezze appropriate e un singolo valore viene renderizzato senza richiedere date fittizie con valore zero.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
r
Ambito
data-visualization
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.