library(tidyverse)
library(ggplot2)
library(dplyr)
library(rvest)
library(ggplot2)
library(scales)
library(data.table)
library(plotly)

Data Preparation

bkm <- read_html("https://bkm.com.tr/secilen-aya-ait-sektorel-gelisim/?filter_year=2019&filter_month=6&List=Listele")

tables <- html_nodes(bkm, "table")
head(tables)
tables_list <- bkm %>%
  html_nodes("table") %>%
  .[4] %>%
  html_table(fill = TRUE)

str(tables_list)
head(tables_list[[1]], 26)
tables_list[[1]] <- tables_list[[1]] %>% slice(2:26)
head(tables_list[[1]], 26)

Renew Column Names

colnames(tables_list[[1]]) <- c("Sector_Name", "Number_of_Transactions_Credit_Card", "Number_of_Transactions_Debit_Card",
                            "Transaction_Amount_Credit_Card", "Transaction_Amount_Debit_Card")
head(tables_list[[1]], 28)
tables_list[[1]][,2:5]  <- as.data.frame(lapply(tables_list[[1]][,2:5], function(x) as.numeric(gsub(",", ".", gsub("\\.", "", x)))))
head(tables_list[[1]], 28)

Analysis 1: Amount of Debit Card Transactions for 20 sector

ggplot(tables_list[[1]], aes(x = Sector_Name, y = Transaction_Amount_Credit_Card), inherit.aes = FALSE) +  
  geom_bar(position="dodge", stat="identity") +  
  theme(axis.text.x = element_text(angle = 90)) +  
  scale_y_continuous(labels = comma) +  
  labs(subtitle="Banka Kartı Islem Tutari (TL)", title= "BKM",  
       caption="(based on data from BKM)", y="Debit Amount", x="Sector Name")  

Analysis 2: Percentage of Average Transaction Credit Card Amounts For March 2019

last_table <- tables_list[[1]] %>%  
  transmute(Sector_Name, Average_transaction_amount_Credit_Card = (Transaction_Amount_Credit_Card / Number_of_Transactions_Credit_Card)*1000000) %>%  
  arrange(desc(Average_transaction_amount_Credit_Card)) %>%  
  mutate(Sector_Name = case_when(Average_transaction_amount_Credit_Card > 400 ~ Sector_Name, TRUE ~ "Others")) %>%  
  group_by(Sector_Name) %>%  
  transmute(Total_Average_transaction_amount_Credit_Card = sum(Average_transaction_amount_Credit_Card)) %>%  
  distinct() %>%  
  arrange(desc(Total_Average_transaction_amount_Credit_Card)) %>% ungroup() %>%  
  transmute(Sector_Name,percentages = round(Total_Average_transaction_amount_Credit_Card*100,2))  

ggplot(data = last_table , aes(x = "", y = percentages, fill = Sector_Name), inherit.aes = FALSE) +  
  geom_bar(width = 1, stat = "identity", color = "white") +  
  coord_polar("y", start = 0) +  
  geom_text(aes(x = 1.3, y = percentages, label = percent(percentages/1000000) ),position = position_stack(vjust = 0.5),color = "white") +  
  labs(fill = "Sectors") +  
  theme_void()