【问题标题】:"Unambiguous date format" using ggplot in r“明确的日期格式”在 r 中使用 ggplot
【发布时间】:2017-12-17 03:29:33
【问题描述】:

我有一个 csv 文件,其中有一列日期和时间,格式为 m/dd/yy H:M。我想沿着我的 x 轴绘制这个,沿着 y 轴绘制数据。我看过这个帖子:Problems creating datetime series graph in R using ggplot,但至少有些建议似乎已经过时了。

我尝试通过以下方式生成图表:

discharge <- read.csv("~/mac2017/Everything_plot/discharge2.csv", header 
= TRUE, stringsAsFactors = FALSE, strip.white = TRUE)

discharge$Date_Time <- as.POSIXct(discharge$Date_Time, "%m/%d/%y %h:%m")

ggplot(data=discharge, aes(x=Date_Time, y=Discharge, type=1, group=1))+
    geom_line()+
    scale_x_date(date_labels = ("%b-%Y")) + #breaks = date_breaks("1 
month"), labels = date_format("%m/%Y"))+
    scale_y_continuous()

我已尝试将 Date 列格式化为 Date 对象,正如该帖子中建议的答案,但继续收到此错误:

Error in as.Date(x, tx, ...) : 
character string is not in a standard unambiguous format

因此,我转而使用 as.POSIXct 命令,但在明确格式方面遇到了同样的错误。我曾尝试使用strptime,但再次收到相同的错误。我认为strptime 会修复明确的格式错误,因为它用于指定当前格式。

这是我很长的 csv 的前几行:

Date        Time    Date_Time       Discharge
8/23/2016   0:00    8/23/16 0:00    92.00
8/23/2016   0:15    8/23/16 0:15    91.00
8/23/2016   0:30    8/23/16 0:30    90.00
8/23/2016   0:45    8/23/16 0:45    89.00
8/23/2016   1:00    8/23/16 1:00    88.00
8/23/2016   1:15    8/23/16 1:15    87.00
8/23/2016   1:30    8/23/16 1:30    86.00
8/23/2016   1:45    8/23/16 1:45    86.00
8/23/2016   2:00    8/23/16 2:00    86.00
8/23/2016   2:15    8/23/16 2:15    85.00
8/23/2016   2:30    8/23/16 2:30    84.00
8/23/2016   2:45    8/23/16 2:45    83.00
8/23/2016   3:00    8/23/16 3:00    82.00

我对 R 很陌生。关于我做错了什么有什么想法吗?

【问题讨论】:

  • 您不能使用(系统不能使用)"%m" 几分钟和几个月。看看?strptime,你会发现你需要"%M" 分钟和"%H" 小时 - h 相当于b 是缩写的月份名称。

标签: r ggplot2


【解决方案1】:

考虑使用“lubridate”包。使用起来比 R 的内置日期/时间函数更直观一些。例如,我可以使用 mdy() 函数来设置/提取您的日期列。或使用 month() 函数设置/提取月份。

【讨论】:

    【解决方案2】:

    查看tidyverse 了解可帮助您进行数据导入、清理以及处理日期和时间的工具。 readr::read_csv 函数和 lubridate 包将非常有用。

    例如,我们将首先加载命名空间并保存一个临时 .csv

    library(readr)
    library(lubridate)
    library(dplyr)
    library(ggplot2)
    
    # save data as a csv for the example
    cat(
    "Date,       Time,   Date_Time,      Discharge
    8/23/2016,  0:00,   8/23/16 0:00,   92.00
    8/23/2016,  0:15,   8/23/16 0:15,   91.00
    8/23/2016,  0:30,   8/23/16 0:30,   90.00
    8/23/2016,  0:45,   8/23/16 0:45,   89.00
    8/23/2016,  1:00,   8/23/16 1:00,   88.00
    8/23/2016,  1:15,   8/23/16 1:15,   87.00
    8/23/2016,  1:30,   8/23/16 1:30,   86.00
    8/23/2016,  1:45,   8/23/16 1:45,   86.00
    8/23/2016,  2:00,   8/23/16 2:00,   86.00
    8/23/2016,  2:15,   8/23/16 2:15,   85.00
    8/23/2016,  2:30,   8/23/16 2:30,   84.00
    8/23/2016,  2:45,   8/23/16 2:45,   83.00
    8/23/2016,  3:00,   8/23/16 3:00,   82.00",
    file = "discharge2.csv")
    

    通过readr读取数据如下:

    # read in the data via readr::read_csv, reading in all data/time columns as
    # characters and the Discharge column as a number
    discharge <- readr::read_csv("discharge2.csv", col_type = "cccd")
    

    我们将为每个日期时间变量设置正确的日期时间存储模式。这是使用dplry::mutate 调用和lubridate 函数mdy、hm、mdy_hm 完成的。这些函数将解析日期时间格式并相应地存储结果。

    # set the date/time storage modes
    discharge <-
      discharge %>%
      dplyr::mutate(Date = lubridate::mdy(Date),
                    Time = lubridate::hm(Time),
                    Date_Time = lubridate::mdy_hm(Date_Time))
    

    一个简单的图形

    ggplot(discharge) +
      aes(x = Date_Time, y = Discharge) +
      geom_point() + geom_line()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-01
      • 2020-06-08
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2015-11-19
      相关资源
      最近更新 更多