0

Related this post: How to plot when Date and hour in separate field using ggplot in R

I have the data frame:

head(d[, c(1,2,7)], 10)
         date hour total_sess
2  2014-04-07   00        115
3  2014-04-07   01          3
4  2014-04-07   16          3
5  2014-04-07   21        115
6  2014-04-08   00        115
7  2014-04-08   06          3
8  2014-04-09   05          3
9  2014-04-09   11        201
10 2014-04-09   14          3
11 2014-04-09   20          3

>  str(d) 'data.frame': 55348 obs. of  9 variables:  $ date       :
> Factor w/ 3 levels "2014/04/07","2014/04/08",..: 1 1 1 1 1 1 1 1 1 1
> ...  $ hour       : Factor w/ 25 levels "00","01","02",..: 1 1 1 1 1 1
> 1 1 1 1 ...  $ total_sess : Factor w/ 711 levels "1","10","100",..:
> 594 413 209 1 1 327 1 1 1 48 ...

what I want to do is use ggplot2 to plot a bar chart with X-Axis = hour (indicating date along the Axis) and Y Axis as total_sess and per the post the solution was:

 "I would convert columns 'date' and 'hour' to as.POSIXct to use as x axis. In the data you show, 'hour' seems to be a character variable (leading zeroes), and you can create a time variable like this:

d$time <- as.POSIXct(paste0(d$date, " ", d$hour, ":00:00"))

The 'time' variable can be used as x variable, and colour by date isn't really necessary (but can of course be added if you wish).

ggplot(data = d, aes(x = time, y = total_sess)) +
  geom_bar(stat='identity') +
  theme_bw()" by @Henrik

I am not sure how to do combine the date and hour fields and convert them to Posixct or posixlt when they are of class Factors ?

Community
  • 1
  • 1
user3006691
  • 435
  • 3
  • 7
  • 16
  • `d$time <- as.POSIXct(paste0(d$date, " ", d$hour, ":00:00"))` works when both date and hour are factors. Have you even tried it? – David Arenburg Apr 09 '14 at 21:20
  • Weird , i keep trying when they are factors but i get this:> d$time <- as.POSIXct(paste0(d$date, " ", d$hour, ":00:00")) # paste date and time and convert to Posixct Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format – user3006691 Apr 09 '14 at 21:21
  • Try `d$time <- as.POSIXct(paste0(as.character(d$date), " ", as.character(d$hour), ":00:00")) ` – David Arenburg Apr 09 '14 at 21:26
  • @David Arenburg ... nope didn't work, get the same error, this is what I am doing d <- read.csv("cat /home/data*"),header=TRUE,sep=",",quote = "\"") there are a bunch of csv files names data1.csv, data2.csv etc ... and i am reading them like this and it wouldn't listen to the posixct conversion – user3006691 Apr 09 '14 at 21:37
  • This line doesn't make sennse. You closed the `read.csv` bruckets before the `header` and etc. Also your `str(d)` in the post doesn't make sense too because it shows that date has only 3 levels, while in your example it has atleast 4 – David Arenburg Apr 09 '14 at 21:47
  • I apologize for the confusion on factors, I corrected my example ... and also regarding the read.csv parenthesis .... but essentially I have 3 factors in date – user3006691 Apr 09 '14 at 21:55
  • @DavidArenburg sure np, I'm not sure if there is an issue when we concatenate two .csv files and then read them. because when I read a single .csv file I dont see this issue at all – user3006691 Apr 09 '14 at 22:02
  • I tried to replicate your error, but i dont get any error even if I make the date in `"%Y/%m/%d"` format. I've also tried to transfrom hours to `"00"` and make them as factors. Still no error. Anyway, your example is not representive of your real data. Please run `dput(d)` and paste here – David Arenburg Apr 09 '14 at 22:06
  • @DavidArenburg, I really apologize for causing you trouble here, I found my mistake. While I was cat'ing [cat /home/data*] the .csv files, I forgot to elminate the headers in each of the .csv files, due to which the POSIXct conversion wasn't working. I really appreciate your help and time on this though – user3006691 Apr 09 '14 at 22:21
  • Ok, glad you've solved it – David Arenburg Apr 09 '14 at 22:26

0 Answers0