---
title: "Interactive Data Visuals"
subtitle: "Corporate Financial & Social Media Insights"
author: "Emrah Akbas"
date: today
format:
html:
theme: cosmo
toc: true
toc-depth: 3
toc-title: "Table of Contents"
code-fold: true
code-tools: true
code-link: true
embed-resources: true
grid:
sidebar-width: 250px
body-width: 850px
margin-width: 250px
execute:
echo: false
warning: false
message: false
---
```{r}
pacman::p_load(tidyverse, highcharter, tidytext, quantmod, pals)
pacman::p_load_gh("lchiffon/wordcloud2")
```
```{r, message=FALSE, warning=FALSE}
library(readr)
d1 <- read_csv("C:/Users/151495/Downloads/company_financials_csv.zip")
```
## Pie chart
This pie chart illustrates the market share distribution of total Operating Income Before Depreciation (OIADPQ) across all companies in the dataset for the 2023 fiscal year.
```{r}
d1 |>
filter(fyearq == 2023) |>
group_by(conm) |>
summarize(oiadpq = sum(oiadpq, na.rm = TRUE), .groups = "drop") |>
arrange(oiadpq) |>
mutate(conm = str_to_title(conm)) |>
hchart("pie", hcaes(x = conm, y = oiadpq))
```
## Column Chart
This column chart visualizes the financial performance of Meta Platforms, Inc. (META) by tracking its Quarterly Operating Income Before Depreciation (OIADPQ) aggregated by fiscal year (fyearq).
```{r}
plot1 = d1 |>
filter(tic == "META") |>
group_by(fyearq) |>
summarize(oiadpq = sum(oiadpq, na.rm = TRUE), .groups = "drop") |>
hchart(type = "column", hcaes(x = as.factor(fyearq), y = oiadpq)) |>
hc_add_theme(hc_theme_darkunica()) |>
hc_title(text = '<span style="font-family: helvetica, inter, sans-serif;"><strong><span style="color: #FFFFFF;">Quarterly Operating Income Before Depreciation (OIADPQ) for Meta Platforms, Inc. (META), by Fiscal Year.</span></strong></span>',
useHTML = TRUE, align = "center")
plot1
```
## Line graph
This line chart tracks the evolving capital structure of Apple Inc. (AAPL) by comparing two complementary balance sheet metrics over time: Financial Leverage and the Equity Ratio. Both metrics are scaled against Total Assets (atq) to provide a standardized view of how the company finances its operations.
```{r}
#| fig-width: 8
#| fig-height: 5
#| eval: true
chart_data <- d1 |>
filter(tic == "AAPL") |>
mutate(
leverage = (replace_na(dlttq, 0) + replace_na(dlcq, 0)) / atq,
leverage = round(leverage, 2),
equity_ratio = replace_na(ceqq, 0) / atq,
equity_ratio = round(equity_ratio, 2)
)
# 2. Build the chart with two lines
highchart() |>
hc_add_series(data = chart_data, type = "line", hcaes(x = datadate, y = leverage), name = "Financial Leverage (Debt/Assets)") |>
hc_add_series(data = chart_data, type = "line", hcaes(x = datadate, y = equity_ratio), name = "Equity Ratio (Equity/Assets)") |>
hc_title(text = "Apple Inc. (AAPL) Capital Structure Trends", align = "left") |>
hc_subtitle(text = "Quarterly comparison of leverage and equity ratios over time", align = "left") |>
hc_xAxis(title = list(text = "Date")) |>
hc_yAxis(title = list(text = "Ratio Value")) |>
hc_add_theme(hc_theme_google())
```
## Scatterplot
This scatter plot visualizes the relationship between top-line revenue and core operating profitability across different companies in the dataset, grouped by company name.
```{r}
#| fig-width: 8
#| fig-height: 5
d1 |>
hchart("scatter", hcaes(x = saleq, y = oibdpq, group = conm)) |>
hc_add_theme(hc_theme_538())
```
## Scatter plot with Regression Line
```{r}
#| fig-width: 8
#| fig-height: 5
d1 |>
filter(tic == "AAPL") |>
hchart("scatter", hcaes(x = saleq, y = oibdpq), regression = TRUE) |>
hc_add_dependency("plugins/highcharts-regression.js") |>
hc_add_theme(hc_theme_538())
```
## Heatmap
This visualization is a correlation matrix heatmap computed using Pearson correlation coefficients (cor()). It evaluates the strength and direction of linear relationships between eight key financial variables for companies in the dataset.
```{r}
cor_dt = d1 |>
select(saleq, oibdpq, cogsq, atq, xrdq, mkvaltq, cheq, capxy) |>
drop_na() |>
cor()
```
```{r}
class(cor_dt)
```
```{r}
cor_dt |>
hchart() |>
hc_colorAxis(
stops = color_stops(colors = rev(c("#000004FF",
"#56106EFF",
"#BB3754FF",
"#F98C0AFF",
"#FCFFA4FF")))
)
```
## Wordcloud
This visualization is a word cloud that highlights the most frequently used words in a harvested dataset of Twitter/X posts (tweets). It provides a rapid, qualitative snapshot of the central themes, recurring topics, or dominant sentiments present in the social media text.To create the wordcloud, we need words and their frequencies from text. For this example, I will use a few random tweets collected in November 2021.
```{r}
tweets = readRDS("C:/Users/151495/Downloads/file001.rds")
```
```{r}
tweet_text = tibble(text = tweets$text) |>
mutate(text = gsub(x = text, pattern = "[0-9]+|[[:punct:]]|\\(.*\\)", replacement = "")) |>
tidytext::unnest_tokens(word, text)
```
```{r}
data(stop_words)
tweet_text = tweet_text |> anti_join(stop_words)
```
```{r}
tweet_text = tweet_text |>
filter(!word %in% c("im", "dont", "amp", "youre", "ive", "hes", "didnt", "isnt"))
```
```{r}
word_freq = tweet_text |>
count(word, sort = T)
```
```{r}
word_freq |>
filter(n > 100) |>
hchart("wordcloud", hcaes(name = word, weight = n), name = "Count")
```