Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何不使用R绘制时间序列中的间隔_R_Ggplot2_Time Series - Fatal编程技术网

如何不使用R绘制时间序列中的间隔

如何不使用R绘制时间序列中的间隔,r,ggplot2,time-series,R,Ggplot2,Time Series,我有一些带有间隙的时间序列数据 df<-read.table(header=T,sep=";", text="Date;x1;x2 2014-01-10;2;5 2014-01-11;4;7 2014-01-20;8;9 2014-01-21;10;15 ") df$Date <- strptime(df$Date,"%Y-%m-%d") df.long <- melt(df,id="Date") ggplot(df.long, aes(x = Date, y = value,

我有一些带有间隙的时间序列数据

df<-read.table(header=T,sep=";", text="Date;x1;x2
2014-01-10;2;5
2014-01-11;4;7
2014-01-20;8;9
2014-01-21;10;15
")
df$Date <- strptime(df$Date,"%Y-%m-%d")
df.long <- melt(df,id="Date")
ggplot(df.long, aes(x = Date, y = value, fill = variable, order=desc(variable))) +   
  geom_area(position = 'stack') 

df您可以在数据框中添加一个附加变量,
group
,指示两个日期之间的差值是否不等于一天:

df.long$group <- c(0, cumsum(diff(df.long$Date) != 1))

ggplot(df.long, aes(x = Date, y = value, fill = variable, 
                    order=desc(variable))) +
  geom_area(position = 'stack', aes(group = group)) 

df.long$组谢谢!我能告诉ggplot从11号到20号“折叠”x轴吗?这是一个连续的图表?非常感谢你的工作,看看这对我是否有效(每个周末都是一个间隙)。
library(plyr)
df.long2 <- ddply(df.long, .(variable), mutate, 
                  group = c(0, cumsum(diff(Date) > 1)))

ggplot(df.long2, aes(x = Date, y = value, fill = variable, 
                     order=desc(variable)))  +
  geom_area(position = 'stack',) +
  facet_wrap( ~ group, scales = "free_x")