如何避免在 R 中绘制缺失日期
How to avoid plotting missing dates in R plotly
我需要绘制一个面积图,它包含财务数据,我正在使用 plotly 来绘制它。
问题是,plotly 具有检测时间序列格式的功能,甚至可以通过简单地延长一条直线直到下一个可用数据输入来绘制缺失的日期。
是否可以禁用此功能并仅绘制数据可用的时间序列?
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")
这是一个基本的样本数据,如果你执行这个,你会注意到图中有空格。
虽然在我的执行过程中,我使用的是面积图,但我提供了一个带有条形图的示例数据,以便更容易注意到空白区域。
我知道 plotly 将 x 轴数据识别为时间序列并自动完成丢失的数据。是否可以禁用此功能并仅绘制数据可用的日期和时间?
这里有一个替代方案,可以满足您的需求。
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"))
您只需要在绘图之前转换日期时间列(如 Jan 5、March 5...格式)
我找到了一个更简单的问题解决方案,我想我会 post 它作为一个可能对其他人也有帮助的答案。
如果你像这里一样在布局中添加一个参数,它会完美地工作
p <- plot_ly(volume_data, x = Datetime, y = plotdata, type = "bar")
p <- layout(p, xaxis = list(type = "category"))
现在似乎有一个可以在 plotly 中设置 rangebreaks 的选项:https://plotly.com/r/time-series/
以下代码来自他们的网站。
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
我需要绘制一个面积图,它包含财务数据,我正在使用 plotly 来绘制它。 问题是,plotly 具有检测时间序列格式的功能,甚至可以通过简单地延长一条直线直到下一个可用数据输入来绘制缺失的日期。 是否可以禁用此功能并仅绘制数据可用的时间序列?
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")
这是一个基本的样本数据,如果你执行这个,你会注意到图中有空格。 虽然在我的执行过程中,我使用的是面积图,但我提供了一个带有条形图的示例数据,以便更容易注意到空白区域。 我知道 plotly 将 x 轴数据识别为时间序列并自动完成丢失的数据。是否可以禁用此功能并仅绘制数据可用的日期和时间?
这里有一个替代方案,可以满足您的需求。
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"))
您只需要在绘图之前转换日期时间列(如 Jan 5、March 5...格式)
我找到了一个更简单的问题解决方案,我想我会 post 它作为一个可能对其他人也有帮助的答案。 如果你像这里一样在布局中添加一个参数,它会完美地工作
p <- plot_ly(volume_data, x = Datetime, y = plotdata, type = "bar")
p <- layout(p, xaxis = list(type = "category"))
现在似乎有一个可以在 plotly 中设置 rangebreaks 的选项:https://plotly.com/r/time-series/
以下代码来自他们的网站。
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