Plotly: ggplotly barcharts with Dates and dynamicTicks=T

Created on 10 Oct 2018  路  1Comment  路  Source: ropensci/plotly

I experience a weird behaviour when using barcharts with Dates on the x-Axis and dynamicTicks=T.

1 bar of the example (4th July) is in a different color, it doesnt show up and the highlight function also doesnt color the clicked bar, although the legend suggests otherwise.

When converting the Dates to character, everything shows up correctly and also the highlight function shows up, but the labels look horrible.


Plot with Dates

library(ggplot2)
library(plotly)

dfN <- data.frame(
  time_stamp = seq.Date(as.Date("2018-04-01"), as.Date("2018-07-30"), 1),
  val = runif(121, 100,1000),
  col = "green", stringsAsFactors = F
)

dfN[95,]$col = "orange"

maxY = max(dfN$val)
key <- highlight_key(dfN)

p <- ggplot() +
  geom_col(data = key, aes(x = (time_stamp), y = val, fill=I(col)), width = 1)

## 4 of July doesnt show at all (should show an orange bar)
ggplotly(p, source = "Src", dynamicTicks = T, originalData = T) %>%
  highlight(selectize=F, on="plotly_click", off = "plotly_doubleclick", color = "blue")

rplot


Plot with Characters

p <- ggplot() +
  geom_col(data = key, aes(x = as.character(time_stamp), y = val, fill=I(col)), width = 1) +
  ggtitle("Characters on x-Axis")

ggplotly(p, source = "Src", dynamicTicks = T, originalData = T) %>%
  highlight(selectize=F, on="plotly_click", off = "plotly_doubleclick", color = "blue")

rplot02

bug ggplotly

Most helpful comment

I'd consider this a bug, but here's a hack you can use in the meantime

library(ggplot2)
library(plotly)

dfN <- data.frame(
  time_stamp = seq.Date(as.Date("2018-04-01"), as.Date("2018-07-30"), 1),
  val = runif(121, 100,1000),
  col = "green", stringsAsFactors = F
)

dfN[95,]$col = "orange"

maxY = max(dfN$val)
key <- highlight_key(dfN)

p <- ggplot() +
  geom_col(data = key, aes(x = plotly:::to_milliseconds(time_stamp), y = val, fill=I(col)))

## 4 of July doesnt show at all (should show an orange bar)
ggplotly(p, source = "Src") %>% layout(xaxis = list(tickval = NULL, ticktext = NULL, type = "date"))

>All comments

I'd consider this a bug, but here's a hack you can use in the meantime

library(ggplot2)
library(plotly)

dfN <- data.frame(
  time_stamp = seq.Date(as.Date("2018-04-01"), as.Date("2018-07-30"), 1),
  val = runif(121, 100,1000),
  col = "green", stringsAsFactors = F
)

dfN[95,]$col = "orange"

maxY = max(dfN$val)
key <- highlight_key(dfN)

p <- ggplot() +
  geom_col(data = key, aes(x = plotly:::to_milliseconds(time_stamp), y = val, fill=I(col)))

## 4 of July doesnt show at all (should show an orange bar)
ggplotly(p, source = "Src") %>% layout(xaxis = list(tickval = NULL, ticktext = NULL, type = "date"))
Was this page helpful?
0 / 5 - 0 ratings

Related issues

viniciusvgp picture viniciusvgp  路  5Comments

fabiangehring picture fabiangehring  路  4Comments

robertleitner picture robertleitner  路  5Comments

talgalili picture talgalili  路  4Comments

ahsanshah picture ahsanshah  路  7Comments