-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.r
315 lines (283 loc) · 9.55 KB
/
script.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#install.packages("magrittr")
#install.packages("DescTools")
#install.packages("ggplot2")
#install.packages("countrycode")
#install.packages("gridExtra")
library(magrittr)
library(DescTools)
library(ggplot2)
library(countrycode)
library(gridExtra)
ds<-read.csv("datasets/BigmacPrice.csv")
head(ds, n=10)
class(ds$date)
ds$date<-as.Date(ds$date)
class(ds$date)
class(ds$currency_code)
class(ds$name)
class(ds$local_price)
class(ds$dollar_ex)
ds$dollar_ex
class(ds$dollar_price)
nrow(ds)
#RECORDS BY YEAR
recordsByYear<-Year(ds$date) %>%
table() %>%
data.frame()
recordsByYear
ggplot(recordsByYear, aes(x=., y=Freq)) +
geom_bar(stat="identity", width=0.6, fill="indianred1") +
theme_minimal() +
labs(title="Number of records by year", x="Year", y="Frequetion")
ggsave("results/recordsByYear.png",
width=600,
height=400,
bg="white",
dpi=72,
units="px")
#BIGMAC PRICE AROUND THE WORLD ALL IN ONE POINTS
ds[, c("date", "dollar_price")]
ggplot(ds, aes(x=Year(date), y=dollar_price)) +
geom_point() +
geom_smooth() +
theme_light() +
labs(title="Bigmac price around the world", x="Year", y="Price (dollar)") +
theme(text=element_text(size = 16))
ggsave("results/bigmacPriceAroundTheWorld.png",
width=600,
height=500,
dpi=72,
units="px")
#BIGMAC PRICE AROUND THE WORLD BY YEAR
tapply(ds$dollar_price, Year(ds$date), FUN=summary, na.rm=T)
ggplot(ds, aes(x=dollar_price, y=factor(Year(date)), fill=factor(Year(date)))) +
geom_boxplot(outlier.shape=NA) +
theme_light() +
theme(text=element_text(size=15), legend.position="none") +
labs(title="Boxplot Bigmac price around the world by year", x="", y="") +
xlim(0,7)
ggsave("results/boxplotPriceAroundTheWorldByYear.png",
width=1000,
height=700,
dpi=72,
units="px")
#BIGMAC PRICE AROUND THE WORLD
with(ds, {
ds$group<-"World";
summary(ds$dollar_price)
ggplot(ds, aes(x=group, y=dollar_price, fill=group)) +
geom_boxplot(outlier.colour="indianred1") +
theme_light() +
theme(text=element_text(size=18), legend.position="none") +
labs(title="Boxplot Bigmac price around the world", y="Price (dollar)", x="")
ggsave("results/boxplotPriceAroundTheWorld.png",
width=500,
height=400,
dpi=72,
units="px")
})
unique(ds$name)
USAData<-ds[ds$name=="United States", c("date", "name","dollar_price")]
USAData
#BIGMAC PRICE IN USA BOXPLOT
summary(USAData$dollar_price)
ggplot(USAData, aes(x=dollar_price, y=name, fill=name)) +
theme_light() +
geom_boxplot() +
theme(legend.position="none") +
labs(title="Bigmac price in USA", x="Price (dollar)", y="Country")
ggsave("results/boxplotPricesInUSA.png",
width=600,
height=400,
units="px",
dpi=72)
#BIGMAC PRICE IN USA POINTS
USAData[, c("date", "dollar_price")]
ggplot(USAData, aes(x=Year(date), y=dollar_price)) +
geom_point() +
theme_light() +
labs(title="Bigmac price in USA", x="Year", y="Price (dollar)") +
geom_smooth()
ggsave("results/bigmacPriceInUSA.png",
width=600,
height=400,
units="px",
dpi=72)
polandAndNeigh<-ds$name=="Poland" | ds$name=="Russia" | ds$name=="Germany" | ds$name=="Ukraine" | ds$name=="Lithuania" | ds$name=="Czech Republic" | ds$name=="Belarus" | ds$name=="Slovakia"
polandAndNeighDS<-ds[polandAndNeigh, c("date", "name", "dollar_price")]
names(polandAndNeighDS)<-c("Date", "Country", "dollarPrice")
polandAndNeighDS
#BIGMAC PRICES IN POLAND AND ITS NEIGHBORS
tapply(polandAndNeighDS$dollarPrice, polandAndNeighDS$Country, FUN=summary)
ggplot(polandAndNeighDS, aes(x=Country, y=dollarPrice, fill=Country)) +
geom_boxplot() +
labs(title="Bigmac price in Poland and its neighbors", x="Country", y="Price (dollar)") +
theme_light() +
theme(legend.position="left")
ggsave("results/pricesInPolandAndItsNeighbors.png",
width=800,
height=500,
bg="white",
dpi=72,
units="px")
#BIGMAC PRICE IN EASTERN EUROPE BY YEAR
polandAndNeighDS
ggplot(polandAndNeighDS, aes(x=Year(Date),y=dollarPrice, shape=Country, color=Country)) +
geom_point(size=2.2) +
scale_shape_manual(values=c(4, 8, 15, 16, 17, 18, 25)) +
scale_color_manual(values=c("red", "green", "gray", "purple", "steelblue", "orange", "violet")) +
labs(title="Bigmac price in eastern Europe by year", x="Year", y="Price (dollar)") +
theme_light() +
theme(text=element_text(size=16))
ggsave("results/bigmacPriceInEasternEurope.png",
width=600,
height=400,
dpi=72,
units="px")
#HEATMAP OF BIGMAC PRICES IN RECENT YEARS
completeData<-polandAndNeighDS[Year(polandAndNeighDS$Date)>=2018,]
completeData
ggplot(completeData, aes(x=Country, y=Year(Date), fill=dollarPrice)) +
scale_fill_gradient(low = "green", high = "darkgreen") +
geom_tile() +
labs(title="Heatmap of Bigmac prices in recent years", y="Year") +
theme_light() +
theme(legend.position="none")
ggsave("results/heatmapPricesInRecentYears.png",
width=600,
height=600,
units="px",
dpi=72)
colnames(ds)
prices2021<-ds[Year(ds$date)==2021, c("name", "dollar_price")]
nrow(prices2021)
table(prices2021$name)
#BIGMAC PRICE IN 2021
summary(prices2021)
ggplot(prices2021, aes(x=dollar_price)) +
geom_boxplot(fill="violet") +
theme_light() +
xlim(0, 7) +
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
text=element_text(size=16)) +
labs(title="Bigmac price in 2021", x="Price (dollar)")
ggsave("results/boxplotBigmacPriceIn2021.png",
width=600,
height=400,
dpi=72,
units="px")
?countrycode
ds$continent<-countrycode(ds$name, origin = "country.name", destination="continent")
ds[is.na(ds$continent),]
is.na(ds$continent) %>%
sum()
dsByContinent<-ds[!is.na(ds$continent), c("date", "name", "dollar_price", "continent")]
head(dsByContinent, n=10)
unique(dsByContinent$continent) %>%
length()
continental<-table(dsByContinent$continent) %>%
data.frame()
colnames(continental)<-c("Continent", "Records")
#NUMBER OF RECORDS BY CONTINENTS
head(continental)
firstplot<-ggplot(continental, aes(x=Continent, y=Records, fill=Continent)) +
geom_bar(stat="identity") +
theme_light() +
theme(legend.position="none", text=element_text(size=20)) +
labs(title="Number of records by continents") +
geom_text(aes(label=Records), vjust=1.6, color="white", size=5.5)
dsByContinent
#BIGMAC PRICES BY CONTINENTS BOXPLOT
tapply(dsByContinent$dollar_price, dsByContinent$continent, FUN=summary, na.rm=T)
secondplot<-ggplot(dsByContinent, aes(x=continent, y=dollar_price, fill=continent)) +
theme_light() +
geom_boxplot(outlier.shape=NA) +
ylim(0,8) +
labs(title="Bigmac prices by continents", x="Continent", y="Price (dollar)") +
theme(legend.position="none", text=element_text(size=20))
g<-grid.arrange(firstplot, secondplot, nrow=2)
g
ggsave("resultS/listOfPricesAndContinents.png",
plot=g,
width=1000,
height=800,
dpi=72,
units="px")
condition<-dsByContinent$continent=="Americas"
dsByContinent[condition, "name"] %>%
unique() %>%
length()
#BIGMAC PRICES IN AMERICAS
americasds<-dsByContinent[condition, c("date", "name", "dollar_price")]
head(americasds)
tapply(americasds$dollar_price, americasds$name, FUN=summary, na.rm=T)
ggplot(americasds, aes(x=dollar_price, y=name, fill=name)) +
theme_light() +
geom_boxplot(outlier.shape=NA) +
theme(text=element_text(size=16)) +
labs(title="Bigmac price in Americas", x="Price (dollar)", y="Country") +
guides(fill=guide_legend(title="Countries"))
ggsave("results/bigmacPricesInAmericas.png",
width=600,
height=400,
dpi=72,
units="px")
#DOTPLOT BIGMAC PRICES IN AMERICAS GROUPED
ggplot(americasds, aes(x=Year(date), y=dollar_price, color=name)) +
geom_point() +
theme_light() +
theme(text=element_text(size=16)) +
labs(title="Grouped Bigmac prices in Americas", x="Year", y="Price (dollar)") +
geom_smooth(method="loess", se=F, size=0.5)
ggsave("results/pricesInAmericaGrouped.png",
width=700,
height=500,
dpi=72,
units="px")
table(dsByContinent$name) %>%
sort(decreasing=T)
chinaPrices<-dsByContinent[dsByContinent$name == "China",]
head(chinaPrices)
duplicated<-!duplicated(Year(chinaPrices$date))
chinaPrices<-chinaPrices[duplicated,]
#BIGMAC PRICES IN CHINA LINE AND POINT
chinaPrices
ggplot(chinaPrices, aes(x=Year(date),y=dollar_price)) +
theme_light() +
geom_line(colour="purple", size=0.8) +
geom_point(colour="purple", size=2.5) +
labs(title="Bigmac price in China", x="Year", y="Price (dollar)") +
theme(text=element_text(size=16))
ggsave("results/bigmacPriceinChina.png",
width=600,
height=400,
dpi=72,
units="px")
#SRO LANKA INFLATION BAR PLOT
sriLankaInf<-ds[ds$name=="Sri Lanka", c("date", "dollar_ex")]
sriLankaInf
ggplot(sriLankaInf, aes(x=Year(date), y=dollar_ex)) +
geom_bar(stat="identity", fill="steelblue") +
labs(title="Dollar exchange rate in Sri Lanka", x="Year", y="Exchange rate") +
theme_light() +
theme(text=element_text(size=16))
ggsave("results/barplotOfDollarExchangeRateInSriLanka.png",
width=600,
height=400,
dpi=72,
units="px")
#SRI LANKA INFLATION POINT PLOT
duplicat<-!duplicated(Year(sriLankaInf$date))
sriLankaInf<-sriLankaInf[duplicat,]
ggplot(sriLankaInf, aes(x=Year(date), y=dollar_ex)) +
geom_point(color="steelblue") +
labs(title="Dollar exchange rate in Sri Lanka", x="Year", y="Exchange rate") +
theme_light() +
theme(text=element_text(size=16)) +
geom_line(color="steelblue")
ggsave("results/pointPlotOfDollarExchangeRateInSriLanka.png",
width=600,
height=400,
dpi=72,
units="px")