累积计数随时间变化的情节?

时间:2015-09-18 00:52:01

标签: r plot dplyr

我想在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")

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

1 个答案:

答案 0 :(得分: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"))))

enter image description here

相关问题