[英]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(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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.