【问题标题】:Plot of cumulative count over time?随着时间的推移累积计数图?
【发布时间】:2015-09-18 00:52:00
【问题描述】:

我想在9/8/2015 00:00:00 上从零开始绘制两条线。我的数据如下所示:

df1 <-
  structure(
    list(
      Timestamp = structure(
        c(
          1441814593, 1441818193,
          1441821398, 1441821449, 1441828375, 1441873127, 1441813676, 1441837436,
          1441843661, 1441885583, 1441966341, 1441985621, 1442048926, 1442321691,
          1442321740, 1442328339, 1442329081, 1442349761, 1442391375, 1442408140,
          1442417679, 1442496854, 1442506500
        ), tzone = "UTC", class = c("POSIXct",
                                    "POSIXt")
      ), Group = c(
        1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
        2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
      )
    ), .Names = c("Timestamp",
                  "Group"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,-23L)
  )

我基本上想要一个喜欢group==1 和一个喜欢group==2。另外我想绘制垂直线on 9/119/15。在 R 中执行此操作的最佳方法是什么?现在我有这个:

library(taucharts)
df1 %>% group_by(Group) %>% 
  arrange(Timestamp) %>% 
  mutate(count = row_number()) %>% 
  tauchart() %>% 
  tau_line("Timestamp", "count", "Group") %>% 
  tau_legend() %>% tau_tooltip() %>% 
  tau_guide_x(auto_scale=TRUE, label="Timestamp", tick_format="%b %d")

这不是我想要的。首先,我无法添加垂直线。另外,我希望这些日子不要重复。

【问题讨论】:

    标签: r plot dplyr


    【解决方案1】:

    你可以使用ggplot2:

    library(ggplot2)
    library(dplyr)
    df1 <- df1 %>% group_by(Group) %>%
                   mutate(number = row_number())
    ggplot(df1, aes(x = Timestamp, y = number, col = factor(Group))) +
           geom_line() +
           geom_vline(xintercept = as.numeric(c(as.POSIXct("2015-09-11 00:00:00"), 
                                                as.POSIXct("2015-09-15 00:00:00"))))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 2019-12-22
      相关资源
      最近更新 更多