Pada contoh time series plot, saya akan menggunanakan data Closing Price saham yang terdaftar di Bursa Efek Indonesia pada rentang 22 September 2020 hingga 22 September 2023. Adapun list saham yang saya gunakan adalah: MEDC, HRUM dan INDY.
library(readr)
medc=read_csv("C:/Users/cnath/OneDrive/Documents/Kuliah/SEMESTER 7/BUSINESS INTELLIGENCE/MEDC Historical Data.csv",
col_types = cols(Date = col_date(format = "%m/%d/%Y"),
Price = col_number(), Open = col_number(),
High = col_number()))
hrum=read_csv("C:/Users/cnath/OneDrive/Documents/Kuliah/SEMESTER 7/BUSINESS INTELLIGENCE/HRUM Historical Data.csv",
col_types = cols(Date = col_date(format = "%m/%d/%Y"),
Price = col_number(), Open = col_number(),
High = col_number()))
indy=read_csv("C:/Users/cnath/OneDrive/Documents/Kuliah/SEMESTER 7/BUSINESS INTELLIGENCE/INDY Historical Data.csv",
col_types = cols(Date = col_date(format = "%m/%d/%Y"),
Price = col_number(), Open = col_number(),
High = col_number()))
library(ggplot2)
library(ggpmisc)
plot_medc=ggplot(medc, aes(x=Date, y=Price))+
geom_line(color="black", linewidth=0.5)+
xlab("Date")
plot_medc
## Graphic Modification
Berikut merupakan hasil modifikasi grafik hasil plottingan time series saham MEDC dengan tampilan yang lebih menarik.
plot_medc+scale_x_date(date_labels = "%b '%y", date_breaks = "3 months") +
theme_minimal()+
theme(axis.title.x = element_text(angle=0, hjust = 0.5))+
geom_vline(xintercept = as.Date("2021-01-01"),
linetype=2, color=2, linewidth=0.8)+
geom_vline(xintercept = as.Date("2022-01-01"),
linetype=2, color=2, linewidth=0.8)+
geom_vline(xintercept = as.Date("2023-01-01"),
linetype=2, color=2, linewidth=0.8)+
stat_peaks(geom="point", span=20, color="blue3", size=1)+
stat_peaks(geom="label", span=20, color="blue3", angle=30,
hjust=-0.1, x.label.fmt = "%d/%m/%y")+
stat_valleys(geom="point", span=50, color="lightblue4", size=1.5)+
stat_valleys(geom="label", span=50, color="lightblue4", angle=15,
hjust=-0.1, x.label.fmt = "%d/%m/%y")+
labs(x="Date", y="Price (Rp)")+
scale_y_continuous(limits = c(300, 1500), breaks = seq(300, 1500, by=100))+
ggtitle("MEDC Daily Closing Price (22 Sep 2020 - 22 Sep 2023)")+
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
## span increased to next odd value: 21
## span increased to next odd value: 21
## span increased to next odd value: 51
## span increased to next odd value: 51
Berdasarkan time series plot di atas, secara gambaran umum saham MEDC berada pada tren positif selama rentang waktu pengamatan dimana harga saham MEDC mengalami kenaikan yang cukup signifikan dari Januari 2022 hingga Januari 2023 yang kemudian terkoreksi hingga pertengahan tahun 2023. Saham MEDC kemudian berhasil break dari last high sebelumnya akhir-akhir ini dengan harga tertinggi di Rp 1.665. Peluang MEDC untuk terus mempunyai tren harga yang positif cukup besar melihat dari historis harga yang cenderung naik selama periode Oktober-Januari pada tahun-tahun sebelumnya.
plot_hrum=ggplot(hrum, aes(x=Date, y=Price))+
geom_line(color="black", linewidth=0.5)+
xlab("Date")
plot_hrum
Berikut merupakan hasil modifikasi grafik hasil plottingan time series saham HRUM dengan tampilan yang lebih menarik.
plot_hrum+scale_x_date(date_labels = "%b '%y", date_breaks = "3 months") +
theme_minimal()+
theme(axis.title.x = element_text(angle=0, hjust = 0.5))+
geom_vline(xintercept = as.Date("2021-01-01"),
linetype=2, color=2, linewidth=0.8)+
geom_vline(xintercept = as.Date("2022-01-01"),
linetype=2, color=2, linewidth=0.8)+
geom_vline(xintercept = as.Date("2023-01-01"),
linetype=2, color=2, linewidth=0.8)+
stat_peaks(geom="point", span=50, color="orange4", size=1.5)+
stat_peaks(geom="label", span=50, color="orange4", angle=15,
hjust=-0.1, x.label.fmt = "%d/%m/%y")+
stat_valleys(geom="point", span=50, color="orange", size=1.5)+
stat_valleys(geom="label", span=50, color="orange", angle=15,
hjust=-0.1, x.label.fmt = "%d/%m/%y")+
labs(x="Date", y="Price (Rp)")+
scale_y_continuous(limits = c(300, 2800), breaks = seq(300, 2800, by=200))+
ggtitle("HRUM Daily Closing Price (22 Sep 2020 - 22 Sep 2023)")+
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
## span increased to next odd value: 51
## span increased to next odd value: 51
## span increased to next odd value: 51
## span increased to next odd value: 51
Berdasarkan time series plot di atas, secara gambaran umum saham HRUM berada pada tren positif selama rentang waktu pengamatan dimana harga saham HRUM mengalami kenaikan yang cukup signifikan dari Oktober 2020 hingga Februari 2021 karena value perusahaan yang diapresiasi oleh para investor. Harga saham HRUM kemudian mulai terkoreksi hingga Oktober 2023 dan mulai mengalami kenaikan yang fantastis kembali akibat adanya perang geopolitik antara Rusia dan Ukraina yang menyebabkan harga jual batubara melonjak tinggi dengan harga tertinggi di sekitar Rp 2.700. Saham HRUM kemudian mulai memasuki tren penurunan di akhir Q1 2022 hingga Q2 2023 akibat harga batubara yang berangsur normal dan mencari nilai ekulibrium kembali. Dalam 3 bulan terakhir, harga saham HRUM mulai berada pada tren positif kembali.
plot_indy=ggplot(indy, aes(x=Date, y=Price))+
geom_line(color="black", linewidth=0.5)+
xlab("Date")
plot_indy
Berikut merupakan hasil modifikasi grafik hasil plottingan time series saham HRUM dengan tampilan yang lebih menarik.
plot_indy+scale_x_date(date_labels = "%b '%y", date_breaks = "3 months") +
theme_minimal()+
theme(axis.title.x = element_text(angle=0, hjust = 0.5))+
geom_vline(xintercept = as.Date("2021-01-01"),
linetype=2, color=2, linewidth=0.8)+
geom_vline(xintercept = as.Date("2022-01-01"),
linetype=2, color=2, linewidth=0.8)+
geom_vline(xintercept = as.Date("2023-01-01"),
linetype=2, color=2, linewidth=0.8)+
stat_peaks(geom="point", span=50, color="darkgreen", size=1.5)+
stat_peaks(geom="label", span=50, color="darkgreen", angle=15,
hjust=-0.1, x.label.fmt = "%d/%m/%y")+
stat_valleys(geom="point", span=30, color="green2", size=1.5)+
stat_valleys(geom="label", span=30, color="green2", angle=15,
hjust=-0.1, x.label.fmt = "%d/%m/%y")+
labs(x="Date", y="Price (Rp)")+
scale_y_continuous(limits = c(900, 3400), breaks = seq(900, 3400, by=200))+
ggtitle("INDY Daily Closing Price (22 Sep 2020 - 22 Sep 2023)")+
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
## span increased to next odd value: 51
## span increased to next odd value: 51
## span increased to next odd value: 31
## span increased to next odd value: 31
Berdasarkan time series plot di atas, secara gambaran umum saham INDY berada pada tren positif selama rentang waktu pengamatan dan memiliki fluktuasi harga yang cukup tiinggi. Dapat diamati pula pola harga saham INDY cukup konsisten berada pada tren positif selama periode Q3 hingga Q4 tiap tahunnya dengan harga tertinggi di sekitar harga Rp 3.300 dan mulai terkoreksi dari akhir Q4 hingga akhir Q2. Berdasarkan historis harga yang terbentuk, maka dapat diasumsikan periode yang tepat untuk mencicil saham INDY adalah mulai dari pertengahan Q2 hingga akhir Q2 dan melakukan penjualan dalam rentang Q3-Q4.
Pada multiple time series plot, saya memvisualisasikan data saham MEDC, HRUM dan INDY dalam satu grafik untuk melihat bagaimana tren dari masing-masing saham.
MHI=data.frame(Date = medc$Date,
MEDC = medc$Price,
HRUM = hrum$Price,
INDY = indy$Price)
str(MHI)
## 'data.frame': 731 obs. of 4 variables:
## $ Date: Date, format: "2023-09-22" "2023-09-21" ...
## $ MEDC: num 1600 1515 1570 1665 1605 ...
## $ HRUM: num 1820 1790 1810 1845 1780 ...
## $ INDY: num 2250 2200 2240 2250 2050 2040 2100 2010 2000 2020 ...
library(ggtext)
MEDC_start=subset(medc, Date == as.Date("2020-09-22"))$Price
MEDC_last=subset(medc, Date == as.Date("2023-09-22"))$Price
HRUM_start=subset(hrum, Date == as.Date("2020-09-22"))$Price
HRUM_last=subset(hrum, Date == as.Date("2023-09-22"))$Price
INDY_start=subset(indy, Date == as.Date("2020-09-22"))$Price
INDY_last=subset(indy, Date == as.Date("2023-09-22"))$Price
plot_MHI=ggplot(MHI, aes(x=Date))+
geom_line(aes(y=MEDC, color="MEDC"), linewidth=0.8)+
geom_line(aes(y=HRUM, color="HRUM"), linewidth=0.8)+
geom_line(aes(y=INDY, color="INDY"), linewidth=0.8)+
scale_color_manual(values = c(MEDC="blue3", HRUM="orange4", INDY="darkgreen"))+
labs(
x = "Date",
y = "Price (Rp)",
title = "Daily Closing Price MEDC, HRUM, and INDY",
color = "",
size=5
) +
scale_x_date(date_labels = "%b '%y", date_breaks = "3 months")+
theme_minimal()+
geom_vline(xintercept = as.Date("2021-01-01"),
linetype=2, color=2, linewidth=0.8)+
geom_vline(xintercept = as.Date("2022-01-01"),
linetype=2, color=2, linewidth=0.8)+
geom_vline(xintercept = as.Date("2023-01-01"),
linetype=2, color=2, linewidth=0.8)+
geom_point(aes(x=as.Date("2020-09-22"), y=MEDC_start), color="blue3", size=2)+
geom_text(aes(x=as.Date("2020-09-22"), y=MEDC_start,
label=paste(MEDC_start)), hjust=0.3, vjust=-0.6, size=4)+
geom_point(aes(x=as.Date("2023-09-22"), y=MEDC_last), color="blue3", size=2)+
geom_text(aes(x=as.Date("2023-09-22"), y=MEDC_last,
label=paste(MEDC_last)), hjust=-0.2, vjust=-0.4, size=4)+
geom_point(aes(x=as.Date("2020-09-22"), y=HRUM_start), color="orange4", size=2)+
geom_text(aes(x=as.Date("2020-09-22"), y=HRUM_start,
label=paste(HRUM_start)), hjust=0.3, vjust=1.3, size=4)+
geom_point(aes(x=as.Date("2023-09-22"), y=HRUM_last), color="orange4", size=2)+
geom_text(aes(x=as.Date("2023-09-22"), y=HRUM_last,
label=paste(HRUM_last)), hjust=-0.2, vjust=-0.4, size=4)+
geom_point(aes(x=as.Date("2020-09-22"), y=INDY_start), color="darkgreen", size=2)+
geom_text(aes(x=as.Date("2020-09-22"), y=INDY_start,
label=paste(INDY_start)), hjust=0.3, vjust=-0.8, size=4)+
geom_point(aes(x=as.Date("2023-09-22"), y=INDY_last), color="darkgreen", size=2)+
geom_text(aes(x=as.Date("2023-09-22"), y=INDY_last,
label=paste(INDY_last)), hjust=-0.2, vjust=-0.4, size=4)+
scale_y_continuous(limits = c(300, 3400), breaks = seq(300, 3400, by=300))+
theme(axis.title.x = element_text(angle=0, hjust = 0.5))+
theme(plot.title = element_text(hjust = 0.5, face = "bold"))+
theme(legend.position = "bottom" , legend.text = element_text(size=12))
plot_MHI
Ketiga saham yang saya pilih sebagai sample pada time series plot ini merupakan saham yang berada pada sektor industry yang sama yaitu sektor energi meskipun lini bisnis utamanya atau komoditas utama yang dijual berbeda. Namun, dari plot time series selama periode 22-09-2020 hingga 22-09-2023 dapat dilihat bahwa ketiga saham tersebut memiliki pola fluktuasi harga yang cenderung mirip. Hal tersebut dapat dikaitkan dengan naik-turunnya harga komoditas yang biasanya saling berkaitan satu sama lain sehingga mendorong pergerakan pola harga saham yang cenderung serupa pula.
Demikian merupakan contoh dari single time series plot dan multiple time series plot pada R Studio.