R Programming: How to arrange tick values ​​in a datetime graph in ggplot

Possible duplicate:
Date dates on the x axis and time on the y axis with ggplot2

I have this data,

Arrival Date
7:50    Apr-19
7:45    Apr-20
7:30    Apr-23
7:30    Apr-24
7:55    Apr-25
7:20    Apr-26
7:30    Apr-27
7:50    Apr-28
8:00    Apr-30
7:45    May-2
8:30    May-3
8:06    May-4
8:25    May-7
7:35    May-8
7:45    May-9
8:02    May-10
7:53    May-11
8:39    May-14
8:14    May-15
8:08    May-16
8:27    May-17
8:20    May-18
12:00   Apr-19
12:00   Apr-20
12:00   Apr-23
12:00   Apr-24
12:00   Apr-25
12:00   Apr-26
12:00   Apr-27
12:00   Apr-28
11:50   Apr-30
12:00   May-2
11:45   May-3
11:50   May-4
12:00   May-7
11:50   May-8
11:55   May-9
12:10   May-10
11:53   May-11
11:54   May-14
11:40   May-15
11:54   May-16
11:45   May-17
12:00   May-18

And I want to build it using ggplot,

This is what I did

OJT <- read.csv(file = "Data.csv", header = TRUE)

qplot(Date,Arrival, data = OJT, xlab = expression(bold("Date")), ylab = expression(bold("Time"))) + theme_bw() + opts(axis.text.x=theme_text(angle=90)) +geom_point(size = 2, colour = "black", fill = "red", pch = 21)

And here is the conclusion

enter image description here

As you can see, the time and date are not arranged. I want the time to start from 7:00 to 12:20 and from April 19 to May 18. I tried to use

as.Date(strptime(OJT$Date,"%m-%dT"))

But still I do not get the desired plot.

And I can not find such problems through the Internet.

Any idea to help me solve this problem.

thank

+3
source share
2 answers

I will try a different approach with some wrangling at lubridate. Target Schedule:

date-hour plot

Code including your details:

library("ggplot2")
library("lubridate")

df <- read.table(text = "Arrival Date
7:50    Apr-19
7:45    Apr-20
7:30    Apr-23
7:30    Apr-24
7:55    Apr-25
7:20    Apr-26
7:30    Apr-27
7:50    Apr-28
8:00    Apr-30
7:45    May-2
8:30    May-3
8:06    May-4
8:25    May-7
7:35    May-8
7:45    May-9
8:02    May-10
7:53    May-11
8:39    May-14
8:14    May-15
8:08    May-16
8:27    May-17
8:20    May-18
12:00   Apr-19
12:00   Apr-20
12:00   Apr-23
12:00   Apr-24
12:00   Apr-25
12:00   Apr-26
12:00   Apr-27
12:00   Apr-28
11:50   Apr-30
12:00   May-2
11:45   May-3
11:50   May-4
12:00   May-7
11:50   May-8
11:55   May-9
12:10   May-10
11:53   May-11
11:54   May-14
11:40   May-15
11:54   May-16
11:45   May-17
12:00   May-18", header=TRUE)

df$Date <- paste('2012-',df$Date, sep='')
df$Full <- paste(df$Date, df$Arrival, sep=' ')
df$Full <- ymd_hm(df$Full)
df$decimal.hour <- hour(df$Full) + minute(df$Full)/60

p <- ggplot(df, aes(x=Full, y=decimal.hour)) +
    geom_point()
p
+2
source
#make some data in your kind of format:

tS <- dummySeries()
a<-rownames(tS)
x<-c(a,a)
y<-1:24
dat<-as.data.frame(cbind(x,y))

#get it in the format for the plot

v<-paste(dat$x,dat$y, sep=" ") 
v2<-as.POSIXct(strptime(v, "%Y-%m-%d %H",tz="GMT"))
v3<-sort(v2)
hrs<-strftime(v2,"%H")
days<-strftime(v2,"%Y-%m-%d")
final<-data.frame(cbind(days,hrs))
qplot(days,hrs,data=final) + geom_point()

#ooooff... I bet this can be done much cleaner...i know little about 
#time series data.

enter image description here

+1
source

All Articles