3

Possible Duplicate:
Plot dates on the x axis and time on the y axis with ggplot2

I have these 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 plot 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 output

enter image description here

As you can see, the time and date is not arrange. I want the time to start from 7:00 am to 12:20 pm, and the date from April 19 to May 18. I tried using

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

But still I don't get the right plot.

And I can't find similar problems through the internet.

Any idea to help me solve this.

Thanks

Community
  • 1
  • 1
Al-Ahmadgaid Asaad
  • 1,172
  • 5
  • 13
  • 25
  • I was thinking of that too, but never tried yet. I'm trying now. – Al-Ahmadgaid Asaad May 19 '12 at 13:52
  • Sort will not work. You need to convert your dates with `as.Date` or `POSIXct` objects, and your time to `POSIXct` objects. Then the plot should work. If you post a reproducible example, someone might be able to help. – Andrie May 19 '12 at 14:02
  • I still can't achieved it, please help me arrange that. :( – Al-Ahmadgaid Asaad May 19 '12 at 14:38
  • Voting to close since this has nothing to do with ggplot2, and everything to do with dealing with real DateTimes and not just character (or factor) data. Read ?DateTimeClasses or the help for lubridate package. – mdsumner May 20 '12 at 09:23

2 Answers2

2

I will try a different approach with some wrangling in lubridate. Target plot:

date-hour plot

The code, including your data:

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
daedalus
  • 10,873
  • 5
  • 50
  • 71
  • I got it already, actually, almost the same with the one you did gauden. But I convert it to decimal hour in excel, and then I import it and plot it. But, I'm happy that you should me how to do it in R. Thank you so much Gauden! – Al-Ahmadgaid Asaad May 19 '12 at 15:53
  • Thanks you to those who made some effort for this question! – Al-Ahmadgaid Asaad May 19 '12 at 15:54
1
#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

user1317221_G
  • 15,087
  • 3
  • 52
  • 78
  • this is like what @andrie said but I made your date and arrival one `POSIXct` object first then took out the dates and times for plotting. I just worked out this route first. – user1317221_G May 19 '12 at 15:08