如何用R中的ggplot2绘制时间序列数据

时间:2016-06-09 01:34:52

标签: r ggplot2 time-series

我的数据看起来像这样。

head(histogram)
  year month day create verified trans
1 2015    12  10      2        2     2
2 2015    12  14      3        1    NA
3 2016     1   6      1       NA    NA
4 2016     1  15      1       NA    NA
5 2016     1  17      1        1    NA
6 2016     1  25      1       NA    NA

年,月,日在不同的列中。 我希望按周绘制一个条形图分组。

例如,2016-1-1到2016-1-6的数据将在x轴上分组以产生3个柱:所有创建的总和对应于create,verified,trans。我更喜欢使用ggplot2,但一切都会好的。

1 个答案:

答案 0 :(得分:4)

如果您想使用时间序列和POSIX,我建议使用ggplot2格式。

请注意,你必须处理00周,这是1月的第一天结束12月的第52周。

## Fake data / without a reproducible example
set.seed(1)
df = data.frame(year = c(rep(2015,14), rep(2016,21)), 
                month = c(rep(12,14), rep(01,21)), day = c(seq(18,31,1), seq(01,21,1)), 
                create = sample(c(1,2,3,NA),35, replace = T, prob = c(0.3,0.3,0.3,0.1)), 
                verified = sample(c(1,2,3,NA),35, replace = T, prob = c(0.1,0.1,0.1,0.7)), 
                trans = sample(c(1,2,3,NA),35, replace = T, prob = c(0.1,0.2,0.1,0.6)))

# Add of week information
df$date_posix = as.POSIXct(paste0(df$year, "-", df$month, "-", df$day))
df$week = strftime(df$date_posix ,format = "%W") 

# summarize
require(plyr)
#> Le chargement a nécessité le package : plyr
df_sum = ddply(df, "week", summarize, 
create_sum = sum(create, na.rm = T), 
verified_sum = sum(verified, na.rm = T), 
trans_sum = sum(trans, na.rm = T))

# melt
require(reshape2)
#> Le chargement a nécessité le package : reshape2
df_sum_melt = melt(df_sum, id = "week")

# plot
require(ggplot2)
#> Le chargement a nécessité le package : ggplot2
ggplot(df_sum_melt, aes(x = week, y = value, fill = variable)) + 
geom_bar(stat = "identity", position = "dodge")

reprex package(v0.2.0)创建于2018-09-18。

编辑(整齐的方式)

library(tidyverse)
library(lubridate)
#> 
#> Attachement du package : 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
set.seed(1)
tibble(year = c(rep(2015,14), rep(2016,21)), 
       month = c(rep(12,14), rep(01,21)), day = c(seq(18,31,1), seq(01,21,1)), 
       create =     sample(c(1,2,3,NA),35, replace = T, prob = c(0.3,0.3,0.3,0.1)), 
       verified  = sample(c(1,2,3,NA),35, replace = T, prob = c(0.1,0.1,0.1,0.7)), 
       trans  = sample(c(1,2,3,NA),35, replace = T, prob = c(0.1,0.2,0.1,0.6))) %>%
  mutate(date_posix = as.Date(paste0(year, "-", month, "-", day)),
         week = lubridate::week(date_posix)) %>%
  group_by(week) %>%
  summarise(create_sum = sum(create, na.rm = T), 
            verified_sum = sum(verified, na.rm = T), 
            trans_sum = sum(trans, na.rm = T)) %>%
  gather(variable, value, -week) %>%
  ggplot(., aes(x = factor(week), y = value, fill = variable)) + 
  geom_bar(stat = "identity", position = "dodge")

reprex package(v0.2.0)创建于2018-09-18。