Reputation: 63
I need to plot a area chart, it contains financial data and I am using plotly to do so. The problem is, plotly has a feature of detecting the time series format and plots even the missing dates by simply extending a linear line until the next available data input. Is it possible to disable this feature and plot only the time series where data is available?
library(plotly)
Datetime <- c(
"2016-01-05 00:00:00",
"2016-01-06 00:00:00",
"2016-01-07 00:00:00",
"2016-01-08 00:00:00",
"2016-01-11 00:00:00",
"2016-01-12 00:00:00",
"2016-01-13 00:00:00",
"2016-01-14 00:00:00",
"2016-01-15 00:00:00",
"2016-01-18 00:00:00",
"2016-01-19 00:00:00",
"2016-01-20 00:00:00",
"2016-01-21 00:00:00",
"2016-01-22 00:00:00",
"2016-01-25 00:00:00",
"2016-01-26 00:00:00",
"2016-01-27 00:00:00",
"2016-01-28 00:00:00",
"2016-01-29 00:00:00",
"2016-02-01 00:00:00")
plotdata <- c(93763,110023,134873,138780,117038,117890,120025,140715,48567,87592,
115852,145189,162258,121456,93643,128475,119310,105771,134946,90386)
volume_data <- data.frame(Datetime, plotdata)
plot_ly(volume_data, x = Datetime, y = plotdata, type = "bar")
This is a basic sample data, If you execute this, you will notice that there are blank spaces in the graph. Though in my execution, I am using area chart, I have presented a sample data with bar chart so that the blank spaces are easier noticed. I understand that plotly recognises the x axis data as timeseries and auto completes the missing data. Is it possible to disable this and plot only the date and time where the data is available?
Upvotes: 2
Views: 3198
Reputation: 436
There seems to be an option to set rangebreaks in plotly now: https://plotly.com/r/time-series/
The following Code is from their website.
library(plotly)
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig <- plot_ly(
type = "scatter",
x = as.Date(df$Date, format= "%Y-%m-%d"),
y = df$AAPL.High,
name = 'AAPL High',
mode = "markers",
)
fig <- fig %>%
layout(
title = "Time Series with Custom Date-Time Format",
xaxis = list(
type = "date",
range=c('2015-12-01', '2016-01-15'),
rangebreaks = list(
list(bounds=c("sat", "mon")),
list(values=c("2015-12-25", "2016-01-01"))
)
)
)
fig
Upvotes: 0
Reputation: 63
I found a much simpler solution to the problem and thought I will post it as an answer which may be helpful for others as well. It works perfectly if you add an argument in layout like here
p <- plot_ly(volume_data, x = Datetime, y = plotdata, type = "bar")
p <- layout(p, xaxis = list(type = "category"))
Upvotes: 3
Reputation: 9836
Here is an alternative that will give you what you want.
p <- plot_ly(
x = c("Jan 5", "March 5", "April 5"),
y = c(20, 14, 23),
name = "SF Zoo",
type = "bar")
p %>% layout(xaxis = list(title="Date"), yaxis = list(title="Volume Data"))
You just need to convert your Datetime column (like Jan 5, March 5...format) prior to plotting
Upvotes: 1