【问题标题】:How to plot time series data with ggplot2 in R如何在 R 中使用 ggplot2 绘制时间序列数据
【发布时间】:2016-06-09 01:34:51
【问题描述】:

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

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 的总和,分别对应于 create、verified、trans。我更喜欢使用 ggplot2,但什么都可以。

【问题讨论】:

    标签: r ggplot2 time-series


    【解决方案1】:

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

    请注意,您必须处理到第 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 年 9 月 18 日创建。

    编辑(tidyverse方式)

    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 年 9 月 18 日创建。

    【讨论】:

      猜你喜欢
      • 2013-01-30
      • 2021-03-17
      • 2017-03-04
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多