Time-series/cumulative 使用 ggplot2 的数据图

Time-series/cumulative data plots using ggplot2

在此之前,我要感谢您阅读我的post。

我在 R 代码语言方面不是很自主,在过去的几天里,我一直在处理关于在一个图中绘制相同数据的多个时间序列线的问题。我已经尝试自己理解它(在 google 上搜索,甚至这里有几个线程),但几乎没有 none 成功。

在我的数据库中,我有创建寄存器时的数据——以及该寄存器是有效还是无效。我的 objective 是做一个绘图,其中我将有一条线,其中包含随时间推移的总数​​,以及随时间推移有效和无效寄存器的累积值。

在我的多次尝试中,我能够只用一段时间内完成的总寄存器来做一个时间序列 - Plot obtained with my script - 但我无法用累积时间序列绘制它有效行和无效行一起。我使用的脚本是:


#Graph
ggplot(data = holocron %>% group_by(Date) %>% summarise(n = n()),
       aes(x = Date, y = n)) +
       ylab("Number of register") +
       xlab("Date") +
       ggtitle("Cumulative number of valid registers through time") +
       geom_line(aes(y = cumsum(n)),linetype= "dotted", color="black", size=1)+
       geom_point(shape=21, aes(y = cumsum(n)), fill="yellow", color="black", size=2)+
       scale_x_date(date_breaks = "months", date_labels = "%m/%Y")+
       theme_bw()+
       theme(text=element_text(size=20,  family="Palatino Linotype", color="Black"),
       plot.title = element_text(hjust=0.5),
       axis.title.x = element_text(color="Black", vjust = -0.5, size=18, family = "Palatino Linotype"),
       axis.title.y = element_text(color="Black", vjust = 2, size=18, family = "Palatino Linotype"))

但是,对于这样的脚本,我已经尝试多次更改它但没有成功...我知道这可能是一个真正的新手错误,但我就是无法理解它.

我的数据是这样的(下面显示的table不代表我的数据,因为我找不到link.txt文件,我只是做了一个类似的table):

Registers Valid Invalid Date
1 0 1 28/11/2019
2 0 1 28/11/2019
3 1 0 28/11/2019
4 0 1 29/11/2019
5 0 1 1/12/2019
6 0 1 2/12/2019
7 1 0 2/12/2019
8 1 0 4/12/2019
9 1 0 6/12/2019
10 0 1 6/12/2019
11 0 1 8/12/2019
12 0 1 9/12/2019
13 0 1 9/12/2019
14 1 0 9/12/2019
15 1 0 9/12/2019
16 1 0 9/12/2019
17 1 0 10/12/2019
18 0 1 12/12/2019
19 1 0 12/12/2019
20 0 1 14/12/2019
21 1 0 15/12/2019
22 1 0 16/12/2019
23 1 0 17/12/2019
24 0 1 18/12/2019

感谢您抽出宝贵时间,我很想准备好您的建议!

安德烈·米拉

这是你想要的吗?

library(ggplot2)
library(dplyr)
library(tidyr)
library(lubridate)

holocron %>%
  mutate(Date = dmy(Date)) %>%
  arrange(Date) %>%    # Just in case not ordered already
  mutate(Valid_Cumulative = cumsum(Valid),
         Invalid_Cumulative = cumsum(Invalid)) %>%
  pivot_longer(cols = c(Registers, Valid_Cumulative, Invalid_Cumulative)) %>%
  
ggplot(aes(Date, value, color = name)) +
  geom_line()