1

我正在处理时间序列。我的真实数据的前 20 行如下所示。我希望:

一种。将我的 ggplot 图子集到特定的时间范围(例如,仅显示 07:46:40 和 07:49:00 之间的值)。

湾。我还希望更改 x 轴上刻度线的频率。使用示例详细的 bwlow 我希望只在轴上显示整分钟(但是对于我的正确图表,我希望只显示每小时值)。

任何关于上述内容的建议将不胜感激。

day3 <- structure(list(Date = c("11/12/2012", "11/12/2012", "11/12/2012", 
                            "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", 
                            "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", 
                            "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", 
                            "11/12/2012", "11/12/2012"), Time = c("07:46:10", "07:46:20", 
                                                                  "07:46:30", "07:46:40", "07:46:50", "07:47:00", "07:47:10", "07:47:20", 
                                                                  "07:47:30", "07:47:40", "07:47:50", "07:48:00", "07:48:10", "07:48:20", 
                                                                  "07:48:30", "07:48:40", "07:48:50", "07:49:00", "07:49:10", "07:49:20"
                            ), Axis1 = c(59L, 651L, 59L, 0L, 22L, 50L, 0L, 0L, 114L, 899L, 
                                         129L, 33L, 21L, 9L, 224L, 135L, 266L, 16L, 59L, 126L), Steps = c(1L, 
                                                                                                          2L, 1L, 0L, 2L, 1L, 0L, 0L, 5L, 15L, 6L, 2L, 2L, 0L, 8L, 5L, 
                                                                                                          16L, 1L, 3L, 8L)), .Names = c("Date", "Time", "Axis1", "Steps"
                                                                                                          ), row.names = 52838:52857, class = "data.frame")
#Creates a new dataframe with a time column.
day3 <- within(day3,{
  posb <- as.POSIXlt(Time,format="%H:%M:%S")
  posb <- NULL  # cleanup
})

library(ggplot2)
g = ggplot(day3, aes(x=strptime(Time, "%H:%M:%S"), y=Axis1)) + geom_line(aes(group = 1)) +
  theme_bw() +
  xlab("Time") + 
  ylab("Activity (Counts per 10 seconds)") + 
  scale_x_datetime(limits=c(as.POSIXct("07:47:50"),as.POSIXct("07:49:10")))


g

编辑

如果我想在我的图表上添加一个文本框,我如何使用时间列?到目前为止,我得到了:

geom_text(aes(05:00,0),label="Sedentary")

...但这目前不想工作。

4

1 回答 1

2

这是你想要的 ?

library(scales)
ggplot(day3, aes(x=strptime(Time, "%H:%M:%S"), y=Axis1)) + geom_line(aes(group = 1)) +
  theme_bw() +
  xlab("Time") + 
  ylab("Activity (Counts per 10 seconds)") +
  scale_x_datetime(limits=c(as.POSIXct("07:46:40",format="%H:%M:%S"),as.POSIXct("07:49:00",format="%H:%M:%S")),
                   breaks=date_breaks("1 min"), labels = date_format("%H:%M"))

在此处输入图像描述

于 2013-02-18T14:18:41.440 回答