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 ?