繁体   English   中英

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

[英]How to plot time series data with ggplot2 in R

我有一个看起来像这样的数据。

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条:与创建,验证,反式对应的所有创建的总和。 我更喜欢使用ggplot2,但一切都很好。

当您要使用时间序列和ggplot2时,建议使用POSIX格式。

请注意,您必须处理第00周,这是一月的第一天,即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包 (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包 (v0.2.0)创建于2018-09-18。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM