2013-02-11 81 views
1

我对ggplot2非常陌生,想知道是否有人可以帮我解决我的简单问题。我的示例数据框如下所示。我想根据计数数量绘制时间(小时和分钟)。使用ggplot2绘制时间序列的错误消息

Date <- c("07/12/2012 05:00:00", "07/12/2012 06:00:00", "07/12/2012 07:00:00", 
     "07/12/2012 08:00:00") 
Date <- strptime(Date, "%d/%m/%Y %H:%M") 
Counts <- c("0","3","10","6") 
Counts <- as.numeric(Counts) 
df1 <- data.frame(Date,Counts,stringsAsFactors = FALSE) 

#Adds time to dataframe. 
df1 <- within(df1,{ 
    posb <- as.POSIXlt(Date,format="%d/%m/%Y %H:%M") 
hours <- posb$hour 
mins <- posb$min 
dates <- format(posb, "%x") 
time <- format(posb, "%H:%M") 
posb <- NULL # cleanup 
}) 
#Draw graph (time versus Counts) 
library(ggplot2) 
g = ggplot(df1, aes(x=time, y=Counts)) 
g + geom_line() 

我总是得到错误消息'geom_path:每个组只包含一个观察值。你需要调整团体审美吗?'。

任何人都可以帮我纠正我的代码,让我的图正确绘制?

编辑 我仍然试图让我的时间变量的格式工作,并绘制图。但目前,它还没有识别日期格式。 我希望能够: 1.绘制一段时间的数据(比如从07:47:50到07:49:10)。 2.询问R每整分钟绘制x轴。 ......我现在都无法工作。下面显示了我真实数据的一个子集。任何意见将受到感谢。

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 

回答

1

的问题是由于这样的事实,你的time变量是一个字符向量:

R> class(df1$time) 
[1] "character" 

你必须将其convet到POSIXlt类的对象,例如像这样:

ggplot(df1, aes(x=strptime(time, "%H:%M"), y=Counts)) + geom_line() 

或者更简单一些,你可以直接使用你的Date变量,不用转换:

ggplot(df1, aes(x=Date, y=Counts)) + geom_line() 

enter image description here

更重要的是,你会看到,ggplot2取决于沿轴的时间跨度自动将标签的x轴。

编辑:如果要定义x轴的限制,你可以这样做:

ggplot(df1, aes(x=Date, y=Counts)) + geom_line() + scale_x_datetime(limits=c(as.POSIXct("2012/12/07 04:00:00"),as.POSIXct("2012/12/07 10:00:00"))) 
+0

非常感谢@Juba。再一个简单的问题。因为我有时间,重新定义x轴的最佳方式是什么(我想从04:00到10:00)。我想我应该使用:xlim = c(xmin,xmax)),但我努力让这个工作... – 2013-02-11 16:03:52

+0

@ KT_1我编辑我的答案来添加一种方法来做到这一点,但肯定有一些更简单。 – juba 2013-02-11 17:16:29

+0

非常感谢@juba。我仍在努力让代码与我的真实数据一起工作。我编辑了我的原始问题以显示额外的数据。任何帮助将是最令人满意的。 – 2013-02-18 13:45:21