plotly / plotly/plotly.R

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

Offen
#1,516 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
R
Sterne
2.7k
Forks
641
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Führen Sie zunächst das mitgelieferte R-Beispiel mit n=3 aus, variieren Sie dann n und vergleichen Sie die Balkenbreiten und die Sichtbarkeit im Overlay-Modus über die beiden Traces hinweg. Verfolgen Sie die von diesem Beispiel verwendete plotly.R-Behandlung von Balken und Date und betrachten Sie das Issue als erledigt, wenn spärlich belegte Date-Traces angemessene Breiten beibehalten und ein einzelner Wert gerendert wird, ohne fiktive Datumswerte mit dem Wert null zu benötigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
r
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.