【发布时间】:2018-09-08 18:06:22
【问题描述】:
你好!有没有办法索引图表以在特定点开始和结束 (可能没有数字顺序)?
我的数据从 10 月 1 日开始,到次年 9 月 31 日结束。该系列在过去多年重复,我想建立一个每日季节性图表。挑战在于X轴不是从低到高,而是10-11-12-1-2-3-4-5-6-7-8-9。
问题 1:
您可以按月 10-11-12-1-2-3-4-5-6-7-8-9 订购指数吗? 同时,与 %m-%d 格式兼容,因为真正的问题在于 每日格式,但为了简洁起见,我只使用月份。
结果应该是这样的...对不起,我不得不使用 excel...
问题 2:
我们能不能去掉相连的图表线,或者1的解决方案,自然修复 问题2?以下尝试中的示例。
问题 3:
解决方案的最终格式是否允许采用移动平均线或其他 初始数据的突变?尝试#2 中的表将允许逐年取每个月的平均值。由于 7 月 17 日是 6 日,7 月 18 日是 12 日,我们将在图表中绘制一个 9,以此类推。
问题 4:
有没有和XTS等价的解决这个问题?
谢谢,谢谢,谢谢!
library(ggplot2)
library(plotly)
library(tidyr)
library(reshape2)
Date <- seq(as.Date("2016-10-1"), as.Date("2018-09-01"), by="month")
values <- c(2,3,4,3,4,5,6,4,5,6,7,8,9,10,8,9,10,11,12,13,11,12,13,14)
YearEnd <-c(2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,
2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018)
df <- data.frame(Date,values,YearEnd)
## PLOT THE TIMESERIES
plot_ly(df, x = ~Date, y = ~values, type = "scatter", mode = "lines")
## PLOT THE DATA BY MONTH: attempt 1
df$Month <- format(df$Date, format="%m")
df2 <- df %>%
select(values, Month, YearEnd)
plot_ly(df2, x = ~Month, y = ~values, type = "scatter", mode = "lines",
connectgaps = FALSE)
## Plot starts on the 10th month, which is good, but the index is
## in standard order, not 10-11-12-1-2-3-4-5-6-7-8-9
## It also still connects the gaps, bad.
## CREATE A PIVOTTABLE: attempt 2
table <- spread(df2,YearEnd, values)
df3 <- melt(table , id.vars = 'Month', variable.name = 'series')
plot_ly(df3, x = ~Month, y = ~values, type = "scatter", mode = "lines",
connectgaps = FALSE)
## now the data are in the right order, but the index is still wrong
## I also do not understand how plotly is ordering it correctly, as 2
## is not the starting point in January.
【问题讨论】:
-
我不明白第一个情节有什么问题?
-
@Tung - 是的,第一个情节没有任何问题。只显示简单的时间序列。我正在尝试将 2017 年和 2018 年的数据以 10-11-12-1-2-3-4-5-6-7-8-9 的月尺度覆盖 x 轴。