library(dplyr)
## 
## Присоединяю пакет: 'dplyr'
## Следующие объекты скрыты от 'package:stats':
## 
##     filter, lag
## Следующие объекты скрыты от 'package:base':
## 
##     intersect, setdiff, setequal, union
library(corrplot)
## corrplot 0.92 loaded
library(ggplot2)
library(tidyr)
library(viridis)
## Загрузка требуемого пакета: viridisLite
library(readxl)
library(circlize)
## ========================================
## circlize version 0.4.15
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: https://jokergoo.github.io/circlize_book/book/
## 
## If you use it in published research, please cite:
## Gu, Z. circlize implements and enhances circular visualization
##   in R. Bioinformatics 2014.
## 
## This message can be suppressed by:
##   suppressPackageStartupMessages(library(circlize))
## ========================================
library(waterfalls)

dataforR <- read_excel("dataforR.xlsx")

JD <- dataforR %>% filter(Company == "JD") %>% filter(Indicators %in% c("Asset Turnover", "Equity multiplier", 
                                                                        "Tax Burden", "Interest Burden", "Return on Sales"))
JD1 <- as.data.frame(t(JD[,-c(1,8)]))[-1,]
colnames(JD1) <- c("Asset Turnover", "Equity multiplier", 
                   "Tax Burden", "Interest Burden", "Return on Sales")
JD1
##      Asset Turnover Equity multiplier   Tax Burden Interest Burden
## 2014      2.4900000         1.9800000    0.8610824       1.0000000
## 2015     2.39071865        2.22452183   1.50111928      0.96826134
## 2016    2.118774945       3.415115859  5.427662957     0.293240093
## 2017    2.103957565       3.679443216 -0.009958071    -2.285029940
## 2018    2.349931336       3.043098044  1.659360190     0.644520809
##      Return on Sales
## 2014      -0.0504513
## 2015     -0.03562859
## 2016    -0.008246131
## 2017    -0.002304516
## 2018    -0.005668586
JD1$`Asset Turnover` <- as.numeric(JD1$`Asset Turnover`)
JD1$`Equity multiplier` <- as.numeric(JD1$`Equity multiplier`)
JD1$`Tax Burden` <- as.numeric(JD1$`Tax Burden`)
JD1$`Interest Burden` <- as.numeric(JD1$`Interest Burden`)
JD1$`Return on Sales` <- as.numeric(JD1$`Return on Sales`)
JD1$ROE <- JD1$`Asset Turnover`*JD1$`Equity multiplier`*JD1$`Tax Burden`*JD1$`Interest Burden`*JD1$`Return on Sales`

corrplot(as.matrix(cor(JD1)))

JD2 <- as.data.frame(cor(JD1))
JD2$Indicator1 <- rownames(JD2)

JD3 <- JD2 %>% pivot_longer(cols = "Asset Turnover":"ROE", names_to = "Indicator2", values_to = "corr")
ggplot(JD3, aes(Indicator1,Indicator2, fill = corr)) + 
  geom_tile(aes(fill = corr)) +
  scale_fill_viridis() +
  geom_text(aes(label = paste(round(corr,2)))) + 
  theme(axis.text.x= element_text(angle=90)) + xlab("") + ylab("") + theme_minimal()

corrplot(as.matrix(cor(JD1)), type = "upper", diag = FALSE, addCoef.col = TRUE, tl.col = "black", 
         method="color")

#для JD отследим динамику компонентов Asset Turnover, Equity multiplier, Interest Burden и Return on sales
dataforR
## # A tibble: 36 × 8
##    Company Indicators          `2014`   `2015`   `2016`   `2017`   `2018` `2019`
##    <chr>   <chr>                <dbl>    <dbl>    <dbl>    <dbl>    <dbl>  <dbl>
##  1 JD      Net Profit Margin -4.34e-2 -5.18e-2 -1.31e-2 -5.24e-5 -6.06e-3     NA
##  2 JD      Net Income        -5.00e+3 -9.39e+3 -3.41e+3 -1.9 e+1 -2.80e+3     NA
##  3 JD      Sales              1.15e+5  1.81e+5  2.60e+5  3.62e+5  4.62e+5     NA
##  4 JD      Asset Turnover     2.49e+0  2.39e+0  2.12e+0  2.10e+0  2.35e+0     NA
##  5 JD      Sales              1.15e+5  1.81e+5  2.60e+5  3.62e+5  4.62e+5     NA
##  6 JD      Average Total As…  4.62e+4  7.58e+4  1.23e+5  1.72e+5  1.97e+5     NA
##  7 JD      Equity multiplier  1.98e+0  2.22e+0  3.42e+0  3.68e+0  3.04e+0     NA
##  8 JD      Average Total As…  4.62e+4  7.58e+4  1.23e+5  1.72e+5  1.97e+5     NA
##  9 JD      Average Sharehol…  2.33e+4  3.41e+4  3.59e+4  4.68e+4  6.46e+4     NA
## 10 JD      Tax Burden         8.61e-1  1.50e+0  5.43e+0 -9.96e-3  1.66e+0     NA
## # … with 26 more rows
JD4 <- t(unique((dataforR %>% filter(Company == "JD"))[c(5,6,8,9,14,15,17,18),-c(1,8)]))
View(JD4)
colnames(JD4) <- c("Sales", "Average Total Assets", "Average Shareholder's Equity", "Earnings before tax", "Operating profit")
JD5 <- as.data.frame(JD4[-1,])
JD5$Sales <- as.numeric(JD5$Sales)
JD5$`Average Total Assets` <- as.numeric(JD5$`Average Total Assets`)
JD5$`Average Shareholder's Equity` <- as.numeric(JD5$`Average Shareholder's Equity`)
JD5$`Earnings before tax` <- as.numeric(JD5$`Earnings before tax`)
JD5$`Operating profit` <- as.numeric(JD5$`Operating profit`)

JD5$ROE <- JD1$ROE

corrplot(as.matrix(cor(JD5)), type = "upper", diag = FALSE, addCoef.col = TRUE, tl.col = "black", 
         method="color")

JD5$years <- rownames(JD5)
JD6 <- JD5 %>% pivot_longer(cols = "Sales":"Operating profit", values_to = "Values", names_to = "Indicators")

ggplot(data = JD6, aes(x = years, y = Values, fill = Indicators)) + geom_col(position = "dodge") + facet_wrap(~Indicators, scales = "free") + theme_classic() + geom_label(aes(label = round(Values, 0)), size = 3) + theme(legend.position = "none") + scale_fill_manual(values = c("#c4d0d9", "#7a6e6c", "#c3beba","#f0e9dd", "#ecede3")) + ylab("") + xlab("") + scale_y_continuous(labels=scales::comma)

ggplot(data = JD6, aes(x = years, y = ROE, group = 1)) + geom_col(fill = "darkblue") + theme_classic() + geom_label(aes(label = round(ROE,3))) + scale_y_continuous(labels = scales::percent) 

#Анализ Alibaba

Alibaba <- dataforR %>% filter(Company == "Alibaba") %>% filter(Indicators %in% c("Asset Turnover", "Equity multiplier", 
                                                                        "Tax Burden", "Interest Burden", "Return on Sales"))
Alibaba1 <- as.data.frame(t(Alibaba[,-c(1,3)]))[-1,]
colnames(Alibaba1) <- c("Asset Turnover", "Equity multiplier", 
                   "Tax Burden", "Interest Burden", "Return on Sales")
Alibaba1
##      Asset Turnover Equity multiplier Tax Burden Interest Burden
## 2015      0.4200000         1.8500000  1.7777778       0.5913119
## 2016      0.3263288         2.4790692 -3.0791724      -0.7955467
## 2017      0.3633190         1.5178516  1.0438019       0.8218916
## 2018      0.4089528         1.6029336  1.5820088       0.5600456
## 2019      0.4480371         1.5947427  6.1823085       0.2273492
##      Return on Sales
## 2015       0.3035930
## 2016       0.2877312
## 2017       0.3036210
## 2018       0.2769613
## 2019       0.1514791
Alibaba1$`Asset Turnover` <- as.numeric(Alibaba1$`Asset Turnover`)
Alibaba1$`Equity multiplier` <- as.numeric(Alibaba1$`Equity multiplier`)
Alibaba1$`Tax Burden` <- as.numeric(Alibaba1$`Tax Burden`)
Alibaba1$`Interest Burden` <- as.numeric(Alibaba1$`Interest Burden`)
Alibaba1$`Return on Sales` <- as.numeric(Alibaba1$`Return on Sales`)
Alibaba1$ROE <- Alibaba1$`Asset Turnover`*Alibaba1$`Equity multiplier`*Alibaba1$`Tax Burden`*Alibaba1$`Interest Burden`*Alibaba1$`Return on Sales`

corrplot(as.matrix(cor(JD1)))

Alibaba2 <- as.data.frame(cor(Alibaba1))
Alibaba2$Indicator1 <- rownames(Alibaba2)

Alibaba3 <- Alibaba2 %>% pivot_longer(cols = "Asset Turnover":"ROE", names_to = "Indicator2", values_to = "corr")
ggplot(Alibaba3, aes(Indicator1,Indicator2, fill = corr)) + 
  geom_tile(aes(fill = corr)) +
  scale_fill_viridis() +
  geom_text(aes(label = paste(round(corr,2)))) + 
  theme(axis.text.x= element_text(angle=90)) + xlab("") + ylab("") + theme_minimal()

corrplot(as.matrix(cor(Alibaba1)), type = "upper", diag = FALSE, addCoef.col = TRUE, tl.col = "black", 
         method="color")

#для Alibaba отследим динамику компонентов Asset Turnover, Equity multiplier, Interest Burden и Tax Burden
dataforR
## # A tibble: 36 × 8
##    Company Indicators          `2014`   `2015`   `2016`   `2017`   `2018` `2019`
##    <chr>   <chr>                <dbl>    <dbl>    <dbl>    <dbl>    <dbl>  <dbl>
##  1 JD      Net Profit Margin -4.34e-2 -5.18e-2 -1.31e-2 -5.24e-5 -6.06e-3     NA
##  2 JD      Net Income        -5.00e+3 -9.39e+3 -3.41e+3 -1.9 e+1 -2.80e+3     NA
##  3 JD      Sales              1.15e+5  1.81e+5  2.60e+5  3.62e+5  4.62e+5     NA
##  4 JD      Asset Turnover     2.49e+0  2.39e+0  2.12e+0  2.10e+0  2.35e+0     NA
##  5 JD      Sales              1.15e+5  1.81e+5  2.60e+5  3.62e+5  4.62e+5     NA
##  6 JD      Average Total As…  4.62e+4  7.58e+4  1.23e+5  1.72e+5  1.97e+5     NA
##  7 JD      Equity multiplier  1.98e+0  2.22e+0  3.42e+0  3.68e+0  3.04e+0     NA
##  8 JD      Average Total As…  4.62e+4  7.58e+4  1.23e+5  1.72e+5  1.97e+5     NA
##  9 JD      Average Sharehol…  2.33e+4  3.41e+4  3.59e+4  4.68e+4  6.46e+4     NA
## 10 JD      Tax Burden         8.61e-1  1.50e+0  5.43e+0 -9.96e-3  1.66e+0     NA
## # … with 26 more rows
Alibaba4 <- t(unique((dataforR %>% filter(Company == "Alibaba"))[c(5,6,8,9,14,15,11,12),-c(1,3)]))
colnames(Alibaba4) <- c("Sales", "Average Total Assets", "Average Shareholder's Equity", "Earnings before tax", "Operating profit", "Net Income")
Alibaba5 <- as.data.frame(Alibaba4[-1,])
Alibaba5$Sales <- as.numeric(Alibaba5$Sales)
Alibaba5$`Average Total Assets` <- as.numeric(Alibaba5$`Average Total Assets`)
Alibaba5$`Average Shareholder's Equity` <- as.numeric(Alibaba5$`Average Shareholder's Equity`)
Alibaba5$`Earnings before tax` <- as.numeric(Alibaba5$`Earnings before tax`)
Alibaba5$`Operating profit` <- as.numeric(Alibaba5$`Operating profit`)
Alibaba5$`Net Income` <- as.numeric(Alibaba5$`Net Income`)
Alibaba5$ROE <- Alibaba1$ROE

corrplot(as.matrix(cor(Alibaba5)), type = "upper", diag = FALSE, addCoef.col = TRUE, tl.col = "black", 
         method="color")

Alibaba5$years <- rownames(Alibaba5)
Alibaba6 <- Alibaba5 %>% pivot_longer(cols = "Sales":"Net Income", values_to = "Values", names_to = "Indicators")

ggplot(data = Alibaba6, aes(x = years, y = Values, fill = Indicators)) + geom_col(position = "dodge") + facet_wrap(~Indicators, scales = "free") + theme_classic() + geom_label(aes(label = round(Values, 0)), size = 3) + theme(legend.position = "none") + scale_fill_manual(values = c("#c4d0d9", "#7a6e6c", "#c3beba","#f0e9dd", "#ecede3", "#9d9c9a")) + ylab("") + xlab("") + scale_y_continuous(labels=scales::comma)

ggplot(data = Alibaba6, aes(x = years, y = ROE, group = 1)) + geom_col(fill = "darkblue") + theme_classic() + geom_label(aes(label = round(ROE,3))) + scale_y_continuous(labels = scales::percent) 

dataforR1 <- read_excel("dataforR1.xlsx")
Alibaba7 <- (dataforR1 %>% filter(Company == "Alibaba") %>% select(-Company, -"2014", -"Structure2"))[-c(2:8),] %>% pivot_longer(cols = "2015":"2019", names_to = "Year", values_to = "Revenue")
chordDiagram(Alibaba7)

Alibaba8 <- (dataforR1 %>% filter(Company == "Alibaba") %>% filter(Structure1 == "Core commerce") %>% select(-Company, -"2014", -"Structure1"))[-c(1),] %>% pivot_longer(cols = "2015":"2019", names_to = "Year", values_to = "Revenue") 
Alibaba9 <- (dataforR1 %>% filter(Company == "Alibaba") %>% select(-Company, -"2014"))[-c(1),] %>% pivot_longer(cols = "2015":"2019", names_to = "Year", values_to = "Revenue") %>% filter(Year == 2019) 

ggplot(data = Alibaba8, aes(x = Year, y = Revenue, fill = Structure2)) + geom_col() + theme_minimal() + scale_y_continuous(labels = scales::comma) + xlab("") + ylab("") + theme(legend.position = "bottom") + scale_fill_viridis_d()

ggplot(data = Alibaba8, aes(x = Year, y = Revenue, fill = Structure2)) + geom_col(position = "fill") + theme_minimal() + scale_y_continuous(labels = scales::comma) + xlab("") + ylab("") + theme(legend.position = "bottom") + scale_fill_viridis_d()

#график водопада для 2019
waterfall(values = Alibaba9$Revenue, labels = Alibaba9$Structure2) + theme_minimal() + ylab("") + xlab("")

Alibaba9$group_id <- NULL
Alibaba9$group_id <- ifelse(Alibaba9$Structure1 == "Core commerce", "1", 
                                          ifelse(Alibaba9$Structure1 == "Cloud computing", "2", 
                                                 ifelse(Alibaba9$Structure1 == "Digital media and entertainment", "3", "4")))

Alibaba10 <- as.data.frame(Alibaba9)
Alibaba10$startBar <- rep(0, 10)
Alibaba10$endBar  <- rep(0, 10)

i <- NULL
for (i in 1:9) {
  Alibaba10$endBar[i] <- Alibaba10$Revenue[i] + Alibaba10$startBar[i]
  Alibaba10$startBar[i+1] <- Alibaba10$endBar[i]
  Alibaba10$endBar[10] <- Alibaba10$startBar[10]+Alibaba10$Revenue[10]
}
Alibaba10
##                           Structure1                        Structure2 Year
## 1                      Core commerce             China commerce retail 2019
## 2                      Core commerce          China commerce wholesale 2019
## 3                      Core commerce     International commerce retail 2019
## 4                      Core commerce  International commerce wholesale 2019
## 5                      Core commerce        Cainiao logistics services 2019
## 6                      Core commerce           Local consumer services 2019
## 7                      Core commerce                             Other 2019
## 8                    Cloud computing                   Cloud computing 2019
## 9    Digital media and entertainment   Digital media and entertainment 2019
## 10 Innovation initiatives and others Innovation initiatives and others 2019
##    Revenue group_id startBar endBar
## 1   247615        1        0 247615
## 2     9988        1   247615 257603
## 3    19558        1   257603 277161
## 4     8167        1   277161 285328
## 5    14885        1   285328 300213
## 6    18058        1   300213 318271
## 7     5129        1   318271 323400
## 8    24702        2   323400 348102
## 9    24077        3   348102 372179
## 10    4665        4   372179 376844
Alibaba10$endBar <- Alibaba10$startBar +Alibaba10$Revenue
Alibaba10$totalByX <- c(rep(323400, 7), 348102, 372179, 376844)

Alibaba10$group_id <- as.numeric(Alibaba10$group_id )

ggplot(Alibaba10, aes(fill = Structure2)) + 
  # Waterfall Chart
  geom_rect(aes(x = Structure1, 
                xmin = group_id - 0.25, # control bar gap width
                xmax = group_id + 0.25, 
                ymin = endBar,
                ymax = startBar)) + scale_x_discrete(labels=c("Core commerce" = "Cloud computing", "Cloud computing" = "Core commerce")) + scale_fill_viridis_d() + scale_y_continuous(labels = scales::comma) + xlab("") + theme_classic() + theme(legend.position = "bottom")
## Warning in geom_rect(aes(x = Structure1, xmin = group_id - 0.25, xmax = group_id
## + : Ignoring unknown aesthetics: x

#JD
JD8 <- (dataforR1 %>% filter(Company == "JD") %>% select(-Company, -"2019", -"Structure2")) %>% pivot_longer(cols = "2014":"2018", names_to = "Year", values_to = "Revenue")
chordDiagram(JD8)

ggplot(data = JD8, aes(x = Year, y = Revenue, fill = Structure1)) + geom_col() + theme_minimal() + scale_y_continuous(labels = scales::comma) + xlab("") + ylab("") + theme(legend.position = "bottom") + scale_fill_viridis_d(option="G")

ggplot(data = JD8, aes(x = Year, y = Revenue, fill = Structure1)) + geom_col(position = "fill") + theme_minimal() + scale_y_continuous(labels = scales::comma) + xlab("") + ylab("") + theme(legend.position = "bottom") + scale_fill_viridis_d(option="G")