generated from jtr13/cctemplate
-
Notifications
You must be signed in to change notification settings - Fork 78
/
waterfall_plot_meta_income_statement.Rmd
155 lines (120 loc) · 5.34 KB
/
waterfall_plot_meta_income_statement.Rmd
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
# Waterfall chart using meta income statement
Soheil Fakhrieh Kashan
```{r, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
```
```{r}
## loading needed libraries
library(waterfalls)
library(ggplot2)
library(scales)
library(readxl)
library(dplyr)
library(knitr)
library(plotly)
```
### Why to use Waterfall chart?
Waterfall chart is one of the most popular data visualizations in the Consulting and Finance presentations. The chart has a similar presentation as a bar chart but only highlights the positive or negative cumulative sequential impact variables. The purpose of a waterfall chart is mostly to explain how one number relates to another number through a series of variables or events. For example, we can effectively visualize how a company's revenue is changed from one year to the next and what factors positively or negatively contributed to it.
#### Data Source
In this short tutorial we will be exploring **META**'s quarterly income statement and use Waterfall charts to visualize their Q3 - 2022 financial statement.
META Quarterly Earnings reports: <https://investor.fb.com/financials/default.aspx>
#### Data prep
```{r}
## downlaod the Q3-2022 META's Income Statement from the above link
## read the Excel file
META_IS <- read_excel('resources/waterfall_plot_meta_income_statement/Downloadable-PL-Q3-2022.xlsx')
## select relevant revenue lines for the waterfall chart
META_summary <- META_IS[c(10,11,12,13,40,41,43),]
## remove empty columns
remove_na <- function(x) any(!is.na(x))
META_summary <-
META_summary |> select(where(remove_na))
## renaming variables
colnames(META_summary) <- c('Breakdown','Q3-2020', 'Q4-2020', 'Q1-2021', 'Q2-2021',
'Q3-2021', 'Q4-2021', 'Q1-2022', 'Q2-2022', 'Q3-2022')
```
### Waterfall library
#### META Q3-2022 Income Statement
- To use this library, the data frame must contain two columns. One with numeric values, and one with categorical variable showing the labels.
- The order of the records on the label field will be the order on the graph. If that order is not desirable, you need to change the order of the records.
```{r echo = F}
META <- read.csv('resources/waterfall_plot_meta_income_statement/META_income_statement.csv')
colnames(META)[2] = 'Q3-2021'
colnames(META)[3] = 'Q3-2022'
META_cond <- META[c(1:7),]
```
```{r ,fig.height = 5, fig.width = 16}
# For this cahrt we are going to use Q3-2022
# Selecting the income statement breakdown and one quarter
waterfall(META[,c(1,3)])+
ggtitle('META Q3-2022 Income Statement Waterfall')+
labs(x='Income Breakdown', y='$MM')+
theme_minimal()
```
#### Improving the chart
- Add a total calculation to show the final summation: `calc_total = TRUE`
- Change the text size to make it larger: `rect_text_size = 1.5`
- Change the scale to show dollar: `scale_y_continuous(labels = dollar)`
```{r ,fig.height = 5, fig.width = 12}
# 'META Q3-2021 Income Statement Waterfall chart
waterfall(
META_cond[,c(1,3)],
rect_text_size = 1.5,
rect_text_labels_anchor = "centre",
calc_total = TRUE,
fill_by_sign = TRUE,
total_rect_color ='Dark Green',
total_axis_text ='Income')+
ggtitle('META Q3-2021 Income Statement Waterfall')+
labs(x='Income Breakdown', y='$MM')+
theme_minimal()+
scale_y_continuous(labels = dollar)
```
```{r echo = F}
META_QoQ <- read.csv('resources/waterfall_plot_meta_income_statement/META_quarter_to_quarter.csv')
META_QoQ$Amount<- as.numeric(gsub(",", "", META_QoQ$Amount))
```
#### How year-over-year quarterly income changed?
- Create two new records. One for the Q3-2021 and one for Q3-2022.
- For the income breakdown categories, calculate the delta between two periods
```{r}
kable(META_QoQ)
```
```{r ,fig.height = 5, fig.width = 14}
# META Q3-2022 to Q4-2022 Income Waterfall chart
waterfall(META_QoQ,
# changing the text size
rect_text_size = 1.5,
# selecting desired colors for each bar
fill_colours = c("seagreen", "orangered2", "seagreen", "orangered2",
"seagreen", "orangered2","orangered2","orangered2",
"seagreen"),
# changing the default fill by sign so we can assign individual color
fill_by_sign = FALSE)+
ggtitle('META Q3-2022 to Q4-2022 Income')+
labs(x='Income Breakdown', y='$MM')+
theme_minimal()+
scale_y_continuous(labels = dollar)
```
From the waterfall chart we can observe that Q3-2022 income is significantly lower
than the last year. This is a mainly driven by the increase in expenses in research and
development, and the decrease in revenue from advertising.
##### Using plotly to create simple waterfall
Alternatively we can use plotly to create interactive waterfall charts.
https://plotly.com/r/waterfall-charts/
``` {r}
wf <- plot_ly(
META_cond[,c(1,3)], type = "waterfall",
## list the breakdown. if the list is not specified. plotly order the categories alphabetically.
x = ~list('Advertising', 'Reality Labs', 'Other revenue',
'Research and development', 'Cost of revenue',
'Marketing and sales', 'General and administrative'),
textposition = "outside", y= ~META_cond$`Q3-2022`)
wf <- wf %>%
layout(title = "META Q3-2022 Income Statement Waterfall ",
xaxis = list(title = "Income Breakdown"),
yaxis = list(title = "$MM"),
autosize = TRUE,
showlegend = TRUE)
wf
```