Install Libraries
Before we dive into the project, let’s set up our environment by installing essential packages. We’ll use thetidyverse
package for data manipulation in R, along with ggplot
and gganimate
libraries to craft our visualization and animated plot.
Load Library
Data Source
After installing the necessary packages and loading the libraries, we can start fetching the data from the Sectors Financial API. To access the API you only need to go to the Sectors website, subscribe to the plan and call the API directly from your markdown. Here is how we fetch the most-traded stock data from R using the endpoint that has been created by Sectors team.date | symbol | volume |
---|---|---|
2024-02-20 | GOTO.JK | 1033123000 |
2024-02-20 | BUMI.JK | 998119800 |
2024-02-20 | CARE.JK | 673579200 |
2024-02-20 | DEWA.JK | 509467700 |
2024-02-20 | DOOH.JK | 489452400 |
Data Manipulation
We will start our data manipulation process by calculating the cumulative sum for each stock from the beginning of 2024 until now. Additionally, we will select only the top 10 stocks with the highest cumulative sum to include in the plot. This can be accomplished using the functions available in the tidyverse package in R.date | symbol | volume | accumulated_volume | rank |
---|---|---|---|---|
2024-01-02 | MPXL.JK | 213109100 | 213109100 | 10 |
2024-01-02 | BAPA.JK | 282855200 | 282855200 | 9 |
2024-01-02 | BIPI.JK | 300042500 | 300042500 | 8 |
2024-01-02 | STRK.JK | 328691100 | 328691100 | 7 |
2024-01-02 | NATO.JK | 454095300 | 454095300 | 6 |
2024-01-02 | DOOH.JK | 489452400 | 489452400 | 5 |
2024-01-02 | DEWA.JK | 509467700 | 509467700 | 4 |
2024-01-02 | CARE.JK | 673579200 | 673579200 | 3 |
2024-01-02 | BUMI.JK | 998119800 | 998119800 | 2 |
2024-01-02 | GOTO.JK | 1033123000 | 1033123000 | 1 |
date | symbol | volume | accumulated_volume | rank | accumulated_volume_text |
---|---|---|---|---|---|
2024-01-02 | MPXL.JK | 213109100 | 213109100 | 10 | 213.11M |
2024-01-02 | BAPA.JK | 282855200 | 282855200 | 9 | 282.86M |
2024-01-02 | BIPI.JK | 300042500 | 300042500 | 8 | 300.04M |
2024-01-02 | STRK.JK | 328691100 | 328691100 | 7 | 328.69M |
2024-01-02 | NATO.JK | 454095300 | 454095300 | 6 | 454.10M |
2024-01-02 | DOOH.JK | 489452400 | 489452400 | 5 | 489.45M |
2024-01-02 | DEWA.JK | 509467700 | 509467700 | 4 | 509.47M |
2024-01-02 | CARE.JK | 673579200 | 673579200 | 3 | 673.58M |
2024-01-02 | BUMI.JK | 998119800 | 998119800 | 2 | 998.12M |
2024-01-02 | GOTO.JK | 1033123000 | 1033123000 | 1 | 1033.12M |
Data Visualization
Let’s begin by designing the theme for our plot! You don’t necessarily have to create a custom theme from scratch; there are pre-built themes available for use in ggplot that are free. However, in this instance, I’ve created a custom theme to ensure that the final content aligns with other Sectors content.