|
1
2
3
4
5
|
#----------------------------------------------------------------
#-------------------------General evolution----------------------
#----------------------------------------------------------------
|
|
6
|
|
|
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
|
|
33
34
35
|
attacks.evol <- as.data.frame(table(attacks.evol$Year))
colnames(attacks.evol) <- c("Year","Attacks")
|
|
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)
|
|
68
69
70
|
}
|
|
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",
|
|
107
|
x = Certs.evol$Year,
|
|
108
|
y = Certs.evol$Count,
|
|
109
110
111
112
113
114
|
group = 1,
xlab = "Years",
ylab = "Certifications",
data = Certs.evol,
geom = "line") +
geom_point() +
|
|
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)
|
|
134
135
136
|
}
|
|
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
|
|
143
144
|
GetAttacksMonthEvolution <- function(Attacks){
|
|
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
|
|
149
150
151
|
attacks.evol <- as.data.frame(table(attacks.evol$Year))
colnames(attacks.evol) <- c("Year","Attacks")
|
|
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") +
|
|
161
162
163
|
geom_point() +
theme(plot.title = element_text(hjust = 0.5)) +
geom_smooth(method = 'loess') +
|
|
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", "", "", "", "", ""))
|
|
169
170
171
172
|
}
|
|
173
174
175
|
#----------------------------------------------------------------
#-------------------------Attack type evolution------------------
#----------------------------------------------------------------
|
|
176
|
|
|
177
|
|
|
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
|
|
191
|
attack.pie <- group_by(Attacks, Attack.standar)
|
|
192
|
#Counting rows for each attack type
|
|
193
194
195
|
attack.pie <- as.data.frame(table(attack.pie$Attack.standar))
attack.pie <- setNames(attack.pie, c("Attack", "Count"))
|
|
196
|
#Removing rows without attack type defined
|
|
197
|
attack.pie <- attack.pie[attack.pie$Attack != "",]
|
|
198
|
#Removing attacks with less than 1% of representation from total
|
|
199
|
attack.pie <- attack.pie[attack.pie$Count > (sum(attack.pie$Count) * 0.01),]
|
|
200
201
|
#Calc % of each attack type
attack.pie <- mutate(attack.pie, Perc=paste(round(100 * attack.pie$Count / sum(attack.pie$Count), 2), "%"))
|
|
202
203
|
|
|
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))
|
|
223
224
225
|
}
|
|
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
|
|
238
239
|
Attacks.pre <- as.data.frame(table(Attacks.pre$Year, Attacks.pre$Attack.standar))
Attacks.pre <- setNames(Attacks.pre, c("Year", "Attack", "Count"))
|
|
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)
|
|
256
257
258
259
|
}
|
|
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)
|
|
297
298
299
|
}
|
|
300
301
302
303
|
#----------------------------------------------------------------
#-------------------------Geolocal evolution---------
#----------------------------------------------------------------
|
|
304
305
306
307
308
309
310
311
312
313
314
|
#' Return pie graph to show % of attacks and certifications for each continent
#'
#' @param Attacks data.frame with procesed source data of attacks
#' @param Attacks.label.vjust Vector of vertical just to each portion label
#' @param Attacks.label.hjust Vector of horizontal just to each portion label
#' @param Cert_PerCountry data.frame with procesed source data of certifications
#' @param Certs.label.vjust Vector of vertical just to each portion label
#' @param Certs.label.hjust Vector of horizontal just to each portion label
#'
#' @return list(AttacksGraph, CertsGraph, ContinentListToStudy)
#' @export
|
|
315
316
317
318
319
320
|
GetContinentPie <- function (Attacks,
Attacks.label.vjust = 0,
Attacks.label.hjust = 0,
Cert_PerCountry,
Certs.label.vjust = 0,
Certs.label.hjust = 0){
|
|
321
|
|
|
322
|
#Group attacks by continent
|
|
323
|
attack.pie <- group_by(Attacks, Continent)
|
|
324
|
#Count attacks for each continent
|
|
325
326
|
attack.pie <- as.data.frame(table(attack.pie$Continent))
attack.pie <- setNames(attack.pie, c("Continent", "Count"))
|
|
327
|
#Remove rows without Continent specified
|
|
328
|
attack.pie <- attack.pie[attack.pie$Continent != "",]
|
|
329
330
|
#Calc % of each continent
attack.pie <- mutate(attack.pie, perc = round(100 * attack.pie$Count / sum(attack.pie$Count), 2))
|
|
331
|
|
|
332
|
#Attacks graph
|
|
333
334
335
336
|
graph1 <- ggplot(data=attack.pie,
aes(x=factor(1),
y=Count,
fill=Continent)) +
|
|
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
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
|
|
360
361
362
363
|
graph2 <- ggplot(data=cert.pie,
aes(x=factor(1),
y=Count,
fill=Continent)) +
|
|
364
365
366
367
368
369
370
371
372
373
374
375
|
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")
|
|
376
377
378
379
|
#Return graphs and union of continent to study
list(graph1, graph2,
union(unique(attack.pie[attack.pie$perc > 5,]$Continent),
unique(cert.pie[cert.pie$perc > 5,]$Continent)))
|
|
380
381
382
|
}
|
|
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
#' Return graph to show the evolution of a attack types list
#'
#' @param Attacks data.frame with procesed source data
#' @param ContinentList List with continents to show
#'
#' @return list(graph, data.frame with ContinentList attacks data by year)
#' @export
GetContinentAttacksEvolution <- function(Attacks,
ContinentList){
#Extract year from date
attacks.evol <- mutate(Attacks, Year = format(Attacks$Date, "%Y")) %>%
group_by(Continent, Year) #Grouping atacks by continent and year
#Counting attacks for each continent and year
|
|
397
398
|
attacks.evol <- as.data.frame(table(attacks.evol$Continent, attacks.evol$Year))
colnames(attacks.evol) <- c("Continent", "Year","Attacks")
|
|
399
400
|
#Filtering by the ContinentList especified
attacks.evol <- attacks.evol[attacks.evol$Continent %in% ContinentList,]
|
|
401
|
|
|
402
|
#Graph
|
|
403
404
405
406
407
408
409
410
411
|
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) +
|
|
412
413
|
geom_line() +
theme(plot.title = element_text(hjust = 0.5))
|
|
414
|
|
|
415
416
|
#Return graph and data to represent 1b1
list(graph1, attacks.evol)
|
|
417
418
419
|
}
|
|
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
#' Return graph to show the evolution of certifications in a continent list
#'
#' @param Cert_PerCountry data.frame with procesed source data
#' @param ContinentList List with continents to show
#'
#' @return list(graph, data.frame with ContinentList certifications data by year)
#' @export
GetContinentCertsEvolution <- function(Cert_PerCountry,
ContinentList){
#Collapsing year columns to 1 column with year value
certs.evol <- gather(Cert_PerCountry, "Year", "Certs", 2:6) %>%
group_by(Continent, Year) #Grouping by continent and year
#Sum of certifications for echa continent and year
certs.evol <- summarise(certs.evol, Certs = sum(Certs))
#Removing the X from year values
|
|
435
|
certs.evol$Year <- substr(certs.evol$Year,2,5)
|
|
436
437
|
#Filtering for the specified continents
certs.evol <- certs.evol[certs.evol$Continent %in% ContinentList,]
|
|
438
|
|
|
439
|
#graph
|
|
440
441
442
443
444
445
446
447
448
|
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) +
|
|
449
450
|
geom_line() +
theme(plot.title = element_text(hjust = 0.5))
|
|
451
|
|
|
452
453
|
#Return the graph and data to represent continents 1b1
list(graph1, certs.evol)
|
|
454
455
|
}
|
|
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
#' Return graph to show the evolution of attacks in a single continent and his smooth
#'
#' @param Attacks data.frame with procesed source data
#' @param Continent Continent 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
GetContinentAttacksSigleEvolution <- function(Attacks,
Continent,
smooth.x = 0,
smooth.y = 0,
smooth.label.hjust = 0){
|
|
471
|
|
|
472
473
|
#Filtering for the continent specified
attacks.evol <- Attacks[Attacks$Continent == Continent,]
|
|
474
|
|
|
475
476
477
|
#Calc slope
slope1 <- lm(formula = Attacks ~ as.numeric(Year), data = attacks.evol)$coef[[2]]
#Graph
|
|
478
|
graph1 <- ggplot(data = attacks.evol,
|
|
479
480
481
482
483
484
485
486
487
488
489
490
|
aes(x = Year, y = Attacks, group = 1)) +
geom_line() +
geom_point() +
theme(plot.title = element_text(hjust = 0.5)) +
ggtitle(Continent) +
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)
|
|
491
|
|
|
492
493
|
#Returning list(graph, slope)
list(graph1, slope1)
|
|
494
|
}
|
|
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
#' Return graph to show the evolution of certifications in a single continent and his smooth
#'
#' @param Certs_byContinent data.frame with procesed source data
#' @param Continent Continent 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
GetContinentCertsSingleEvolution <- function(Certs_byContinent,
Continent,
smooth.x = 0,
smooth.y = 0,
smooth.label.hjust = 0){
|
|
510
|
|
|
511
512
|
#Filtering for the continent specified
certs.evol <- Certs_byContinent[Certs_byContinent$Continent == Continent,]
|
|
513
|
|
|
514
515
516
|
#Calc slope
slope1 <- lm(formula = Certs ~ as.numeric(Year), data = certs.evol)$coef[[2]]
#Graph
|
|
517
|
graph1 <- ggplot(data = certs.evol,
|
|
518
519
520
521
522
523
524
525
526
527
528
529
|
aes(x = Year, y = Certs, group = 1)) +
geom_line() +
geom_point() +
theme(plot.title = element_text(hjust = 0.5)) +
ggtitle(Continent) +
xlab("Years") + ylab("Certifications")+
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)
|
|
530
|
|
|
531
532
|
#Returning list(graph, slope)
list(graph1, slope1)
|
|
533
534
|
}
|
|
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
#' Return graph to show the attacks by country
#'
#' @param Attacks data.frame with procesed source data of attacks
#' @param Attacks.label.vjust Vector of vertical just to each portion label
#' @param Attacks.label.hjust Vector of horizontal just to each portion label
#' @param Cert_PerCountry data.frame with procesed source certifications
#' @param Certs.label.vjust Vector of vertical just to each portion label
#' @param Certs.label.hjust Vector of horizontal just to each portion label
#'
#' @return list(AttacksGraph, CertsGraph, CountryListToStudy)
GetCountriesCol <- function(Attacks,
Attacks.label.vjust = 0,
Attacks.label.hjust = 0,
Cert_PerCountry,
Certs.label.vjust = 0,
Certs.label.hjust = 0){
#Group attacks by continent and country
attacks.col <- group_by(Attacks, Continent, Country)
#Count attacks for each country
attacks.col <- as.data.frame(table(attacks.col$Continent, attacks.col$Country))
attacks.col <- setNames(attacks.col, c("Continent", "Country", "Count"))
#Remove rows without Continent/Country specified
attacks.col <- attacks.col[attacks.col$Country != "",]
attacks.col <- attacks.col[attacks.col$Country != "AU",] #Abnormal behaivour, corrupt??
attacks.col <- attacks.col[attacks.col$Continent != "",]
#Only the countries with more than 2% of total attacks
attacks.col <- attacks.col[attacks.col$Count > (sum(attacks.col$Count) * 0.015), ]
#Sort by attacks
attacks.col <- arrange(attacks.col, desc(Count))
|
|
565
|
|
|
566
567
568
569
570
571
572
573
574
575
576
577
|
#Attacks graph
graph1 <- ggplot2::ggplot(aes(x = reorder(Country, Count),
y = Count),
data = attacks.col) +
geom_col(aes(fill = Continent)) +
geom_text(aes(label = attacks.col$Count),
vjust = Attacks.label.vjust,
hjust = Attacks.label.hjust,
size = 3) +
theme(plot.title = element_text(hjust = 0.5)) +
ggtitle("Attacks") +
xlab("Country") + ylab("Attacks")
|
|
578
|
|
|
579
580
581
582
583
584
585
586
587
588
589
|
#Grouping certifications by continent
certs.col <- group_by(Cert_PerCountry, Continent, country_short)
#Counting certificates for each continent
certs.col <- dplyr::summarise(certs.col, Count = sum(X2011 + X2012 + X2013 + X2014 + X2015))
#Remove rows without Continent specified
certs.col <- certs.col[certs.col$Continent != "",]
certs.col <- certs.col[certs.col$country_short != "",]
#Only the countries with more than 2% of total certs
certs.col <- certs.col[certs.col$Count > (sum(certs.col$Count) * 0.02), ]
#Sort by certifications
certs.col <- arrange(certs.col, desc(Count))
|
|
590
591
|
|
|
592
593
|
#Certifications graph
graph2 <- ggplot2::ggplot(aes(x = reorder(country_short, Count),
|
|
594
|
y = Count),
|
|
595
596
597
598
599
600
601
602
603
|
data = certs.col) +
geom_col(aes(fill = Continent)) +
geom_text(aes(label = certs.col$Count),
vjust = Certs.label.vjust,
hjust = Certs.label.hjust,
size = 3) +
theme(plot.title = element_text(hjust = 0.5)) +
ggtitle("ISO 27001") +
xlab("Country") + ylab("Certifications")
|
|
604
|
|
|
605
606
607
608
|
#Return graphs and union of countries to study
list(graph1, graph2,
union(unique(head(attacks.col$Country, 3)),
unique(head(certs.col$country_short, 3))))
|
|
609
610
|
}
|
|
611
|
|
|
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
|
#' Return graph to show the evolution of attacks in a single continent and his smooth
#'
#' @param Attacks data.frame with procesed source data
#' @param Country Country 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
GetCountriesAttacksSingleEvolution <- function(Attacks,
Country,
smooth.x = 0,
smooth.y = 0,
smooth.label.hjust = 0){
#Extract year from date
attacks.evol <- mutate(Attacks, Year = format(Attacks$Date, "%Y")) %>%
group_by(Year, Country) #Group by country
#Count attacks for each year and country
|
|
631
632
|
attacks.evol <- as.data.frame(table(attacks.evol$Year, attacks.evol$Country))
attacks.evol <- setNames(attacks.evol, c("Year", "Country", "Count"))
|
|
633
634
|
#Filter by the specified country
attacks.evol <- attacks.evol[attacks.evol$Country == Country,]
|
|
635
|
|
|
636
637
638
|
#Calc slope
slope1 <- lm(formula = Count ~ as.numeric(Year), data = attacks.evol)$coef[[2]]
#Graph
|
|
639
|
graph1 <- ggplot(data = attacks.evol,
|
|
640
641
642
643
644
645
646
647
648
649
650
651
|
aes(x = Year, y = Count, group = 1)) +
geom_line() +
geom_point() +
theme(plot.title = element_text(hjust = 0.5)) +
ggtitle(Country) +
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)
|
|
652
|
|
|
653
654
|
#Return list(graph, slope)
list(graph1, slope1)
|
|
655
656
657
|
}
|
|
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
|
#' Return graph to show the evolution of certifications in a single continent and his smooth
#'
#' @param Attacks data.frame with procesed source data
#' @param Country Country 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
GetCountriesCertsSingleEvolution <- function(Cert_PerCountry,
Country,
smooth.x = 0,
smooth.y = 0,
smooth.label.hjust = 0){
#Collapsing year columns to only one with the year value
certs.evol <- gather(Cert_PerCountry, "Year", "Certs", 2:6) %>%
group_by(country_short, Year) #Group by country and year
#sum certificates for each country and year
|
|
678
679
|
certs.evol <- summarise(certs.evol,
Certs = sum(Certs))
|
|
680
|
#Removing the X of the year values
|
|
681
|
certs.evol$Year <- substr(certs.evol$Year,2,5)
|
|
682
683
|
#Filter by the specified Country
certs.evol <- certs.evol[certs.evol$country_short == Country,]
|
|
684
|
|
|
685
686
687
|
#Calc slope
slope1 <- lm(formula = Certs ~ as.numeric(Year), data = certs.evol)$coef[[2]]
#Graph
|
|
688
|
graph1 <- ggplot(data = certs.evol,
|
|
689
|
aes(x = Year, y = Certs, group = 1)) +
|
|
690
691
692
|
geom_line() +
geom_point() +
theme(plot.title = element_text(hjust = 0.5)) +
|
|
693
|
ggtitle(Country) +
|
|
694
|
xlab("Years") + ylab("Certifications")+
|
|
695
696
697
698
699
700
701
702
703
|
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)
#Return graph and slope
list(graph1, slope1)
|
|
704
705
706
707
708
709
710
711
|
}
#----------------------------------------------------------------
#-------------------------gelocal/type---------------------------
#----------------------------------------------------------------
|
|
712
713
714
715
716
717
718
719
720
721
722
|
#' Return graph representing % of attacks type by continent
#'
#' @param Attacks data.frame with procesed source data
#' @param Country Country to represent on the left side
#' @param Country2 Country to represent on the right side
#'
#' @return Graph
#' @export
GetContinentAttackCol <- function (Attacks,
Country,
Country2){
|
|
723
|
|
|
724
|
#Grouping attacks by attack type and country
|
|
725
|
attack.pie <- group_by(Attacks, Attack.standar, Country)
|
|
726
|
#Counting attacks for each attack type and country
|
|
727
728
|
attack.pie <- as.data.frame(table(attack.pie$Country, attack.pie$Attack.standar))
attack.pie <- setNames(attack.pie, c("Country", "Attack", "Count"))
|
|
729
|
#Remove rows withouth a country specified
|
|
730
|
attack.pie <- attack.pie[attack.pie$Country != "",]
|
|
731
|
#Remove rows withouth attack type specified
|
|
732
|
attack.pie <- attack.pie[attack.pie$Attack != "",]
|
|
733
|
#grouping non 'important' attack types in 'Otros'
|
|
734
735
736
737
738
|
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)
|
|
739
|
#Grouping atatcks by attacky type and country
|
|
740
|
attack.pie <- group_by(attack.pie, Attack, Country)
|
|
741
|
#sum attacks for each attack type and country
|
|
742
743
|
attack.pie <- summarise(attack.pie, Count = sum(Count))
|
|
744
745
746
|
#Filtering by the desired countries
attack.pie.C1 <- attack.pie[attack.pie$Country == Country,]
attack.pie.C2 <- attack.pie[attack.pie$Country == Country2,]
|
|
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
|
#Calc % of each attack type
attack.pie.C1$perc <- round(100 * attack.pie.C1$Count / sum(attack.pie.C1$Count), 2)
attack.pie.C2$perc <- round(100 * attack.pie.C2$Count / sum(attack.pie.C2$Count), 2)
#Calc max % to draw gray background
attack.pie.max <- data.frame(Attack = attack.pie.C1$Attack)
percs <- c()
for (AT in attack.pie.max$Attack){
percs <- c(percs, max(attack.pie.C1[attack.pie.C1$Attack == AT,]$perc,
attack.pie.C2[attack.pie.C2$Attack == AT,]$perc))
}
attack.pie.max$perc <- percs
graph1 <- ggplot() +
geom_col(aes(x=Attack,
y=perc),
width = 1,
data = attack.pie.max,
color = "Black",
fill = "Grey") +
geom_col(aes(x=1.25:4.25,
y=perc,
fill = Country),
width = 0.4,
data = attack.pie.C1) +
geom_text(aes(x=1.25:4.25,
y=perc + 1,
label = paste(perc, "%")),
data = attack.pie.C1) +
geom_col(aes(x=0.75:3.75,
y=perc,
fill = Country2),
width = 0.4,
data = attack.pie.C2) +
geom_text(aes(x=0.75:3.75,
y=perc + 1,
label = paste(perc, "%")),
data = attack.pie.C2) +
scale_x_discrete("Attack", attack.pie.C1$Attack) +
ylab("% of total attacks") +
theme(plot.title = element_text(hjust = 0.5)) +
ggtitle(paste(Country, Country2, sep = " - "))
|
|
790
|
|
|
791
|
graph1
|
|
792
793
|
}
|
|
794
|
|
|
795
796
797
798
799
800
801
802
803
|
#' Return graph representing the evolution of attack types in a country
#'
#' @param Attacks data.frame with procesed source data
#' @param Country Country to represent
#'
#' @return list(Graph, data to represent attacktypes of the country 1b1)
GetContinentAttackEvolution <- function(Attacks, Country){
#Extract Year from date
|
|
804
|
attack.evol <-mutate(Attacks, Year = format(Attacks$Date, "%Y")) %>%
|
|
805
806
|
group_by(Attack.standar, Country, Year) #Group by attack type, country and year
#Counting the attacks for each attack type, country and year
|
|
807
808
|
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"))
|
|
809
|
#Removing rows without country or attack type
|
|
810
811
|
attack.evol <- attack.evol[attack.evol$Country != "",]
attack.evol <- attack.evol[attack.evol$Attack != "",]
|
|
812
|
#grouping non 'important' attack types in 'Otros'
|
|
813
814
815
816
817
|
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)
|
|
818
|
#Counting attacks of new tyoe
|
|
819
820
|
attack.evol <- group_by(attack.evol, Attack, Country, Year)
attack.evol <- summarise(attack.evol, Count = sum(Count))
|
|
821
822
|
#Filtering by specified country
attack.evol <- attack.evol[attack.evol$Country == Country,]
|
|
823
|
|
|
824
825
826
827
828
|
#Graph
graph1 <- ggplot2::qplot(main = Country,
x = attack.evol$Year,
y = attack.evol$Count,
group = attack.evol$Attack,
|
|
829
|
xlab = "Years",
|
|
830
831
|
ylab = "Attacks",
data = attack.evol,
|
|
832
833
834
835
836
|
geom = "point",
color = Attack) +
geom_line() +
theme(plot.title = element_text(hjust = 0.5))
|
|
837
838
|
#list with graph and processed data to represent 1b1
list(graph1, attack.evol)
|
|
839
840
841
|
}
|
|
842
|
|