Most Traded Stocks on Indonesia Stock Exchange in 2024
By: Gerald Bryan · April 23, 2024
Have you ever thought about creating an animated plot that evolves over time, perfect for sharing on Instagram Reels or other content platforms? Well, now you can achieve that using R and data from the Sectors. In this recipe, I’ll guide you to create the animated plot of the most traded stocks on Indonesia Stock Exchange in 2024.
Install Libraries
Before we dive into the project, let’s set up our environment by installing essential packages. We’ll use the tidyverse
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.
Using the most-traded API endpoints we could fetch the top-n stocks based on the trading volume each day, we could configure the start date, the end date, and the top-n most traded companies in each day. After that we only need to process the data to make it a proper data frame that can be used in our next step.
Here is the glimpse result of the API endpoint that we have called and processed. We will use this data to create a animation bar chart in this recipe
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 |
You can also use this dataset for this task, it is the same with the data that we called from the API, and it’s accessible on Sectors.
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.
With the provided code, the data will be transformed as shown in the table below. This table will include the ranking and cumulative sum up until the most recent date, which is essential for creating the plot.
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 |
To finalize the data preparation for the plot, we will format the accumulated volume to enhance readability. This formatting will make the numbers easier for people to interpret.
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.
After creating the theme, we can directly plot the data into static plot first before we animate it into an animated plot.
After finishing create the static plot, we can directly convert it into a gif by using this code:
and here is the result of the gif:
In our latest experiment, we dove into the world of animated plots using data from Sectors with R. Stay tuned for more exciting recipes featuring Sectors data!