打印重叠点ggplot2时间序列数据

时间:2013-12-19 21:35:36

标签: r ggplot2 time-series duplicate-data

我正在绘制一些数据记录的开始和结束时间。下面是我正在使用的数据框架,而不是我制作的。基本上我有2个循环遍历我的数据,寻找开始和结束时间,每个循环产生一个数据帧,然后粘贴到“数据原始”。

现在我想提醒我的观察者每个离散数据集的开始和结束时间是什么,并且暂时只用不同颜色着色标记为“开始”和“结束”的点就足够了。老实说,虽然我想让这个更加花哨的画一个框周围的框,褪色的背景与交替的颜色,如果可能使用我选择作为角落的点。这样第一个“开始”点是1个角,第一个“结束”点是下一个角。没有任何线索是可能的,虽然我想在跑之前我会爬行。

下面是我正在使用的数据框和我用来标记点的ggplot2函数。

老实说,我觉得这是一个非常简单的问题,因为我对ggplot如何映射或优先处理数据缺乏了解。

require(ggplot2)

dataraw <- structure(list(Time = c(1383817893L, 1383817970L, 1383818010L, 
        1383818080L, 1383818170L, 1383818250L, 1383817923L, 1383818003L, 
        1383818043L, 1383818113L, 1383818203L, 1383818286L, 1383817890L, 
        1383817893L, 1383817896L, 1383817899L, 1383817902L, 1383817905L, 
        1383817908L, 1383817911L, 1383817914L, 1383817917L, 1383817920L, 
        1383817923L, 1383817970L, 1383817973L, 1383817976L, 1383817979L, 
        1383817982L, 1383817985L, 1383817988L, 1383817991L, 1383817994L, 
        1383817997L, 1383818000L, 1383818003L, 1383818010L, 1383818013L, 
        1383818016L, 1383818019L, 1383818022L, 1383818025L, 1383818028L, 
        1383818031L, 1383818034L, 1383818037L, 1383818040L, 1383818043L, 
        1383818080L, 1383818083L, 1383818086L, 1383818089L, 1383818092L, 
        1383818095L, 1383818098L, 1383818101L, 1383818104L, 1383818107L, 
        1383818110L, 1383818113L, 1383818170L, 1383818173L, 1383818176L, 
        1383818179L, 1383818182L, 1383818185L, 1383818188L, 1383818191L, 
        1383818194L, 1383818197L, 1383818200L, 1383818203L, 1383818250L, 
        1383818253L, 1383818256L, 1383818259L, 1383818262L, 1383818265L, 
        1383818268L, 1383818271L, 1383818274L, 1383818277L, 1383818280L, 
        1383818283L, 1383818286L), value = c(4307L, 4748L, 5419L, 4663L, 
        4779L, 4532L, 5539L, 4589L, 5541L, 5403L, 5277L, 5183L, 4246L, 
        4307L, 4368L, 4416L, 4930L, 5417L, 5444L, 5461L, 5485L, 5507L, 
        5520L, 5539L, 4748L, 4730L, 4741L, 4706L, 4717L, 4684L, 4673L, 
        4673L, 4660L, 4651L, 4585L, 4589L, 5419L, 5463L, 5487L, 5510L, 
        5513L, 5535L, 5535L, 5538L, 5532L, 5543L, 5534L, 5541L, 4663L, 
        4632L, 4598L, 4586L, 4577L, 4565L, 4546L, 5316L, 5348L, 5340L, 
        5363L, 5403L, 4779L, 4790L, 4790L, 4779L, 4762L, 4771L, 5249L, 
        5294L, 5286L, 5290L, 5267L, 5277L, 4532L, 4361L, 4327L, 4319L, 
        4307L, 4288L, 4285L, 5098L, 5151L, 5158L, 5163L, 5177L, 5183L
        ), variable = c("Start", "Start", "Start", "Start", "Start", 
        "Start", "End", "End", "End", "End", "End", "End", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1", 
        "rawPressure1", "rawPressure1", "rawPressure1", "rawPressure1"
        )), .Names = c("Time", "value", "variable"), row.names = c(NA, 
        85L), class = "data.frame")


    p <- ggplot(dataraw,aes(Time, value,group=variable)) +
                geom_point(aes(colour=variable))

编辑:这是上一个发现“开始”和“结束”的循环,它确实复制了数据。如下所述,最好将start和end添加为一个因子,而不是将其复制并将其添加为变量。

dataraw <-structure(list(Time = c(1383817890L, 1383817893L, 1383817896L, 
1383817899L, 1383817902L, 1383817905L, 1383817908L, 1383817911L, 
1383817914L, 1383817917L, 1383817920L, 1383817923L, 1383817970L, 
1383817973L, 1383817976L, 1383817979L, 1383817982L, 1383817985L, 
1383817988L, 1383817991L, 1383817994L, 1383817997L, 1383818000L, 
1383818003L, 1383818010L, 1383818013L, 1383818016L, 1383818019L, 
1383818022L, 1383818025L, 1383818028L, 1383818031L, 1383818034L, 
1383818037L, 1383818040L, 1383818043L, 1383818080L, 1383818083L, 
1383818086L, 1383818089L, 1383818092L, 1383818095L, 1383818098L, 
1383818101L, 1383818104L, 1383818107L, 1383818110L, 1383818113L, 
1383818170L, 1383818173L, 1383818176L, 1383818179L, 1383818182L, 
1383818185L, 1383818188L, 1383818191L, 1383818194L, 1383818197L, 
1383818200L, 1383818203L, 1383818250L, 1383818253L, 1383818256L, 
1383818259L, 1383818262L, 1383818265L, 1383818268L, 1383818271L, 
1383818274L, 1383818277L, 1383818280L, 1383818283L, 1383818286L
), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = "rawPressure1", class = "factor"), value = c(4246L, 
4307L, 4368L, 4416L, 4930L, 5417L, 5444L, 5461L, 5485L, 5507L, 
5520L, 5539L, 4748L, 4730L, 4741L, 4706L, 4717L, 4684L, 4673L, 
4673L, 4660L, 4651L, 4585L, 4589L, 5419L, 5463L, 5487L, 5510L, 
5513L, 5535L, 5535L, 5538L, 5532L, 5543L, 5534L, 5541L, 4663L, 
4632L, 4598L, 4586L, 4577L, 4565L, 4546L, 5316L, 5348L, 5340L, 
5363L, 5403L, 4779L, 4790L, 4790L, 4779L, 4762L, 4771L, 5249L, 
5294L, 5286L, 5290L, 5267L, 5277L, 4532L, 4361L, 4327L, 4319L, 
4307L, 4288L, 4285L, 5098L, 5151L, 5158L, 5163L, 5177L, 5183L
), dt = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 47L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 7L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 37L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 57L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
47L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, NA)), .Names = c("Time", 
"variable", "value", "dt"), row.names = c(NA, -73L), class = "data.frame")

这是通过下面的循环推送的数据框。

这些循环只是简单地说出了我认为是每个离散数据集的开始和结束时间。而不是Rbind点我应该为数据点添加一个因子。

Start <- data.frame()

for (i in 2:(length(dataraw[,1])-1))
{
  if (( i == 2) ||  (dataraw$dt[i-1] > 3 && dataraw$dt[i]==3)) { 
  Start <- rbind(Start,c(dataraw[i,1],dataraw[i,3]))

  }

  {next}
}
colnames(Start) <- c('Time','value')
Start$variable <- paste("Start")

End <- data.frame()
for (i in 1:(length(dataraw[,1])))
{ 
  if (( dataraw$dt[i] > 3 && dataraw$dt[i-1]==3) || is.na(dataraw$dt[i])==TRUE) { 
    End <- rbind(End,c(dataraw[i,1],dataraw[i,3]))

  }

{next}
}
colnames(End) <- c('Time','value')
End$variable <- paste("End")
Events <- rbind(Start,End)
dataraw <- dataraw[,1:3]
dataraw <- rbind(Events,dataraw)

2 个答案:

答案 0 :(得分:1)

认为您可能会以错误的形式提供您正在尝试执行的操作的数据。

开始时间和结束时间似乎是作为新点而不是旧点的属性创建的?所以ggplot在同一时间和rawPressure1绘制两个点,隐藏彩色的点。

编辑:如果在循环结束时,不使用rbind组合数据帧,请使用merge,然后删除额外的列。这会创建一个数据框,其中包含点,它们的值以及开始,结束或NA的因子。然后可以使用这个新因子对点进行着色。

因此,代替循环的最后一行,添加这两行

data <- merge(x = dataraw, y = Events, by = "Time", all.x = T)
data <- data[,c(1,3,5)]

这将创建具有Start / End的数据帧作为适当时间的属性。

然后像以前一样密谋

p <- ggplot(data,aes(Time, rawPressure1))+
           geom_point(aes(colour= factor(pos)))
p

Figure

答案 1 :(得分:0)

如果我理解你(你的主要问题是情节)你应该尝试 qplot ;

查看此tutorial