1

I am trying to create a graph with the following characteristics:

  • x-axis: time and date
  • y-axis: data

here you can download my dataframe: https://my.cloudme.com/josechka/data

I try to produce the graph using:

p <- ggplot(data,aes(x = Date, y = Var,group = 1)) 
        + geom_line() 
        + scale_x_date(labels = date_format("%m/%d/%Y")) 
        + scale_y_continuous(limits = c(0, 70000))    
p    

And I get the result:

Error: Invalid input: date_trans works with objects of class Date only

I am quite new in R and ggplot. What am I doing wrong?

2
  • 1
    use as.Date() on your date column first Commented Nov 17, 2014 at 17:03
  • ... as keegan says but with format="%m/%d/%Y" Commented Nov 17, 2014 at 17:34

2 Answers 2

2

As suggested you have to format the Date column into a Date object.

data$Date<-as.Date(data$Date, format="%d/%m/%Y")

Now you can use your script in order to create the plot:

library("ggplo2") 
library("scales")
p <- ggplot(data,aes(x = Date, y = Var,group = 1)) 
        + geom_line() 
        + scale_x_date(labels = date_format("%m/%d/%Y")) 
        + scale_y_continuous(limits = c(0, 70000))    
p

And this is the resulting plot:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for the comments. They helped me to find out the solution. Both comments allow to represent my data. However, there is small problem: data from the same day is grouped and it is not possible to see the daily behaviour of the variable. I tested to format the Date column using the next command:

as.POSIXct(data$Date, format="%d/%m/%Y %H:%M:%S")    

It worked out. However it is important to have the original data in the format d/m/Y h:m:s. Thanks very much for the comments which help me a lot to solve my problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.