1. Import Dataset

Menggunakan library readxl untuk membaca data harga penutupan harian 3 saham dari file Excel.

library(readxl)

Data_Saham <- read_xlsx("D:/S. Aktr/SEMESTER 6/Business Intelligence/Data Harga Penutupan Harian 3 Saham.xlsx", 
                        sheet = "Gabungan")

str(Data_Saham)
## tibble [709 × 4] (S3: tbl_df/tbl/data.frame)
##  $ Date: chr [1:709] "02/27/2026" "02/26/2026" "02/25/2026" "02/24/2026" ...
##  $ JPFA: num [1:709] 2380 2350 2410 2420 2420 2380 2480 2560 2560 2550 ...
##  $ CPIN: num [1:709] 4250 4410 4340 4360 4430 4330 4270 4270 4300 4410 ...
##  $ MAIN: num [1:709] 790 780 790 770 790 780 785 775 755 760 ...

2. Mengubah Tipe Data Variabel Date

Variabel Date diubah menjadi tipe data Date dengan format %m/%d/%Y.

Data_Saham$Date <- as.Date(Data_Saham$Date, format = "%m/%d/%Y")

str(Data_Saham)
## tibble [709 × 4] (S3: tbl_df/tbl/data.frame)
##  $ Date: Date[1:709], format: "2026-02-27" "2026-02-26" ...
##  $ JPFA: num [1:709] 2380 2350 2410 2420 2420 2380 2480 2560 2560 2550 ...
##  $ CPIN: num [1:709] 4250 4410 4340 4360 4430 4330 4270 4270 4300 4410 ...
##  $ MAIN: num [1:709] 790 780 790 770 790 780 785 775 755 760 ...

3. Plot Time Series dengan ggplot2

Membuat visualisasi time series untuk masing-masing saham menggunakan ggplot2.

library(ggplot2)

Saham JPFA

ggplot(Data_Saham, aes(x = Date, y = JPFA)) +
  geom_line(color = "Red") +
  ylab("Price") +
  labs(title = "Harga Penutupan Saham JPFA") +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5))

Saham CPIN

ggplot(Data_Saham, aes(x = Date, y = CPIN)) +
  geom_line(color = "Blue") +
  ylab("Price") +
  labs(title = "Harga Penutupan Saham CPIN") +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5))

Saham MAIN

ggplot(Data_Saham, aes(x = Date, y = MAIN)) +
  geom_line(color = "Green") +
  ylab("Price") +
  labs(title = "Harga Penutupan Saham MAIN") +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5))


4. Plot Single Data Series

Membuat objek time series untuk masing-masing saham secara individual menggunakan fungsi ts().

Saham JPFA

JPFA.timeseries <- ts(rev(Data_Saham$JPFA), start = c(2023, 1), frequency = 252)

plot(JPFA.timeseries,
     main = "Single Data Series (JPFA)",
     ylab = "Price",
     col  = "red")

Saham CPIN

CPIN.timeseries <- ts(rev(Data_Saham$CPIN), start = c(2023, 1), frequency = 252)

plot(CPIN.timeseries,
     main = "Single Data Series (CPIN)",
     ylab = "Price",
     col  = "blue")

Saham MAIN

MAIN.timeseries <- ts(rev(Data_Saham$MAIN), start = c(2023, 1), frequency = 252)

plot(MAIN.timeseries,
     main = "Single Data Series (MAIN)",
     ylab = "Price",
     col  = "green")


5. Plot Multiple Data Series

Menggabungkan ketiga saham ke dalam satu objek time series dan menampilkannya dalam satu plot.

combined.saham <- cbind(rev(Data_Saham$JPFA),
                        rev(Data_Saham$CPIN),
                        rev(Data_Saham$MAIN))
colnames(combined.saham) <- c("JPFA", "CPIN", "MAIN")

combined.timeseries <- ts(combined.saham, start = c(2023, 1), frequency = 252)

plot(combined.timeseries,
     main = "Multiple Data Series")

Multiple Data Series dengan Pembatas Tahunan

Menambahkan garis vertikal sebagai pembatas antar tahun (2024 dan 2025).

plot(combined.timeseries,
     main = "Multiple Data Series")
abline(v = 2024, col = "blue", lty = 1)
abline(v = 2025, col = "blue", lty = 1)


6. Eksplorasi Data dengan Boxplot

Membuat boxplot untuk membandingkan distribusi harga ketiga saham secara bersamaan.

boxplot(combined.saham,
        main   = "Boxplot Harga Saham Gabungan",
        ylab   = "Price",
        col    = c("red", "blue", "green"),
        border = "black")