Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error from ggplot2 plotting date data -- missing value where TRUE/FALSE needed

Tags:

r

ggplot2

posixct

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?

like image 616
Dustin Avatar asked Aug 26 '11 08:08

Dustin


People also ask

How do you fix missing value where True False needed?

This error occurs when you compare some value to NA in an if statement in R using the syntax x == NA. An if statement expects either a TRUE or FALSE value, so you need to use is.na(x) instead because this function always returns TRUE or FALSE.

Is Na missing value where True False needed?

When we will compare the value with NA(Not available) putting into an “if statement” or “while statement”, then the ”missing value where true/false needed” error occurs is that you are passing an invalid value to “if statement” or “while statement”.

Is Na true missing true missing or false Not missing?

Logical computations treat NA as a missing TRUE/FALSE value, and so may return TRUE or FALSE if the expression does not depend on the NA operand. The default method for anyNA handles atomic vectors without a class and NULL .


1 Answers

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)
like image 183
rcs Avatar answered Oct 27 '22 18:10

rcs