【问题标题】:R - plotting multiple time series, same x-axis values, but time is mixed within data frameR - 绘制多个时间序列,相同的 x 轴值,但时间在数据框中混合
【发布时间】:2016-07-17 23:32:56
【问题描述】:

我正在尝试绘制以下数据框,其中有 3 个不同的时间序列(由 user0、user1 和 user2 标识)。每行都有一个用户标识符、日期和一个值。

> df
   userId       date steps
1   user0 2016-03-24   794
2   user0 2016-03-25   562
3   user0 2016-03-26   682
4   user0 2016-03-27   722
5   user0 2016-03-28   883
6   user1 2016-03-24  3642
7   user1 2016-03-25  3776
8   user1 2016-03-26  3585
9   user1 2016-03-27  3585
10  user1 2016-03-28  3471
11  user2 2016-03-24  5959
12  user2 2016-03-25  5933
13  user2 2016-03-26  5802
14  user2 2016-03-27  6094
15  user2 2016-03-28  5903
> dput(df)
structure(list(userId = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("user0", "user1", 
"user2"), class = "factor"), date = structure(c(16884, 16885, 
16886, 16887, 16888, 16884, 16885, 16886, 16887, 16888, 16884, 
16885, 16886, 16887, 16888), class = "Date"), steps = c(794L, 
562L, 682L, 722L, 883L, 3642L, 3776L, 3585L, 3585L, 3471L, 5959L, 
5933L, 5802L, 6094L, 5903L)), .Names = c("userId", "date", "steps"
), row.names = c(NA, -15L), class = "data.frame")

我想使用不同的颜色和以日期为 x 轴绘制所有时间序列(无论有多少由 userId 字段标识)。我尝试了以下方法,但如您所见,日期在 x 轴上重复。

plot(df$steps, axes=F, xlab="", ylab="Steps", ylim=c(0,max(df$steps)))
axis(2)
axis(1, at = seq_along(df$date), labels = df$date, las = 2, cex.axis = 0.70)
box()

我查看了其他帖子,例如“Plot multiple lines (data series) each with unique color in R”和“Plotting multiple time series on the same plot using ggplot()”,但它们没有我的时间变量与其他数据混合的问题。

非常感谢使用带和不带 ggplot 的颜色线的解决方案。

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    使用 ggplot:

    library(ggplot2)
    ggplot(df, aes(x = date, y = steps, colour = userId)) + geom_line()
    


    等效(但仍然很丑)的基本 R 版本需要更多的工作:

    plot(0, type = 'n', axes = FALSE, xlab = 'date', ylab = 'steps',
         xlim = c(min(df$date), max(df$date)), 
         ylim = c(min(df$steps) - 100, max(df$steps) + 100))
    axis.Date(1, df$date, format = '%F')    # `axis.Date` is helpful here
    axis(2, seq(0, max(df$steps + 500), 500))
    box()
    lapply(split(df, df$userId), function(x){lines(x$date, x$steps, 
                                                   col = as.numeric(substr(x$userId, 5, 5)) + 1)})
    # `paste` extra space to align legend correctly...*sigh*
    legend('bottomright', paste(levels(df$userId), '   '), col = 1:3, lty = 1)
    

    请注意,它需要进行一些微调。

    【讨论】:

      【解决方案2】:

      这是一个基本的 R 版本:

      plot(0, 0, type = "n", xlim = range(df$date), ylim = c(0, max(df$step)), axes = FALSE, xlab = "", ylab = "steps")
      axis(2, las = 1)
      axis(1, at = df$date, labels = df$date, las = 2, cex.axis = 0.70)
      box()
      
      cols <- c("red", "green", "blue")
      for (i in 1:length(unique(df$userId)))
        with(df[df$userId == unique(df$userId)[i], ], lines(date, steps, col = cols[i]))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-09
        相关资源
        最近更新 更多