8

外部ソースから取得しているいくつかの成績をプロットしようとしています。日付形式は次のようになります。

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

...しかし、どうすれば日付を正しく処理できますか?

4

2 に答える 2

12

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)
于 2011-08-26T08:36:59.817 に答える
2

私はこれを理解したと思います。違いは、理解と理解にありPOSIXctますPOSIXltPOSIXlt部分単位のカレンダー時間です。 POSIXctエポックからの秒数です。 strptime`POSIXct を返します

このデータを使用するには、タイムスタンプを変換する必要があります。

grades$date <- as.POSIXct(grades$date)
于 2011-08-26T08:36:20.067 に答える