Error message plotting timeseries with ggplot2

前端 未结 1 2053
-上瘾入骨i
-上瘾入骨i 2020-12-22 03:32

I am very new to ggplot2 and wondered whether anyone could help me with my simple problem. My sample dataframe is given as follows. I want to plot the time (hours and minu

相关标签:
1条回答
  • 2020-12-22 04:07

    The problem is due to the fact that your time variable is a character vector :

    R> class(df1$time)
    [1] "character"
    

    You must convet it to an object of class POSIXlt, for example like this :

    ggplot(df1, aes(x=strptime(time, "%H:%M"), y=Counts)) + geom_line()
    

    Or, much simpler, you can directly use your Date variable, without transformation :

    ggplot(df1, aes(x=Date, y=Counts)) + geom_line()
    

    enter image description here

    Better yet, you will see that ggplot2 automagically labels your x axis depending on your timespan along the axis.

    EDIT : if you want to define the x-axis limits, you can do something like :

    ggplot(df1, aes(x=Date, y=Counts)) + geom_line() + scale_x_datetime(limits=c(as.POSIXct("2012/12/07 04:00:00"),as.POSIXct("2012/12/07 10:00:00")))
    
    0 讨论(0)
提交回复
热议问题