相关这篇文章: How to plot when Date and hour in separate field using ggplot in R
我有数据框:
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 ...
我想要做的是使用ggplot2
绘制一个条形图,其中X轴=小时(沿轴指示日期),Y轴为total_sess,按照解决方案的帖子:
"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"))
'time'变量可以用作x变量,并且不一定需要按日期颜色(当然可以根据需要添加)。
ggplot(data = d, aes(x = time, y = total_sess)) +
geom_bar(stat='identity') +
theme_bw()" by @Henrik
我不确定如何组合日期和小时字段,并在类别因素时将它们转换为Posixct或posixlt?