【问题标题】:Error message plotting timeseries with ggplot2使用 ggplot2 绘制时间序列的错误消息
【发布时间】:2013-02-11 14:34:51
【问题描述】:

我对 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

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

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

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

    您必须将其转换为 POSIXlt 类的对象,例如:

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

    【讨论】:

    • 非常感谢@Juba。只是另一个快速的问题。由于我有时间,重新定义 x 轴的最佳方法是什么(我想从 04:00 到 10:00)。我想我应该使用:xlim=c(xmin, xmax)) 但我正在努力让它工作......
    • @KT_1 我编辑了我的答案以添加一种方法来做到这一点,但肯定有更简单的方法。
    • 非常感谢@juba。我仍在努力让代码与我的真实数据一起使用。我已经编辑了我的原始问题以显示我的额外数据。任何帮助将不胜感激。
    • @KT_1 我认为最好再问一个问题。
    猜你喜欢
    • 2017-03-04
    • 2013-01-30
    • 1970-01-01
    • 2018-07-05
    • 2021-03-17
    • 2019-05-26
    • 2021-05-12
    • 2011-11-21
    • 1970-01-01
    相关资源
    最近更新 更多