ggplot plotting date time with POSIXct gives error

Hello everyone,

I have a problem with plotting my data in ggplot
The data is a data frame with three columns: date, value, variable

The data is already in posixct format, the value as numeric.

 $ date   : POSIXct, format: "2020-06-09 11:30:00" "2020-06-09 11:40:00" "2020-06-09 11:50:00" "2020-06-09 12:00:00" ...
 $ variable: Factor w/ 93 levels "AMSTE01_04","AMSTE01_06",..: 
 $ value   : num  17 17.1 17.2 17.2 17.2 ...

I would like to make a ggplot (geom_line). The problem is when I try script1 that I get an empty graph, and with script2 I get an error

script1 <- ggplot(mydata, aes("date", "value", color = "variable"))+
# here the result is an empty grid
script2 <- ggplot(mydata, aes("date", "value", color = "variable"))+
  geom_line() +
  scale_x_datetime(date_labels = "&Y-%m-%d %H:%M:%S")
# >Error: Invalid input: time_trans works with objects of class POSIXct only

I feel like ggplot is not able to read the data, but I dont know how to fix it.

Thanks in advance!


script1 <- ggplot(mydata, aes(x=date, 
y=value, color = variable))+

I did try that already, but I got the same results

As has already been pointed out, you should not have the quote marks inside the aes. ggplot is expecting the bare column names. Beyond that, it is difficult to help much without a reproducible example. Please can you run dput(head(mydata)) and provide the result.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.