Necessary packages for having a comprehensive analysis are listed below:
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(rvest)
library(ggplot2)
library(scales)
library(dplyr)
library(plyr)
All BKM data from 2015 to the 6th month of 2019 were obtained.
bkm_sektor <- function(year,month) {
url <- paste('https://bkm.com.tr/secilen-aya-ait-sektorel-gelisim/?filter_year=',year,'&filter_month=',month,'&List=Listele', sep='',collapse = NULL)
return(url)
}
yil_parametre <-c('2015','2016','2017','2018','2019')
ay_parametre <- c('01','02','03','04','05','06','07','08','09','10','11','12')
total_data <- ''
for (i in 1:length(yil_parametre)) {
for (j in 1:length(ay_parametre)) {
if (yil_parametre[i]=='2019' & ay_parametre[j]=='07') {break}
url <- bkm_sektor(yil_parametre[i],ay_parametre[j])
page <- read_html(url)
df <- page %>% html_nodes("table") %>%.[(4)] %>% html_table(page, fill = TRUE,header = FALSE) %>% as.data.frame()
df <- df %>%mutate_if(is.numeric,funs(ifelse(is.na(.),0,.))) %>% mutate(yearmonth=paste(yil_parametre[i],ay_parametre[j],sep=''))
total_data<-rbind(total_data,df)
}
}
colnames(total_data) <- c('isyeri_grubu','islem_adet_kk','islem_adet_bk','islem_tutar_kk','islem_tutar_bk' ,'yearmonth')
total_data <- total_data %>% slice(2:max(nrow(.))) %>% filter(isyeri_grubu !='TOPLAM')
total_data <- total_data %>% filter(isyeri_grubu !='Ä°ÅŸyeri Grubu')
bkm_data <-total_data
bkm_data[,2:5] <- as.data.frame(lapply(bkm_data[,2:5], function(x) as.numeric(gsub(",", ".", gsub("\\.", "", x)))))
head(bkm_data, 10)
## isyeri_grubu islem_adet_kk islem_adet_bk
## 1 ARABA KÄ°RALAMA 145150 12932
## 2 ARAÇ KİRALAMA-SATIŞ/SERVİS/YEDEK PARÇA 2630077 310014
## 3 BENZÄ°N VE YAKIT Ä°STASYONLARI 19577823 3838459
## 4 ÇEŞİTLİ GIDA 17780721 6219640
## 5 DOÄžRUDAN PAZARLAMA 537690 46977
## 6 EĞİTİM / KIRTASİYE / OFİS MALZEMELERİ 4590558 1209397
## 7 ELEKTRÄ°K-ELEKTRONÄ°K EÅžYA, BÄ°LGÄ°SAYAR 7246433 737629
## 8 GÄ°YÄ°M VE AKSESUAR 26589276 5292159
## 9 HAVAYOLLARI 1979264 123354
## 10 HİZMET SEKTÖRLERİ 13939802 2243915
## islem_tutar_kk islem_tutar_bk yearmonth
## 1 78.47 2.59 201501
## 2 1269.03 47.57 201501
## 3 3196.04 226.34 201501
## 4 2435.24 174.03 201501
## 5 92.95 2.82 201501
## 6 819.86 58.44 201501
## 7 2579.01 130.74 201501
## 8 2950.10 398.78 201501
## 9 657.67 62.47 201501
## 10 3615.39 125.23 201501
Total transaction amount data from 2015 to 2019 were collected.
#Data set preparation for chart
toplam_islem_tutar <-bkm_data %>% mutate( toplam = (islem_tutar_kk + islem_tutar_bk))%>% select(isyeri_grubu,yearmonth,toplam)%>%filter(toplam!=0)
toplam_islem_tutar_2015<-toplam_islem_tutar %>% filter(grepl("^2015", yearmonth))
toplam_islem_tutar_2016<-toplam_islem_tutar %>% filter(grepl("^2016", yearmonth))
toplam_islem_tutar_2017<-toplam_islem_tutar %>% filter(grepl("^2017", yearmonth))
toplam_islem_tutar_2018<-toplam_islem_tutar %>% filter(grepl("^2018", yearmonth))
toplam_islem_tutar_2019<-toplam_islem_tutar %>% filter(grepl("^2019", yearmonth))
toplam_islem_tutar_2015<-ddply(toplam_islem_tutar_2015,"isyeri_grubu",numcolwise(sum))
toplam_islem_tutar_2016<-ddply(toplam_islem_tutar_2016,"isyeri_grubu",numcolwise(sum))
toplam_islem_tutar_2017<-ddply(toplam_islem_tutar_2017,"isyeri_grubu",numcolwise(sum))
toplam_islem_tutar_2018<-ddply(toplam_islem_tutar_2018,"isyeri_grubu",numcolwise(sum))
toplam_islem_tutar_2019<-ddply(toplam_islem_tutar_2019,"isyeri_grubu",numcolwise(sum))
head(toplam_islem_tutar_2015, 10)
## isyeri_grubu toplam
## 1 ARABA KÄ°RALAMA 1330.44
## 2 ARAÇ KİRALAMA-SATIŞ/SERVİS/YEDEK PARÇA 18622.43
## 3 BENZÄ°N VE YAKIT Ä°STASYONLARI 49106.46
## 4 BIREYSEL EMEKLILIK 3731.21
## 5 ÇEŞİTLİ GIDA 34952.31
## 6 DÄ°ÄžER 10940.36
## 7 DOÄžRUDAN PAZARLAMA 1349.76
## 8 EĞİTİM / KIRTASİYE / OFİS MALZEMELERİ 13570.05
## 9 ELEKTRÄ°K-ELEKTRONÄ°K EÅžYA, BÄ°LGÄ°SAYAR 36004.16
## 10 GÄ°YÄ°M VE AKSESUAR 44290.78
All data are added to the year they belong.
year <- '2015'
islem_tutar_2015 <- cbind(toplam_islem_tutar_2015, year)
year <- '2016'
islem_tutar_2016 <- cbind(toplam_islem_tutar_2016, year)
year <- '2017'
islem_tutar_2017 <- cbind(toplam_islem_tutar_2017, year)
year <- '2018'
islem_tutar_2018 <- cbind(toplam_islem_tutar_2018, year)
year <- '2019'
islem_tutar_2019 <- cbind(toplam_islem_tutar_2019, year)
islem_tutari_data <- rbind(islem_tutar_2015, islem_tutar_2016, islem_tutar_2017, islem_tutar_2018, islem_tutar_2019)
islem_tutari_data$year <-as.character(islem_tutari_data$year)
tail(islem_tutari_data, 10)
## isyeri_grubu toplam year
## 121 KUYUMCULAR 8484.07 2019
## 122 MARKET VE ALIÅžVERÄ°Åž MERKEZLERÄ° 83788.70 2019
## 123 MOBÄ°LYA VE DEKORASYON 15255.34 2019
## 124 MÃœTEAHHÄ°T Ä°ÅžLERÄ° 4956.95 2019
## 125 SAÄžLIK/SAÄžLIK ÃœRÃœNLERÄ°/KOZMETÄ°K 19205.58 2019
## 126 SEYAHAT ACENTELERÄ°/TAÅžIMACILIK 14118.59 2019
## 127 SÄ°GORTA 18983.67 2019
## 128 TELEKOMÃœNÄ°KASYON 12078.66 2019
## 129 YAPI MALZEMELERÄ°, HIRDAVAT, NALBURÄ°YE 17692.26 2019
## 130 YEMEK 22506.26 2019
Total transaction amount was calculated by years.
bkm_islem_tutari <- aggregate(islem_tutari_data$toplam, by=list(year=islem_tutari_data$year), FUN=sum)
library(ggplot2)
theme_set(theme_bw())
options(scipen=999)
# Draw plot
ggplot(bkm_islem_tutari, aes(x=year, y=x)) +
geom_bar(stat="identity", width=.5, fill="tomato3") +
labs(title="Total Transaction Amount by Years",
subtitle="Amount of Transaction from All Sectors",
y = "Total Transaction Amount",
x = "Year",
caption="source: mpg") +
theme(axis.text.x = element_text(angle=65, vjust=0.6))
Sectoral Distribution of Total Transaction Amount was calculated. Market and Shopping Centers had the highest share.
library(ggplot2)
library(scales)
ggplot(islem_tutari_data, aes(x=isyeri_grubu, y=toplam)) +
geom_bar(stat="identity",fill="tomato3")+ coord_flip() + scale_y_continuous(labels=comma) +
labs(title = "Sectoral Distribution of Total Transaction Amount", x="", y="") +theme_minimal()
The total transaction amount of the sectors in all years was calculated.
bkm_islem_tutari <- aggregate(islem_tutari_data$toplam, by=list(isyeri_grubu=islem_tutari_data$isyeri_grubu), FUN=sum)
head(bkm_islem_tutari, 10)
## isyeri_grubu x
## 1 ARABA KÄ°RALAMA 9245.06
## 2 ARAÇ KİRALAMA-SATIŞ/SERVİS/YEDEK PARÇA 109801.11
## 3 BENZÄ°N VE YAKIT Ä°STASYONLARI 279730.90
## 4 BIREYSEL EMEKLILIK 30128.21
## 5 ÇEŞİTLİ GIDA 211903.41
## 6 DÄ°ÄžER 73529.93
## 7 DOÄžRUDAN PAZARLAMA 20354.80
## 8 EĞİTİM / KIRTASİYE / OFİS MALZEMELERİ 79653.60
## 9 ELEKTRÄ°K-ELEKTRONÄ°K EÅžYA, BÄ°LGÄ°SAYAR 178644.06
## 10 GÄ°YÄ°M VE AKSESUAR 256556.52
The 5 sectors with the highest amount of transactions were identified and their distribution was plotted.
bkm_islem_tutari <- bkm_islem_tutari[order(bkm_islem_tutari$x, decreasing = TRUE),]
bkm_islem_tutari_top5 <- bkm_islem_tutari %>% slice(1:5)
# Compute percentages
bkm_islem_tutari_top5$fraction = bkm_islem_tutari_top5$x / sum(bkm_islem_tutari_top5$x)
# Compute the cumulative percentages (top of each rectangle)
bkm_islem_tutari_top5$ymax = cumsum(bkm_islem_tutari_top5$fraction)
# Compute the bottom of each rectangle
bkm_islem_tutari_top5$ymin = c(0, head(bkm_islem_tutari_top5$ymax, n=-1))
# Make the plot
ggplot(bkm_islem_tutari_top5, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=isyeri_grubu)) +
geom_rect() +
coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially
xlim(c(2, 4)) # Try to remove that to see how to make a pie chart