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