ggplot2 绘制日期数据时出错 - 需要 TRUE/FALSE 时缺少值

发布于 2024-12-01 15:55:57 字数 1565 浏览 4 评论 0原文

我正在尝试绘制一些从外部来源获取的成绩。日期格式看起来像这样:

2011-08-23T17:07:05

所以我用 strptime(date, "%FT%X") 解析它并得到 POSIXlt。我最终得到一个完整的数据框,如下所示:

                  date    subject  grade
1  2011-08-23 17:07:05 AP Biology  95.83
2  2011-08-24 17:07:03 AP Biology  95.83
3  2011-08-25 17:08:27 AP Biology  95.83
4  2011-08-17 17:05:54 US History 157.14
5  2011-08-18 17:05:24 US History 157.14
6  2011-08-19 17:05:35 US History 157.14
7  2011-08-22 17:06:25 US History 157.14
8  2011-08-23 17:07:05 US History 157.14
9  2011-08-24 17:07:03 US History 157.14
10 2011-08-25 17:08:27 US History 157.14
11 2011-08-19 17:05:35   Yearbook   0.00
12 2011-08-22 17:06:25   Yearbook   0.00
13 2011-08-23 17:07:05   Yearbook 100.00
14 2011-08-24 17:07:03   Yearbook 100.00
15 2011-08-25 17:08:27   Yearbook 100.00

具有以下结构:

'data.frame':   15 obs. of  3 variables:
 $ date   : POSIXlt, format: "2011-08-23 17:07:05" "2011-08-24 17:07:03" ...
 $ subject: Factor w/ 3 levels "AP Biology","US History",..: 1 1 1 2 2 2 2 ...
 $ grade  : num  95.8 95.8 95.8 157.1 157.1 ...

当我尝试绘制此数据时:

> ggplot(data=grades, aes(date, grade, factor=subject)) + geom_line()
Error in if (length(range) == 1 || diff(range) == 0) { : 
  missing value where TRUE/FALSE needed

我不知道我在这里做错了什么。我通过这样做将其范围缩小到日期处理:

ggplot(data=grades,
       aes(seq(length(grades[,1])),
           grade, color=subject)) + geom_line()

...但是如何正确进行日期处理?

I'm trying to plot some grades I'm pulling from an external source. The date format comes in looking like this:

2011-08-23T17:07:05

So I parse it with strptime(date, "%FT%X") and get a POSIXlt. I end up with a complete data frame that looks like this:

                  date    subject  grade
1  2011-08-23 17:07:05 AP Biology  95.83
2  2011-08-24 17:07:03 AP Biology  95.83
3  2011-08-25 17:08:27 AP Biology  95.83
4  2011-08-17 17:05:54 US History 157.14
5  2011-08-18 17:05:24 US History 157.14
6  2011-08-19 17:05:35 US History 157.14
7  2011-08-22 17:06:25 US History 157.14
8  2011-08-23 17:07:05 US History 157.14
9  2011-08-24 17:07:03 US History 157.14
10 2011-08-25 17:08:27 US History 157.14
11 2011-08-19 17:05:35   Yearbook   0.00
12 2011-08-22 17:06:25   Yearbook   0.00
13 2011-08-23 17:07:05   Yearbook 100.00
14 2011-08-24 17:07:03   Yearbook 100.00
15 2011-08-25 17:08:27   Yearbook 100.00

With the following structure:

'data.frame':   15 obs. of  3 variables:
 $ date   : POSIXlt, format: "2011-08-23 17:07:05" "2011-08-24 17:07:03" ...
 $ subject: Factor w/ 3 levels "AP Biology","US History",..: 1 1 1 2 2 2 2 ...
 $ grade  : num  95.8 95.8 95.8 157.1 157.1 ...

When I try to plot this data:

> ggplot(data=grades, aes(date, grade, factor=subject)) + geom_line()
Error in if (length(range) == 1 || diff(range) == 0) { : 
  missing value where TRUE/FALSE needed

I don't know what I'm doing wrong here. I narrowed it down to the date handling by doing this:

ggplot(data=grades,
       aes(seq(length(grades[,1])),
           grade, color=subject)) + geom_line()

... but how do I do the date handling correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖哒 2024-12-08 15:55:57

ggplot2 仅支持 POSIXct 类的时间。 POSIXct 类将自 1970 年初(UTC 时区)以来的(有符号)秒数表示为数值向量。 POSIXlt 类是表示九个元素(secminhour 等)的向量命名列表。

您可以使用以下内容:

grades$date <- as.POSIXct(grades$date)

Only times of class POSIXct are supported in ggplot2. Class POSIXct represents the (signed) number of seconds since the beginning of 1970 (in the UTC timezone) as a numeric vector. Class POSIXlt is a named list of vectors representing nine elements (sec, min, hour, etc.).

You can use the following:

grades$date <- as.POSIXct(grades$date)
心如荒岛 2024-12-08 15:55:57

我想我已经明白了这一点。区别在于对 POSIXctPOSIXlt 的理解。 POSIXlt 是部分日历时间。 POSIXct 是自纪元以来的秒数。 strptime 返回一个 `POSIXct

为了使用此数据,我需要转换时间戳:

grades$date <- as.POSIXct(grades$date)

I think I've figured this out. The difference is in understanding POSIXct vs. POSIXlt. POSIXlt is the calendar time in parts. POSIXct is the number of seconds since the epoch. strptime returns a `POSIXct

In order to use this data, I need to convert the timestamps:

grades$date <- as.POSIXct(grades$date)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文