-
Notifications
You must be signed in to change notification settings - Fork 1
/
workshop.R
126 lines (76 loc) · 3.35 KB
/
workshop.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
58
59
60
61
62
63
64
65
66
67
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
library(dplyr)
library(stringr)
library(ggplot2)
library(CSO)
# if there are any problems so far, make sure these are installed using install.packages()
# for the first three, and devtools::install_github("brendanjodowd/CSO") for the fourth.
############## PART 1 - import, filter, plot ##############
unemp <- get_cso("MUM01")
unemp1 <- unemp %>%
filter(Age.Group == "15 - 74 years") %>%
filter(Sex=="Both sexes") %>%
filter(str_detect(Statistic , "Rate"))
ggplot(unemp1 , aes(Month , value)) + geom_point() + geom_line()
unemp2 <- unemp %>%
filter(Age.Group == "15 - 74 years") %>%
filter(!Sex=="Both sexes") %>%
filter(str_detect(Statistic , "Rate"))
ggplot(unemp2 , aes(Month , value , colour=Sex)) +
geom_point() + geom_line() +
labs(x="" , y = "Unemployment rate (%)") +
ylim(0,20) +
xlim( as.Date("2005-01-01") , as.Date("2012-01-01") ) + theme_minimal()
############## PART 2 - joining datasets ##############
travel <- get_cso("TMA08") %>%
filter(Statistic=="Overseas Trips by Irish Residents (Thousand)") %>%
filter(Reason.for.Journey=="All reasons for journey")
ggplot(travel , aes(Year , value )) + geom_line()
population <- get_cso("PEA01") %>%
filter(Age.Group=="All ages" ) %>%
filter(Sex == "Both sexes")
ggplot(population , aes(Year , value )) + geom_line()
trips_and_pop <- bind_rows(travel , population) %>%
select(Year, Statistic, value)
ggplot(trips_and_pop , aes(Year, value, colour=Statistic)) + geom_line() +xlim(2009,2020)
travel2 <- travel %>%
select(value, Year) %>%
rename(trips = value)
population2 <- population %>%
select(value, Year) %>%
rename(pop = value)
trips_and_pop <- left_join(travel2 , population2 , by="Year") %>%
mutate(trips_per_person = trips/pop)
############## PART 3 - columns ##############
crime <- get_cso("CJA07") %>%
filter(str_detect(Garda.Station , "D.M.R. Western")) %>%
filter(Type.of.Offence=="Burglary and related offences") %>%
mutate(Garda.Station = word(Garda.Station , 1 , sep=",")) %>%
filter(Year %in% c(2012,2014,2016))
ggplot(crime , aes(Year , value, colour=Garda.Station)) + geom_line()
ggplot(crime , aes(Year , value , fill=Garda.Station)) + geom_col( position="dodge") + coord_flip()
ggplot(crime , aes(Garda.Station , value, fill=factor(Year))) + geom_col(position="dodge") + coord_flip()
dublin_averages <- crime %>%
group_by(Garda.Station) %>%
summarise(Avg.Burglaries = mean(value))
dublin_averages <- crime %>%
group_by(Garda.Station) %>%
mutate(Avg.Burglaries = mean(value))
dublin_averages <- dublin_averages %>%
mutate(Status = case_when(
value > Avg.Burglaries ~ "Above",
value < Avg.Burglaries ~ "Below",
value == Avg.Burglaries ~ "Same"
)
)
############## PART 4 - chloropleth ##############
rent <- get_cso("E1021") %>%
filter(str_detect(Statistic, "Average")) %>%
filter(Census.Year=="2016") %>%
filter(Nature.of.Occupancy=="Rented")
rent_map_data <- right_join(rent , admin_counties, by="County.and.City")
ggplot(rent_map_data, aes(long, lat, group=group, fill=value)) +
geom_polygon() + coord_quickmap()
ggplot(rent_map_data, aes(long, lat, group=group, fill=value)) +
geom_polygon(colour="white") + coord_quickmap() +
scale_fill_gradient(low="blue", high="red") + theme_void() +
labs(fill="Euro per week", title ="Average weekly rent")