错误消息用ggplot2绘制时间序列 [英] Error message plotting timeseries with ggplot2

查看:169
本文介绍了错误消息用ggplot2绘制时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 日期<-c(07 / 12/2012 05:00:00,2012年12月12日06:00:00,2012年12月12日07:00:00,
2012年12月12日08:00: 00)
日期<-strptime(日期,%d /%m /%Y%H:%M)
计数<-c(0,3 (日期,计数,stringsAsFactors = FALSE)

$计数< - as.numeric(计数)
df1 < - data.frame增加时间到数据框。
df1 < - 在(df1,{
posb< - as.POSIXlt(日期,格式=%d /%m /%Y%H:%M)
小时内< - posb $ hour
mins< - posb $ min
dates< - format(posb,%x)
time< - format(posb,%H: %b











$ b $ d

$ 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(2012年11月12日 ,2012年11月12日,11/12/2012,
2012年11月12日,2012年12月12日,11/12/2012, 2012年11月12日,
2012年11月12日,11/12/2012,11/12/2012,11/12/2012, 12/2012,
2012年11月12日,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) ,步骤= c(1L,1 b,2 b,1L,1L,0L,5L, ,8L)),.Names = c(Date,Time,Axis1,Steps
),row.names = 52838:52857,class =data.frame)
#用时间列创建一个新的数据帧。
day3 < - in(day3,{
posb < - as.POSIXlt(Time,format =%H:%M:%S)
posb < - NULL#清理
))

库(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


解决方案

问题是由于您的 time 变量是一个字符向量:

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

您必须将其转化为例如类似于 POSIXlt 的对象: df1,aes(x = strptime(time,%H:%M),y = Counts))+ geom_line()

或者,更简单的是,您可以直接使用 Date 变量,而无需转换:

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



更好的是,你会看到 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)))


I am very new to ggplot2 and wondered whether anyone could help me with my simple problem. My sample dataframe is given as follows. I want to plot the time (hours and minutes) against the number of Counts.

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()

I always get the error message 'geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?'.

Can anyone help me rectify my code to allow my graph to plot correctly?

Edit I am still trying to get the formatting of my time variable to work, and plot a graph. However at the moment, it's still not recognising the date format. I would like to be able to: 1. Plot a set period of data (say from 07:47:50 to 07:49:10). 2. Ask R to plot the x axis every whole minute. ... neither of these I can get to work at the moment. A subset of my real data is shown below. Any advice would be gratefully received.

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

解决方案

The problem is due to the fact that your time variable is a character vector :

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

You must convet it to an object of class POSIXlt, for example like this :

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

Or, much simpler, you can directly use your Date variable, without transformation :

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

Better yet, you will see that ggplot2 automagically labels your x axis depending on your timespan along the axis.

EDIT : if you want to define the x-axis limits, you can do something like :

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")))

这篇关于错误消息用ggplot2绘制时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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