-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotByCountry.r
57 lines (45 loc) · 2.11 KB
/
plotByCountry.r
1
2
3
4
5
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
library(ggplot2)
source('helpers.r')
countryDat <- read.table('result/countryDat.tsv', sep='\t', header=TRUE)
countryDat <- within(countryDat, country_txt <- reorder(country_txt, -totalEvents))
countryAnnualDat <- read.table('result/countryAnnualDat.tsv', sep='\t', header=TRUE)
regions <- 'South Asia' #c('Middle East & North Africa')
p <- ggplot(subset(countryDat, region_txt==regions),
aes(x=country_txt, y=totalEvents))
p <- p + geom_bar(stat="identity") +coord_flip()
myggsave('figure/region_SouthAsiaTotalAttackes.svg')
pEventKilled <- ggplot(countryDat,
aes(x=totalEvents, y=totalKilled, color=region_txt))
pEventKilled + geom_point() +
scale_x_log10() + scale_y_log10()
myggsave('figure/region_numAttackesVsNumKilled.svg')
highLight <- function(x, labels){
newx <- c()
highlighted <- x %in% labels
newx[highlighted] <- as.character(x)[highlighted]
newx[!highlighted] <- 'Others'
newx <- factor(newx, levels=c(labels, 'Others'))
return(newx)
}
iRegions <- c('Central Asia', 'South Asia', 'South America',
'Middle East & North Africa')
pEventTrend <- ggplot(countryAnnualDat,
aes(x=iyear, y=totalEvents, size=totalKilled,
color=highLight(region_txt, iRegions)))
pEventTrend + geom_point(alpha=0.3) + scale_size_area(max_size=10) +
facet_wrap(~ region_txt) +
geom_smooth(method='loess')
myggsave('figure/region_attacksVsKilledRegional.svg')
pEventTrend2 <- ggplot(subset(countryAnnualDat, region_txt=='Middle East & North Africa'),
aes(x=iyear, y=totalEvents, size=totalKilled,
color=country_txt))
pEventTrend2 + geom_point(alpha=0.3) + scale_size_area(max_size=10) +
geom_smooth(method='loess')
myggsave('figure/region_attacksVsKilledMiddleEast.svg')
# p
# dev.print(pdf, # copies the plot to a the PDF file
# "figure/testFigure_method2.pdf")
#
# p2 <- ggplot(subset(countryDat, region_txt==regions), aes(x=1, y=totalKilled, fill = country_txt))
# p2 + geom_bar(stat='identity') +coord_polar(theta='y')
#p + #geom_violin(alpha=0.3) + coord_flip()