Blame view

ISO27001effectiveness/R/ReportGraphs.R 38.5 KB
Miguel Tuñón authored
1
2
3
4
5

#----------------------------------------------------------------
#-------------------------General evolution----------------------
#----------------------------------------------------------------
Miguel Tuñón authored
6
Miguel Tuñón authored
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#' Return graph representing the general attacks evolution by year
#'
#' @param Attacks data.frame with procesed source data
#' @param point.vjust Vector of vertical just to each point label
#' @param point.hjust Vector of Horizontal just to each point label
#' @param iso2013.y Vertical position to ISO27001:2013 label
#' @param iso2013.label.hjust Horizontal just to ISO27001:2013 label
#' @param smooth.x Horizontal position to smooth slope label
#' @param smooth.y Vertical position to smooth slope label
#' @param smooth.label.hjust Horizontal just to smooth slope label
#'
#' @return list(graph, slope)
#' @export
GetAttacksEvolution <- function(Attacks,
                                point.vjust = 0,
                                point.hjust = 0,
                                iso2013.y = 0,
                                iso2013.label.hjust = 0,
                                smooth.x = 0,
                                smooth.y = 0,
                                smooth.label.hjust = 0){

  #Extracting year from date
  attacks.evol <- dplyr::mutate(Attacks, Year = format(Attacks$Date, "%Y")) %>%
                  dplyr::group_by(Year)#Grouping by year
  #Counting attacks each year
Miguel Tuñón authored
33
34
35
  attacks.evol <- as.data.frame(table(attacks.evol$Year))
  colnames(attacks.evol) <- c("Year","Attacks")
Miguel Tuñón authored
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  #Slope of smooth
  attacks.evol.slope <- lm(formula = Attacks ~ as.numeric(Year),
                           data = attacks.evol)$coef[[2]]
  #Graph
  attacks.evol.graph <- ggplot2::qplot(main = "Cyberattacks evolution",
                                       x = attacks.evol$Year,
                                       y = attacks.evol$Attacks,
                                       group = 1,
                                       xlab = "Years",
                                       ylab = "Attacks",
                                       data = attacks.evol,
                                       geom = "line")  +
                        ggplot2::geom_point() +
                        ggplot2::geom_label(aes(label=attacks.evol$Attacks),
                                   vjust = point.vjust,
                                   hjust = point.hjust)+
                        ggplot2::theme(plot.title = element_text(hjust = 0.5)) +
                        ggplot2::geom_smooth(method = "lm", se = FALSE) +
                        ggplot2::geom_label(aes(x = smooth.x, y = smooth.y,
                                       label = paste("Slope =",
                                                     signif(attacks.evol.slope))),
                                   color = "blue",
                                   hjust = smooth.label.hjust) +
                        ggplot2::geom_vline(xintercept = 3,
                                            color = "Red",
                                            linetype = "longdash") +
                        ggplot2::geom_label(aes(x = "2014", y = iso2013.y,
                                                label = "ISO27001:2013 effects"),
                                            color = "Red",
                                            hjust = iso2013.label.hjust)

  list(attacks.evol.graph, attacks.evol.slope)
Miguel Tuñón authored
68
69
70

}
Miguel Tuñón authored
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#' Return graph representing the general ISO27001 evolution
#'
#' @param Cert_PerCountry data.frame with procesed source data
#' @param point.vjust Vector of vertical just to each point label
#' @param point.hjust Vector of Horizontal just to each point label
#' @param iso2013.y Vertical position to ISO27001:2013 label
#' @param iso2013.label.hjust Horizontal just to ISO27001:2013 label
#' @param smooth.x Horizontal position to smooth slope label
#' @param smooth.y Vertical position to smooth slope label
#' @param smooth.label.hjust Horizontal just to smooth slope label
#'
#' @return list(graph, slope)
#' @export
GetCertsEvolution <- function(Cert_PerCountry,
                              point.vjust = 0,
                              point.hjust = 0,
                              iso2013.y = 0,
                              iso2013.label.hjust = 0,
                              smooth.x = 0,
                              smooth.y = 0,
                              smooth.label.hjust = 0){

  #Years from columns to rows in 1 column
  Certs.evol <- tidyr::gather(Cert_PerCountry, "Year", "Count", 2:6)
  #Group by year
  Certs.evol <- dplyr::group_by(Certs.evol, Year)
  #Sum counts to 1 row for each year
  Certs.evol <- dplyr::summarise(Certs.evol, Count = sum(Count))
  #Removing X from years
  Certs.evol$Year <- substr(Certs.evol$Year, 2, 5)

  #Slope of smooth
  Certs.evol.slope <- lm(formula = Count ~ as.numeric(Year),
                           data = Certs.evol)$coef[[2]]
  #Graph
  Certs.evol.graph <- ggplot2::qplot(main = "ISO 27001 evolution",
Miguel Tuñón authored
107
                           x = Certs.evol$Year,
Miguel Tuñón authored
108
                           y = Certs.evol$Count,
Miguel Tuñón authored
109
110
111
112
113
114
                           group = 1,
                           xlab = "Years",
                           ylab = "Certifications",
                           data = Certs.evol,
                          geom = "line") +
            geom_point() +
Miguel Tuñón authored
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
            geom_label(aes(label=Certs.evol$Count),
                       vjust = point.vjust,
                       hjust = point.hjust)+
            theme(plot.title = element_text(hjust = 0.5)) +
            ggplot2::geom_smooth(method = "lm", se = FALSE) +
            ggplot2::geom_label(aes(x = smooth.x, y = smooth.y,
                                    label = paste("Slope =",
                                                  signif(Certs.evol.slope))),
                                color = "blue",
                                hjust = smooth.label.hjust)+
            ggplot2::geom_vline(xintercept = 3,
                                color = "Red",
                                linetype = "longdash") +
            ggplot2::geom_label(aes(x = "2014", y = iso2013.y,
                                    label = "ISO27001:2013 changes"),
                                color = "Red",
                                hjust = iso2013.label.hjust)

  list(Certs.evol.graph, Certs.evol.slope)
Miguel Tuñón authored
134
135
136

}
Miguel Tuñón authored
137
138
139
140
141
142
#' Return graph representing the general attacks evolution by month
#'
#' @param Attacks data.frame with procesed source data
#'
#' @return graph
#' @export
Miguel Tuñón authored
143
144
GetAttacksMonthEvolution <- function(Attacks){
Miguel Tuñón authored
145
146
147
148
  #Extract Year - Month from date
  attacks.evol <- mutate(Attacks, Year = format(Attacks$Date, "%Y-%m")) %>%
                  group_by(Year)
  #Group by Year - Month
Miguel Tuñón authored
149
150
151
  attacks.evol <- as.data.frame(table(attacks.evol$Year))
  colnames(attacks.evol) <- c("Year","Attacks")
Miguel Tuñón authored
152
153
154
155
156
157
158
159
160
  #Graph
  ggplot2::qplot(main = "Cyberattacks evolution",
                 x = attacks.evol$Year,
                 y = attacks.evol$Attacks,
                 group = 1,
                 xlab = "Months",
                 ylab = "Attacks",
                 data = attacks.evol,
                 geom = "line")  +
Miguel Tuñón authored
161
162
163
    geom_point() +
    theme(plot.title = element_text(hjust = 0.5)) +
    geom_smooth(method = 'loess') +
Miguel Tuñón authored
164
165
166
167
168
    scale_x_discrete(labels = c("2012/01", "", "", "", "", "", "2012/07", "", "", "", "", "",
                                "2013/01", "", "", "", "", "", "2013/07", "", "", "", "", "",
                                "2014/01", "", "", "", "", "", "2014/07", "", "", "", "", "",
                                "2015/01", "", "", "", "", "", "2015/07", "", "", "", "", "",
                                "2016/01", "", "", "", "", "", "2016/07", "", "", "", "", ""))
Miguel Tuñón authored
169
170
171
172


}
Miguel Tuñón authored
173
174
175
#----------------------------------------------------------------
#-------------------------Attack type evolution------------------
#----------------------------------------------------------------
Miguel Tuñón authored
176
Miguel Tuñón authored
177
Miguel Tuñón authored
178
179
180
181
182
183
184
185
186
187
188
189
190
#' Return pie graph to show % of each attack type
#'
#' @param Attacks data.frame with procesed source data
#' @param label.vjust Vector of vertical just to each portion label
#' @param label.hjust Vector of horizontal just to each portion label
#'
#' @return list(graph, AttackTypeShowedList)
#' @export
GetAttackTypePie <- function (Attacks,
                              label.vjust = 0,
                              label.hjust = 0){

  #Group by attack type
Miguel Tuñón authored
191
  attack.pie <- group_by(Attacks, Attack.standar)
Miguel Tuñón authored
192
  #Counting rows for each attack type
Miguel Tuñón authored
193
194
195
  attack.pie <- as.data.frame(table(attack.pie$Attack.standar))
  attack.pie <- setNames(attack.pie, c("Attack", "Count"))
Miguel Tuñón authored
196
  #Removing rows without attack type defined
Miguel Tuñón authored
197
  attack.pie <- attack.pie[attack.pie$Attack != "",]
Miguel Tuñón authored
198
  #Removing attacks with less than 1% of representation from total
Miguel Tuñón authored
199
  attack.pie <- attack.pie[attack.pie$Count > (sum(attack.pie$Count) * 0.01),]
Miguel Tuñón authored
200
201
  #Calc % of each attack type
  attack.pie <- mutate(attack.pie, Perc=paste(round(100 * attack.pie$Count / sum(attack.pie$Count), 2), "%"))
Miguel Tuñón authored
202
203
Miguel Tuñón authored
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  #Graph
  graph1 <- ggplot(data=attack.pie,
                  aes(x=factor(1),
                   y=Count,
                   fill=Attack)) +
            geom_col(width = 1, color='black') +
            geom_label(aes(label = attack.pie$Perc),
                        vjust = label.vjust,
                        hjust = label.hjust) +
            coord_polar(theta="y") +
            scale_x_discrete(labels = c("")) +
            scale_y_discrete(labels = c("")) +
            theme(plot.title = element_text(hjust = 0.5),
                  axis.title.x=element_blank(),
                  axis.title.y=element_blank()) +
            ggtitle("Attack types")

  #Returning graph and attack list
  list(graph1, unique(attack.pie$Attack))
Miguel Tuñón authored
223
224
225
}
Miguel Tuñón authored
226
227
228
229
230
231
232
233
234
235
236
237
#' Return graph to show the evolution of a attack types list
#'
#' @param Attacks data.frame with procesed source data
#' @param TypeList List with attack types to show
#'
#' @return list(graph, data.frame with TypeList attacks data by year)
#' @export
GetAttackTypeEvolution <- function(Attacks, TypeList){
  #Obtaining year from date
  Attacks.pre <- dplyr::mutate(Attacks, Year = format(Attacks$Date, "%Y")) %>%
                group_by(Year, Attack.standar) #group by attack type
  #Count rows for each attack type and each year
Miguel Tuñón authored
238
239
  Attacks.pre <- as.data.frame(table(Attacks.pre$Year, Attacks.pre$Attack.standar))
  Attacks.pre <- setNames(Attacks.pre, c("Year", "Attack", "Count"))
Miguel Tuñón authored
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  #Removing rows without an attack type specified
  Attacks.pre <- Attacks.pre[Attacks.pre$Attack %in% TypeList,]

  #Graph
  graph1 <- ggplot(data = Attacks.pre,
                   aes(x = Year,
                       y = Count,
                       color = Attack,
                       group = Attack)) +
            geom_point() +
            geom_line() +
            theme(plot.title = element_text(hjust = 0.5)) +
            ggtitle("Attack type evolution") +
            labs(colour = "Attack type") + xlab("Years") + ylab("Attacks")
  #Return graph and used data to separate representation
  list(graph1, Attacks.pre)
Miguel Tuñón authored
256
257
258
259

  }
Miguel Tuñón authored
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#' Return graph to show the evolution of a single attack type and his smooth
#'
#' @param Attacks data.frame with procesed source data
#' @param AttackType Attack type to represent
#' @param smooth.x Horizontal position to smooth slope label
#' @param smooth.y Vertical position to smooth slope label
#' @param smooth.label.hjust Horizontal just to smooth slope label
#'
#' @return list(graph, slope)
#' @export
GetAttackTypeSigleEvolution <- function(Attacks,
                                        AttackType,
                                        smooth.x = 0,
                                        smooth.y = 0,
                                        smooth.label.hjust = 0){
  #Filtering for the AttackType specified
  attacks.evol <- Attacks[Attacks$Attack == AttackType,]

  #Cal slope
  slope1 <- lm(formula = Count ~ as.numeric(Year), data = attacks.evol)$coef[[2]]
  #Graph
  graph1 <- ggplot(data = attacks.evol,
                   aes(x = Year, y = Count, group = 1)) +
            geom_line() +
            geom_point() +
            theme(plot.title = element_text(hjust = 0.5)) +
            ggtitle(AttackType) +
            xlab("Years") + ylab("Attacks")+
            geom_smooth(method = "lm",
                        se = FALSE) +
            geom_label(aes(x = smooth.x, y = smooth.y,
                           label = paste("Slope =", signif(slope1, 5))),
                       color = "blue",
                       hjust = smooth.label.hjust)

  #Returning list(graph, slope)
  list(graph1, slope1)
Miguel Tuñón authored
297
298
299

}
Miguel Tuñón authored
300
301
302
303
#----------------------------------------------------------------
#-------------------------Geolocal evolution---------
#----------------------------------------------------------------
Miguel Tuñón authored
304
305
306
307
308
309
GetContinentPie <- function (Attacks,
                             Attacks.label.vjust = 0,
                             Attacks.label.hjust = 0,
                             Cert_PerCountry,
                             Certs.label.vjust = 0,
                             Certs.label.hjust = 0){
Miguel Tuñón authored
310
Miguel Tuñón authored
311
  #Group attacks by continent
Miguel Tuñón authored
312
  attack.pie <- group_by(Attacks, Continent)
Miguel Tuñón authored
313
  #Count attacks for each continent
Miguel Tuñón authored
314
315
  attack.pie <- as.data.frame(table(attack.pie$Continent))
  attack.pie <- setNames(attack.pie, c("Continent", "Count"))
Miguel Tuñón authored
316
  #Remove rows without Continent specified
Miguel Tuñón authored
317
  attack.pie <- attack.pie[attack.pie$Continent != "",]
Miguel Tuñón authored
318
319
  #Calc % of each continent
  attack.pie <- mutate(attack.pie, perc = round(100 * attack.pie$Count / sum(attack.pie$Count), 2))
Miguel Tuñón authored
320
Miguel Tuñón authored
321
  #Attacks graph
Miguel Tuñón authored
322
323
324
325
  graph1 <- ggplot(data=attack.pie,
                   aes(x=factor(1),
                       y=Count,
                       fill=Continent)) +
Miguel Tuñón authored
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
            geom_col(width = 1, color='black') +
            geom_label(aes(label=paste(attack.pie$perc, "%")),
                           vjust = Attacks.label.vjust,
                           hjust = Attacks.label.hjust) +
            coord_polar(theta="y") +
            scale_x_discrete(labels = c("")) +
            scale_y_discrete(labels = c("")) +
            theme(plot.title = element_text(hjust = 0.5),
                  axis.title.x=element_blank(),
                  axis.title.y=element_blank()) +
            ggtitle("Attacks")


  #Grouping certifications by continent
  cert.pie <- group_by(Cert_PerCountry, Continent)
  #Counting certificates for each continent
  cert.pie <- dplyr::summarise(cert.pie, Count = sum(X2011 + X2012 + X2013 + X2014 + X2015))
  #Remove rows without Continent specified
  cert.pie <- cert.pie[cert.pie$Continent != "",]
  #Calc % of each continent
  cert.pie <- mutate(cert.pie, perc = round(100 * cert.pie$Count / sum(cert.pie$Count), 2))

  #Certifications graph
Miguel Tuñón authored
349
350
351
352
  graph2 <- ggplot(data=cert.pie,
                   aes(x=factor(1),
                       y=Count,
                       fill=Continent)) +
Miguel Tuñón authored
353
354
355
356
357
358
359
360
361
362
363
364
365
366
            geom_col(width = 1, color='black') +
            geom_label(aes(label=paste(cert.pie$perc, "%")),
                           vjust = Certs.label.vjust,
                           hjust = Certs.label.hjust) +
            coord_polar(theta="y") +
            scale_x_discrete(labels = c("")) +
            scale_y_discrete(labels = c("")) +
            theme(plot.title = element_text(hjust = 0.5),
                  axis.title.x=element_blank(),
                  axis.title.y=element_blank()) +
            ggtitle("ISO 27001")

  list(graph1, unique(attack.pie[attack.pie$perc > 5,]$Continent),
       graph2, unique(cert.pie[cert.pie$perc > 5,]$Continent))
Miguel Tuñón authored
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
}

GetContinentAttacksEvolution <- function(Attacks){

  attacks.evol <- mutate(Attacks, Year = format(Attacks$Date, "%Y")) %>% group_by(Continent, Year)
  attacks.evol <- as.data.frame(table(attacks.evol$Continent, attacks.evol$Year))
  colnames(attacks.evol) <- c("Continent", "Year","Attacks")

  attacks.evol <- attacks.evol[attacks.evol$Continent != "Oceania",]
  attacks.evol <- attacks.evol[attacks.evol$Continent != "Africa",]

  graph1 <- ggplot2::qplot(main = "Cyberattacks evolution",
                           x = attacks.evol$Year,
                           y = attacks.evol$Attacks,
                           group = Continent,
                           xlab = "Years",
                           ylab = "Attacks",
                           data = attacks.evol,
                           geom = "point",
                           color = Continent)  +
    geom_line() +
    theme(plot.title = element_text(hjust = 0.5))

  graph1

}

GetContinentCertsEvolution <- function(Cert_PerCountry){

  certs.evol <- gather(Cert_PerCountry, "Year", "Certs", 2:6) %>% group_by(Continent, Year)

  certs.evol <- summarise(certs.evol,
                          #Continent = Continent,
                          #Year = Year,
                          Certs = sum(Certs))
  certs.evol$Year <- substr(certs.evol$Year,2,5)

  certs.evol <- certs.evol[certs.evol$Continent != "Oceania",]
  certs.evol <- certs.evol[certs.evol$Continent != "Africa",]

  graph1 <- ggplot2::qplot(main = "ISO 27001 evolution",
                           x = certs.evol$Year,
                           y = certs.evol$Certs,
                           group = Continent,
                           xlab = "Years",
                           ylab = "Certifications",
                           data = certs.evol,
                           geom = "point",
                           color = Continent)  +
    geom_line() +
    theme(plot.title = element_text(hjust = 0.5))

  graph1

}

GetContinentAttacksTopEvolution <- function(Attacks){
  attacks.evol <- mutate(Attacks, Year = format(Attacks$Date, "%Y")) %>% group_by(Year, Continent)
  attacks.evol <- as.data.frame(table(attacks.evol$Year, attacks.evol$Continent))
  attacks.evol <- setNames(attacks.evol, c("Year", "Continent", "Count"))
  attacks.evol <- attacks.evol[attacks.evol$Continent != "",]

  attacks.evol <- spread(attacks.evol, "Continent", "Count")

  graph1 <- ggplot(data = attacks.evol,
                   aes(x = Year, y = Americas, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("Americas") +
    xlab("Years") + ylab("Attacks")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))

  graph2 <- ggplot(data = attacks.evol,
                   aes(x = Year, y = Asia, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("Asia") +
    xlab("Years") + ylab("Attacks")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))

  graph3 <- ggplot(data = attacks.evol,
                   aes(x = Year, y = Europe, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("Europe") +
    xlab("Years") + ylab("Attacks")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))


  list(graph1, graph2, graph3)

}

GetContinentCertsTopEvolution <- function(Attacks){

  certs.evol <- gather(Cert_PerCountry, "Year", "Certs", 2:6) %>% group_by(Continent, Year)
Miguel Tuñón authored
472
Miguel Tuñón authored
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
  certs.evol <- summarise(certs.evol,
                          #Continent = Continent,
                          #Year = Year,
                          Certs = sum(Certs))
  certs.evol$Year <- substr(certs.evol$Year,2,5)

  certs.evol <- spread(certs.evol, "Continent", "Certs")

  graph1 <- ggplot(data = certs.evol,
                   aes(x = Year, y = Americas, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("Americas") +
    xlab("Years") + ylab("Certifications")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))

  graph2 <- ggplot(data = certs.evol,
                   aes(x = Year, y = Asia, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("Asia") +
    xlab("Years") + ylab("Certifications")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))

  graph3 <- ggplot(data = certs.evol,
                   aes(x = Year, y = Europe, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("Europe") +
    xlab("Years") + ylab("Certifications")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))


  list(graph1, graph2, graph3)

}

GetCountriesCol <- function(Attacks, Cert_PerCountry){

  certs.evol <- gather(Cert_PerCountry, "Year", "Certs", 2:6) %>% group_by(Continent, country_short)

  certs.evol <- summarise(certs.evol,
                          Certs = sum(Certs))

  graph1 <- ggplot2::ggplot(aes(x = reorder(country_short, Certs),
                                y = Certs),
                           data = certs.evol[certs.evol$Certs > (sum(certs.evol$Certs) * 0.02),]) +
          geom_col(aes(fill = Continent)) +
          theme(plot.title = element_text(hjust = 0.5)) +
          ggtitle("ISO 27001") +
          xlab("Country") + ylab("Certifications")

  attacks.evol <- mutate(Attacks, Year = format(Attacks$Date, "%Y")) %>% group_by(Year, Continent, Country)
  attacks.evol <- as.data.frame(table(attacks.evol$Year, attacks.evol$Continent, attacks.evol$Country))
  attacks.evol <- setNames(attacks.evol, c("Year", "Continent", "Country", "Count"))
  attacks.evol <- attacks.evol[attacks.evol$Continent != "",]
  attacks.evol <- attacks.evol[attacks.evol$Country != "",]
  attacks.evol <- attacks.evol[attacks.evol$Country != "AU",]

  attacks.evol <- attacks.evol[attacks.evol$Count > (sum((attacks.evol[attacks.evol$Country != "US",])$Count) * 0.01),]
  attacks.evol <- arrange(attacks.evol, Count)


  graph2 <- ggplot2::ggplot(aes(x = reorder(Country, Count),
                                y = Count),
                            data = attacks.evol) +
          geom_col(aes(fill = Continent)) +
          theme(plot.title = element_text(hjust = 0.5)) +
          ggtitle("Attacks") +
          xlab("Country") + ylab("Attacks")

  list(graph1, graph2)

  }
Miguel Tuñón authored
556
Miguel Tuñón authored
557
558
559
560
561
562
563
564
GetCountriesAttacksTopEvolution <- function(Attacks){
  attacks.evol <- mutate(Attacks, Year = format(Attacks$Date, "%Y")) %>% group_by(Year, Country)
  attacks.evol <- as.data.frame(table(attacks.evol$Year, attacks.evol$Country))
  attacks.evol <- setNames(attacks.evol, c("Year", "Country", "Count"))
  attacks.evol <- attacks.evol[attacks.evol$Country != "",]

  attacks.evol <- spread(attacks.evol, "Country", "Count")
Miguel Tuñón authored
565
  slope1 <- lm(formula = US ~ as.numeric(Year), data = attacks.evol)
Miguel Tuñón authored
566
567
568
569
570
571
572
573
574
  graph1 <- ggplot(data = attacks.evol,
                   aes(x = Year, y = US, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("US") +
    xlab("Years") + ylab("Attacks")+
Miguel Tuñón authored
575
576
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..)) +
    geom_label(aes(x = "2014", y = 700, label = signif(slope1$coef[[2]])), color = "blue")
Miguel Tuñón authored
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742

  graph2 <- ggplot(data = attacks.evol,
                   aes(x = Year, y = GB, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("GB") +
    xlab("Years") + ylab("Attacks")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))

  graph3 <- ggplot(data = attacks.evol,
                   aes(x = Year, y = IN, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("IN") +
    xlab("Years") + ylab("Attacks")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))

  graph4 <- ggplot(data = attacks.evol,
                   aes(x = Year, y = JP, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("JP") +
    xlab("Years") + ylab("Attacks")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))


  list(graph1, graph2, graph3, graph4)

}

GetCountriesCertsTopEvolution <- function(Attacks){

  certs.evol <- gather(Cert_PerCountry, "Year", "Certs", 2:6) %>% group_by(country_short, Year)

  certs.evol <- rbind(certs.evol[certs.evol$country_short == "US",],
                      certs.evol[certs.evol$country_short == "GB",],
                      certs.evol[certs.evol$country_short == "IN",],
                      certs.evol[certs.evol$country_short == "JP",])

  certs.evol <- summarise(certs.evol,
                          Certs = sum(Certs))
  certs.evol$Year <- substr(certs.evol$Year,2,5)

  certs.evol <- spread(certs.evol, "country_short", "Certs")

  graph1 <- ggplot(data = certs.evol,
                   aes(x = Year, y = US, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("US") +
    xlab("Years") + ylab("Certifications")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))

  graph2 <- ggplot(data = certs.evol,
                   aes(x = Year, y = GB, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("GB") +
    xlab("Years") + ylab("Certifications")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))

  graph3 <- ggplot(data = certs.evol,
                   aes(x = Year, y = IN, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("IN") +
    xlab("Years") + ylab("Certifications")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))


  graph4 <- ggplot(data = certs.evol,
                   aes(x = Year, y = JP, group = 1)) +

    geom_line() +
    geom_point() +

    theme(plot.title = element_text(hjust = 0.5)) +
    ggtitle("JP") +
    xlab("Years") + ylab("Certifications")+
    stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..))


  list(graph1, graph2, graph3, graph4)

}


#----------------------------------------------------------------
#-------------------------gelocal/type---------------------------
#----------------------------------------------------------------

GetContinentAttackPie <- function (Attacks){

  attack.pie <- group_by(Attacks, Attack.standar, Country)
  attack.pie <- as.data.frame(table(attack.pie$Country, attack.pie$Attack.standar))
  attack.pie <- setNames(attack.pie, c("Country", "Attack", "Count"))

  attack.pie <- attack.pie[attack.pie$Country != "",]
  attack.pie <- attack.pie[attack.pie$Attack != "",]

  attack.pie$Attack <- as.character(attack.pie$Attack)
  attack.pie[attack.pie$Attack != "DDoS" &
               attack.pie$Attack != "Defacement" &
               attack.pie$Attack != "Injection",]$Attack <- "Otros"
  attack.pie$Attack <- as.factor(attack.pie$Attack)

  attack.pie <- group_by(attack.pie, Attack, Country)
  attack.pie <- summarise(attack.pie, Count = sum(Count))

  attack.pie.US <- attack.pie[attack.pie$Country == "US",]
  attack.pie.JP <- attack.pie[attack.pie$Country == "JP",]


  graph1 <- ggplot(data=attack.pie.US,
                   aes(x=factor(1),
                       y=Count,
                       fill=Attack)) +
    geom_col(width = 1, color='black') +
    geom_label(aes(label=paste(round(100 * attack.pie.US$Count / sum(attack.pie.US$Count), 2), "%")),
               vjust=c(0),
               hjust=c(0)) +
    coord_polar(theta="y") +
    scale_x_discrete(labels = c("")) +
    scale_y_discrete(labels = c("")) +
    theme(plot.title = element_text(hjust = 0.5),
          axis.title.x=element_blank(),
          axis.title.y=element_blank()) +
    ggtitle("US")

  graph2 <- ggplot(data=attack.pie.JP,
                   aes(x=factor(1),
                       y=Count,
                       fill=Attack)) +
    geom_col(width = 1, color='black') +
    geom_label(aes(label=paste(round(100 * attack.pie.JP$Count / sum(attack.pie.JP$Count), 2), "%")),
               vjust=c(0),
               hjust=c(0)) +
    coord_polar(theta="y") +
    scale_x_discrete(labels = c("")) +
    scale_y_discrete(labels = c("")) +
    theme(plot.title = element_text(hjust = 0.5),
          axis.title.x=element_blank(),
          axis.title.y=element_blank()) +
    ggtitle("JP")

  list(graph1, graph2)
}
Miguel Tuñón authored
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
GetContinentAttackEvolution <- function(Attacks){

  attack.evol <-mutate(Attacks, Year = format(Attacks$Date, "%Y")) %>%
              group_by(Attack.standar, Country, Year)
  attack.evol <- as.data.frame(table(attack.evol$Country, attack.evol$Attack.standar, attack.evol$Year))
  attack.evol <- setNames(attack.evol, c("Country", "Attack", "Year", "Count"))

  attack.evol <- attack.evol[attack.evol$Country != "",]
  attack.evol <- attack.evol[attack.evol$Attack != "",]

  attack.evol$Attack <- as.character(attack.evol$Attack)
  attack.evol[attack.evol$Attack != "DDoS" &
               attack.evol$Attack != "Defacement" &
               attack.evol$Attack != "Injection",]$Attack <- "Otros"
  attack.evol$Attack <- as.factor(attack.evol$Attack)

  attack.evol <- group_by(attack.evol, Attack, Country, Year)
  attack.evol <- summarise(attack.evol, Count = sum(Count))

  attack.evol.US <- attack.evol[attack.evol$Country == "US",]
  attack.evol.JP <- attack.evol[attack.evol$Country == "JP",]

  attack.evol.US.Otros <- attack.evol.US[attack.evol.US$Attack == "Otros",]
  attack.evol.JP.Otros <- attack.evol.JP[attack.evol.JP$Attack == "Otros",]


  graph1 <- ggplot2::qplot(main = "US",
                           x = attack.evol.US$Year,
                           y = attack.evol.US$Count,
                           group = Attack,
                           xlab = "Years",
                           ylab = "Certifications",
                           data = attack.evol.US,
                           geom = "point",
                           color = Attack)  +
            geom_line() +
            theme(plot.title = element_text(hjust = 0.5))

  graph2 <- ggplot2::qplot(main = "JP",
                           x = attack.evol.JP$Year,
                           y = attack.evol.JP$Count,
                           group = Attack,
                           xlab = "Years",
                           ylab = "Certifications",
                           data = attack.evol.JP,
                           geom = "point",
                           color = Attack)  +
            geom_line() +
            theme(plot.title = element_text(hjust = 0.5))

  graph3 <- ggplot2::qplot(main = "US",
                           x = attack.evol.US.Otros$Year,
                           y = attack.evol.US.Otros$Count,
                           group = 1,
                           xlab = "Years",
                           ylab = "Certifications",
                           data = attack.evol.US.Otros,
                           geom = "point")  +
            geom_line() +
            stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..)) +
            theme(plot.title = element_text(hjust = 0.5))

  graph4 <- ggplot2::qplot(main = "JP",
                           x = attack.evol.JP.Otros$Year,
                           y = attack.evol.JP.Otros$Count,
                           group = 1,
                           xlab = "Years",
                           ylab = "Certifications",
                           data = attack.evol.JP.Otros,
                           geom = "point")  +
            geom_line() +
            stat_smooth(method = "lm", se = FALSE, aes(outfit=fit<<-..y..)) +
            theme(plot.title = element_text(hjust = 0.5))

  list(graph1, graph2, graph3, graph4)

}
Miguel Tuñón authored
821
822
823
#----------------------------------------------------------------
#----------------------------------------------------------------
#----------------------------------------------------------------
Miguel Tuñón authored
824
825
Miguel Tuñón authored
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
GetBaseCertsGraph <- function(Cert_PerCountry, year){
  graph1 <- ggplot2::qplot(main = "Countries with above average number of companies certified with 27001 (2012)",
                           x = reorder(country_short,X2012),
                           y = X2012,
                           xlab = "Country",
                           ylab = "Number of certifications",
                           data = Cert_PerCountry[Cert_PerCountry$X2012 > mean(Cert_PerCountry$X2012),],
                           geom = "col",
                           fill = Continent)
  graph1
}
Miguel Tuñón authored
841
842
843
844
845
846
847
848
#' Return every graph used in the report file
#'
#' @param Cert_PerCountry data.frame with the processed data of ISO 27001 certifications
#' @param Attacks data.frame with the processed data of cyberattacks
#'
#' @return data.frame
#' @export
GetReportGraphs <- function(Cert_PerCountry,Attacks) {
Imanol-Mikel Barba Sabariego authored
849
  #2012
Miguel Tuñón authored
850
  graph1 <- ggplot2::qplot(main = "Countries with above average number of companies certified with 27001 (2012)",
Imanol-Mikel Barba Sabariego authored
851
852
                  x = reorder(country_short,X2012),
                  y = X2012,
Miguel Tuñón authored
853
854
                  xlab = "Country",
                  ylab = "Number of certifications",
Miguel Tuñón authored
855
856
857
858
                  data = Cert_PerCountry[Cert_PerCountry$X2012 > mean(Cert_PerCountry$X2012),],
                  geom = "col",
                  fill = Continent)
Imanol-Mikel Barba Sabariego authored
859
860
861
  attacks2k12 <- Attacks[Attacks$Date < "2013-01-01" & Attacks$Date >= "2012-01-01",]
  frameAttacks2k12 <- as.data.frame(table(attacks2k12$Country))
  colnames(frameAttacks2k12) <- c("Country","Attacks")
Miguel Tuñón authored
862
  graph2 <- ggplot2::qplot(main = "Countries with above average number of cyberattacks (2012)",
Imanol-Mikel Barba Sabariego authored
863
864
                  x = reorder(Country,Attacks),
                  y = Attacks,
Miguel Tuñón authored
865
866
                  xlab = "Country",
                  ylab = "Number of attacks",
Miguel Tuñón authored
867
868
869
                  data = frameAttacks2k12[frameAttacks2k12$Attacks > mean(frameAttacks2k12$Attacks),],
                  geom = "col",
                  fill = Continent)
Imanol-Mikel Barba Sabariego authored
870
871
872
873

  Attacks2012ByMonth <- mutate(attacks2k12, month = format(attacks2k12$Date, "%m")) %>% group_by(month)
  Attack2012FreqByMonth <- as.data.frame(table(Attacks2012ByMonth$month))
  colnames(Attack2012FreqByMonth) <- c("Month", "Attacks")
Miguel Tuñón authored
874
875
  graph3 <- ggplot2::qplot(x = as.numeric(Month),
                  y = Attacks,
Imanol-Mikel Barba Sabariego authored
876
877
878
879
                  main = "Global cyberattack progression by month (2012)",
                  data = Attack2012FreqByMonth,
                  geom = c("point", "smooth"),
                  xlim = c(1,12),
Miguel Tuñón authored
880
                  xlab = "Month") + ggplot2::scale_x_continuous(breaks = 1:12)
Imanol-Mikel Barba Sabariego authored
881
882

  #2013
Miguel Tuñón authored
883
  graph4 <- ggplot2::qplot(main = "Countries with above average number of companies certified with 27001 (2013)",
Imanol-Mikel Barba Sabariego authored
884
885
                  x = reorder(country_short,X2013),
                  y = X2013,
Miguel Tuñón authored
886
887
                  xlab = "Country",
                  ylab = "Number of certifications",
Miguel Tuñón authored
888
889
890
                  data = Cert_PerCountry[Cert_PerCountry$X2013 > mean(Cert_PerCountry$X2013),]
                  , geom = "col",
                  fill = Continent)
Imanol-Mikel Barba Sabariego authored
891
892
893
  attacks2k13 <- Attacks[Attacks$Date < "2014-01-01" & Attacks$Date >= "2013-01-01",]
  frameAttacks2k13 <- as.data.frame(table(attacks2k13$Country))
  colnames(frameAttacks2k13) <- c("Country","Attacks")
Miguel Tuñón authored
894
  graph5 <- ggplot2::qplot(main = "Countries with above average number of cyberattacks (2013)",
Imanol-Mikel Barba Sabariego authored
895
896
                  x = reorder(Country,Attacks),
                  y = Attacks,
Miguel Tuñón authored
897
898
                  xlab = "Country",
                  ylab = "Number of attacks",
Miguel Tuñón authored
899
900
901
                  data = frameAttacks2k13[frameAttacks2k13$Attacks > mean(frameAttacks2k13$Attacks),]
                  , geom = "col",
                  fill = Continent)
Imanol-Mikel Barba Sabariego authored
902
903
904
905

  Attacks2013ByMonth <- mutate(attacks2k13, month = format(attacks2k13$Date, "%m")) %>% group_by(month)
  Attack2013FreqByMonth <- as.data.frame(table(Attacks2013ByMonth$month))
  colnames(Attack2013FreqByMonth) <- c("Month", "Attacks")
Miguel Tuñón authored
906
907
  graph6 <- ggplot2::qplot(x = as.numeric(Month),
                  y = Attacks,
Imanol-Mikel Barba Sabariego authored
908
909
910
911
                  main = "Global cyberattack progression by month (2013)",
                  data = Attack2013FreqByMonth,
                  geom = c("point", "smooth"),
                  xlim = c(1,12),
Miguel Tuñón authored
912
                  xlab = "Month") + ggplot2::scale_x_continuous(breaks = 1:12)
Imanol-Mikel Barba Sabariego authored
913
914

  #2014
Miguel Tuñón authored
915
  graph7 <- ggplot2::qplot(main = "Countries with above average number of companies certified with 27001 (2014)",
Imanol-Mikel Barba Sabariego authored
916
917
                  x = reorder(country_short,X2014),
                  y = X2014,
Miguel Tuñón authored
918
919
                  xlab = "Country",
                  ylab = "Number of certifications",
Miguel Tuñón authored
920
921
922
                  data = Cert_PerCountry[Cert_PerCountry$X2014 > mean(Cert_PerCountry$X2014),]
                  , geom = "col",
                  fill = Continent)
Imanol-Mikel Barba Sabariego authored
923
924
925
  attacks2k14 <- Attacks[Attacks$Date < "2015-01-01" & Attacks$Date >= "2014-01-01",]
  frameAttacks2k14 <- as.data.frame(table(attacks2k14$Country))
  colnames(frameAttacks2k14) <- c("Country","Attacks")
Miguel Tuñón authored
926
  graph8 <- ggplot2::qplot(main = "Countries with above average number of cyberattacks (2014)",
Imanol-Mikel Barba Sabariego authored
927
928
                  x = reorder(Country,Attacks),
                  y = Attacks,
Miguel Tuñón authored
929
930
                  xlab = "Country",
                  ylab = "Number of attacks",
Miguel Tuñón authored
931
932
933
                  data = frameAttacks2k14[frameAttacks2k14$Attacks > mean(frameAttacks2k14$Attacks),]
                  , geom = "col",
                  fill = Continent)
Imanol-Mikel Barba Sabariego authored
934
935
936
937

  Attacks2014ByMonth <- mutate(attacks2k14, month = format(attacks2k14$Date, "%m")) %>% group_by(month)
  Attack2014FreqByMonth <- as.data.frame(table(Attacks2014ByMonth$month))
  colnames(Attack2014FreqByMonth) <- c("Month", "Attacks")
Miguel Tuñón authored
938
939
  graph9 <- ggplot2::qplot(x = as.numeric(Month),
                  y = Attacks,
Imanol-Mikel Barba Sabariego authored
940
941
942
943
                  main = "Global cyberattack progression by month (2014)",
                  data = Attack2014FreqByMonth,
                  geom = c("point", "smooth"),
                  xlim = c(1,12),
Miguel Tuñón authored
944
                  xlab = "Month") + ggplot2::scale_x_continuous(breaks = 1:12)
Imanol-Mikel Barba Sabariego authored
945
946

  #2015
Miguel Tuñón authored
947
  graph10 <- ggplot2::qplot(main = "Countries with above average number of companies certified with 27001 (2015)",
Imanol-Mikel Barba Sabariego authored
948
949
                  x = reorder(country_short,X2015),
                  y = X2015,
Miguel Tuñón authored
950
951
                  xlab = "Country",
                  ylab = "Number of certifications",
Miguel Tuñón authored
952
953
954
                  data = Cert_PerCountry[Cert_PerCountry$X2015 > mean(Cert_PerCountry$X2015),]
                  , geom = "col",
                  fill = Continent)
Imanol-Mikel Barba Sabariego authored
955
956
957
  attacks2k15 <- Attacks[Attacks$Date < "2016-01-01" & Attacks$Date >= "2015-01-01",]
  frameAttacks2k15 <- as.data.frame(table(attacks2k15$Country))
  colnames(frameAttacks2k15) <- c("Country","Attacks")
Miguel Tuñón authored
958
  graph11 <- ggplot2::qplot(main = "Countries with above average number of cyberattacks (2015)",
Imanol-Mikel Barba Sabariego authored
959
960
                  x = reorder(Country,Attacks),
                  y = Attacks,
Miguel Tuñón authored
961
962
                  xlab = "Country",
                  ylab = "Number of attacks",
Miguel Tuñón authored
963
964
965
                  data = frameAttacks2k15[frameAttacks2k15$Attacks > mean(frameAttacks2k15$Attacks),]
                  , geom = "col",
                  fill = Continent)
Imanol-Mikel Barba Sabariego authored
966
967
968
969

  Attacks2015ByMonth <- mutate(attacks2k15, month = format(attacks2k15$Date, "%m")) %>% group_by(month)
  Attack2015FreqByMonth <- as.data.frame(table(Attacks2015ByMonth$month))
  colnames(Attack2015FreqByMonth) <- c("Month", "Attacks")
Miguel Tuñón authored
970
  graph12 <- ggplot2::qplot(x = as.numeric(Month),
Imanol-Mikel Barba Sabariego authored
971
972
973
974
975
                  y = Attacks,
                  main = "Global cyberattack progression by month (2015)",
                  data = Attack2015FreqByMonth,
                  geom = c("point", "smooth"),
                  xlim = c(1,12),
Miguel Tuñón authored
976
                  xlab = "Month") + ggplot2::scale_x_continuous(breaks = 1:12)
Imanol-Mikel Barba Sabariego authored
977
978
979
980
981
982




  list(graph1,graph2,graph3,graph4,graph5,graph6,graph7,graph8,graph9,graph10,graph11,graph12)
}