ggplot2时间序列绘图:如何省略没有数据点的时段? [英] ggplot2 time series plotting: how to omit periods when there is no data points?

查看:191
本文介绍了ggplot2时间序列绘图:如何省略没有数据点的时段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多天数据的时间序列。在每天之间有一段时间没有数据点。如何在使用 ggplot2



绘制时间序列时省略这些时间段。我可以摆脱两个没有数据的时期吗?

代码:

 <$时间= Sys.time()+(seq(1,100)* 60 + c(rep(1,100)* 3600 * 24,rep(2,100)* 3600 * 24,rep(3,100)* 3600 * 24))
Value = rnorm(length(Time))
g < - ggplot()
g < - g + geom_line(aes(x = Time,y = Value))
g

解决方案

首先,创建一个分组变量。这里,如果时间差大于1分钟,两个组是不同的:

 组<-c(0,cumsum( diff(Time)> 1))

现在可以使用 facet_grid 和参数 scales =free_x

  library(ggplot2)
g < - ggplot(data.frame(Time,Value,Group))+
geom_line(aes(x = Time,y = Value))+
facet_grid(〜Group,scales =free_x)


I have a time series with multiple days of data. In between each day there's one period with no data points. How can I omit these periods when plotting the time series using ggplot2?

An artificial example shown as below, how can I get rid of the two periods where there's no data?

code:

Time = Sys.time()+(seq(1,100)*60+c(rep(1,100)*3600*24, rep(2, 100)*3600*24, rep(3, 100)*3600*24))
Value = rnorm(length(Time))
g <- ggplot() 
g <- g + geom_line (aes(x=Time, y=Value))
g

解决方案

First, create a grouping variable. Here, two groups are different if the time difference is larger than 1 minute:

Group <- c(0, cumsum(diff(Time) > 1))

Now three distinct panels could be created using facet_grid and the argument scales = "free_x":

library(ggplot2)
g <- ggplot(data.frame(Time, Value, Group)) + 
  geom_line (aes(x=Time, y=Value)) +
  facet_grid(~ Group, scales = "free_x")

这篇关于ggplot2时间序列绘图:如何省略没有数据点的时段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆