library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
#========== TUGAS 3 ==========#

#---------- Data Saham BBNI ----------#

# Import Data
bni <- read.csv("C:/Users/AZ ZAHRA AULINA/OneDrive/文件/KULIAH/SEMESTER 6/BINTEL/TUGAS/DATA/Data Tugas 2/Data Historis BBNI.csv")

# Ubah nama kolom pertama menjadi Tanggal
names(bni)[1] <- "Tanggal"

# Merubah tipe data variabel date dan membuat data frame baru berisi time dan closed price
# CARA 1 PAKE as.Date
databni1 <- data.frame(Time = as.Date(bni$Tanggal, format = "%d/%m/%Y"),Price = bni$Terakhir)

str(databni1)
## 'data.frame':    709 obs. of  2 variables:
##  $ Time : Date, format: "2026-03-16" "2026-03-13" ...
##  $ Price: num  4.34 4.24 4.28 4.29 4.26 4.29 4.27 4.28 4.15 4.3 ...
# CARA 1 PAKE LUBRIDATE
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
databni2 <- data.frame(Time= dmy(bni$Tanggal), Price=bni$Terakhir)
str(databni2)
## 'data.frame':    709 obs. of  2 variables:
##  $ Time : Date, format: "2026-03-16" "2026-03-13" ...
##  $ Price: num  4.34 4.24 4.28 4.29 4.26 4.29 4.27 4.28 4.15 4.3 ...
# PLOT TIME SERIES
# Membuat Plot Time Series dengan GGPLOT2

library(ggplot2)
library(ggpmisc)
## Loading required package: ggpp
## Registered S3 methods overwritten by 'ggpp':
##   method                  from   
##   heightDetails.titleGrob ggplot2
##   widthDetails.titleGrob  ggplot2
## 
## Attaching package: 'ggpp'
## The following object is masked from 'package:ggplot2':
## 
##     annotate
p <- ggplot(databni1, aes(x=Time, y=Price)) + # INI BISA PAKE databni1 atau databni2, sama aja. cuma beda metode ajah
  geom_line()+
  xlab("Date")+
  labs(title = "BBNI Stock Price", x = "Date", y = "Price")
p 

# Modification
# PAKE PEAKS
p+
  scale_x_date(date_labels = "%b' %Y", date_breaks = "2 months" )+ # scale itu buat x nya per 2 bulan
  theme_minimal()+ # ini buat rapihin plotnya
  theme(axis.text.x=element_text(angle=50, hjust=1))+ # ini buat tulisan di x nya jadi miring
  geom_vline(xintercept = as.Date("2023-04-26"), linetype = 2, color = 2, linewidth = 1) + # ini buat nambahin garis putus" di plot
  stat_peaks(geom = "point", span = 15, color="green4", size =2)+ # ini buat kasih titik" di nilai max
  stat_peaks(geom = "label", span = 15, color = "pink1", angle = 0,
             hjust = -0.1, x.label.fmt = "%d/%m/%y")+ # ini buat kasih keterangan tanggal di nilai max
  stat_peaks(geom = "rug", span = 15, color = "tomato4", sides = "b") # ini buat kasih titik" di sumbu x untuk nilai max  nya

# PAKE VALLEY
p+
  scale_x_date(date_labels = "%b' %Y", date_breaks = "2 months" )+ # scale itu buat x nya per 2 bulan
  theme_minimal()+ # ini buat rapihin plotnya
  theme(axis.text.x=element_text(angle=50, hjust=1))+ # ini buat tulisan di x nya jadi miring
  geom_vline(xintercept = as.Date("2023-04-26"), linetype = 2, color = 2, linewidth = 1) + # ini buat nambahin garis putus" di plot
  stat_valleys(geom = "point", span = 11, color="green", size =2)+ # ini buat kasih titik" di nilai min
  stat_valleys(geom = "label", span = 11, color = "blue", angle = 0,
               hjust = -0.1, x.label.fmt = "%d/%m/%y")+ # ini buat kasih keterangan tanggal di nilai min
  stat_valleys(geom = "rug", span = 11, color = "red", sides = "b") # ini buat kasih titik" di sumbu x untuk nilai min  nya

#---------- Data Saham BBCA ----------#

# Import Data
bca <- read.csv("C:/Users/AZ ZAHRA AULINA/OneDrive/文件/KULIAH/SEMESTER 6/BINTEL/TUGAS/DATA/Data Tugas 2/Data Historis BBCA.csv")

# Ubah nama kolom pertama menjadi Tanggal
names(bca)[1] <- "Tanggal"

# Merubah tipe data variabel date dan membuat data frame baru berisi time dan closed price
# CARA 1 PAKE as.Date
databca1 <- data.frame(
  Time = as.Date(bca$Tanggal, format = "%d/%m/%Y"),
  Price = bca$Terakhir
)

str(databca1)
## 'data.frame':    709 obs. of  2 variables:
##  $ Time : Date, format: "2026-03-16" "2026-03-13" ...
##  $ Price: num  6.75 6.88 6.9 6.83 6.97 ...
# CARA 1 PAKE LUBRIDATE
library(lubridate)
databca2 <- data.frame(Time= dmy(bca$Tanggal), Price=bca$Terakhir)
str(databca2)
## 'data.frame':    709 obs. of  2 variables:
##  $ Time : Date, format: "2026-03-16" "2026-03-13" ...
##  $ Price: num  6.75 6.88 6.9 6.83 6.97 ...
# PLOT TIME SERIES
# Membuat Plot Time Series dengan GGPLOT2

library(ggplot2)
library(ggpmisc)
p <- ggplot(databca1, aes(x=Time, y=Price)) + # INI BISA PAKE databni1 atau databni2, sama aja. cuma beda metode ajah
  geom_line()+
  xlab("Date")+
  labs(title = "BBNI Stock Price", x = "Date", y = "Price")
p 

# Modification
# PAKE PEAKS
p+
  scale_x_date(date_labels = "%b' %Y", date_breaks = "2 months" )+ # scale itu buat x nya per 2 bulan
  theme_minimal()+ # ini buat rapihin plotnya
  theme(axis.text.x=element_text(angle=50, hjust=1))+ # ini buat tulisan di x nya jadi miring
  geom_vline(xintercept = as.Date("2023-04-26"), linetype = 2, color = 2, linewidth = 1) + # ini buat nambahin garis putus" di plot
  stat_peaks(geom = "point", span = 15, color="green4", size =2)+ # ini buat kasih titik" di nilai max
  stat_peaks(geom = "label", span = 15, color = "pink1", angle = 0,
             hjust = -0.1, x.label.fmt = "%d/%m/%y")+ # ini buat kasih keterangan tanggal di nilai max
  stat_peaks(geom = "rug", span = 15, color = "tomato4", sides = "b")+ # ini buat kasih titik" di sumbu x untuk nilai max  nya
  scale_x_date(date_labels = "%b' %Y", date_breaks = "2 months" )+ # scale itu buat x nya per 2 bulan
  theme_minimal()+ # ini buat rapihin plotnya
  theme(axis.text.x=element_text(angle=50, hjust=1))+ # ini buat tulisan di x nya jadi miring
  geom_vline(xintercept = as.Date("2023-04-26"), linetype = 2, color = 2, linewidth = 1) + # ini buat nambahin garis putus" di plot
  stat_valleys(geom = "point", span = 11, color="green", size =2)+ # ini buat kasih titik" di nilai min
  stat_valleys(geom = "label", span = 11, color = "blue", angle = 0,
               hjust = -0.1, x.label.fmt = "%d/%m/%y")+ # ini buat kasih keterangan tanggal di nilai min
  stat_valleys(geom = "rug", span = 11, color = "red", sides = "b") # ini buat kasih titik" di sumbu x untuk nilai min  nya
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.

#---------- Data Saham ANTAM ----------#

# Import Data
antam <- read.csv("C:/Users/AZ ZAHRA AULINA/OneDrive/文件/KULIAH/SEMESTER 6/BINTEL/TUGAS/DATA/Data Tugas 2/Data Historis ANTM.csv")

# Ubah nama kolom pertama menjadi Tanggal
names(antam)[1] <- "Tanggal"

# Merubah tipe data variabel date dan membuat data frame baru berisi time dan closed price
# CARA 1 PAKE as.Date
dataantam1 <- data.frame(
  Time = as.Date(antam$Tanggal, format = "%d/%m/%Y"),
  Price = antam$Terakhir)
str(dataantam1)
## 'data.frame':    709 obs. of  2 variables:
##  $ Time : Date, format: "2026-03-16" "2026-03-13" ...
##  $ Price: num  3.64 3.81 3.89 3.97 4.02 3.82 4.04 4.13 4.05 4.41 ...
# CARA 1 PAKE LUBRIDATE
library(lubridate)
dataantam2 <- data.frame(Time= dmy(antam$Tanggal), Price=antam$Terakhir)
str(dataantam2)
## 'data.frame':    709 obs. of  2 variables:
##  $ Time : Date, format: "2026-03-16" "2026-03-13" ...
##  $ Price: num  3.64 3.81 3.89 3.97 4.02 3.82 4.04 4.13 4.05 4.41 ...
# PLOT TIME SERIES
# Membuat Plot Time Series dengan GGPLOT2

library(ggplot2)
library(ggpmisc)
p <- ggplot(dataantam1, aes(x=Time, y=Price)) + # INI BISA PAKE databni1 atau databni2, sama aja. cuma beda metode ajah
  geom_line()+
  xlab("Date")+
  labs(title = "BBNI Stock Price", x = "Date", y = "Price")
p 

# Modification
# PAKE PEAKS
p+
  scale_x_date(date_labels = "%b' %Y", date_breaks = "2 months" )+ # scale itu buat x nya per 2 bulan
  theme_minimal()+ # ini buat rapihin plotnya
  theme(axis.text.x=element_text(angle=50, hjust=1))+ # ini buat tulisan di x nya jadi miring
  geom_vline(xintercept = as.Date("2023-04-26"), linetype = 2, color = 2, linewidth = 1) + # ini buat nambahin garis putus" di plot
  stat_peaks(geom = "point", span = 15, color="green4", size =2)+ # ini buat kasih titik" di nilai max
  stat_peaks(geom = "label", span = 15, color = "pink1", angle = 0,
             hjust = -0.1, x.label.fmt = "%d/%m/%y")+ # ini buat kasih keterangan tanggal di nilai max
  stat_peaks(geom = "rug", span = 15, color = "tomato4", sides = "b")# ini buat kasih titik" di sumbu x untuk nilai max  nya

# PAKE VALLEY
p+
  scale_x_date(date_labels = "%b' %Y", date_breaks = "2 months" )+ # scale itu buat x nya per 2 bulan
  theme_minimal()+ # ini buat rapihin plotnya
  theme(axis.text.x=element_text(angle=50, hjust=1))+ # ini buat tulisan di x nya jadi miring
  geom_vline(xintercept = as.Date("2023-04-26"), linetype = 2, color = 2, linewidth = 1) + # ini buat nambahin garis putus" di plot
  stat_valleys(geom = "point", span = 11, color="green", size =2)+ # ini buat kasih titik" di nilai min
  stat_valleys(geom = "label", span = 11, color = "blue", angle = 0,
               hjust = -0.1, x.label.fmt = "%d/%m/%y")+ # ini buat kasih keterangan tanggal di nilai min
  stat_valleys(geom = "rug", span = 11, color = "red", sides = "b") # ini buat kasih titik" di sumbu x untuk nilai min  nya

# ========== MULTIPLE TIME SERIES ========== #
# === MULTIPLE DATA SERIES === # 
df_bbni <- data.frame(
  Time = as.Date(databni1$Time, format = '%d/%m/%Y'), 
  Price = databni1$Price,
  Keterangan = "BBNI")

df_bbca <- data.frame(
  Time = as.Date(databca1$Time, format = '%d/%m/%Y'), 
  Price = databca1$Price,
  Keterangan = "BBCA")

df_antam <- data.frame(
  Time = as.Date(dataantam1$Time, format = '%d/%m/%Y'), 
  Price = dataantam1$Price,
  Keterangan = "ANTAM")

data_final <- rbind(df_bbni, df_bbca, df_antam)
str(data_final)
## 'data.frame':    2127 obs. of  3 variables:
##  $ Time      : Date, format: "2026-03-16" "2026-03-13" ...
##  $ Price     : num  4.34 4.24 4.28 4.29 4.26 4.29 4.27 4.28 4.15 4.3 ...
##  $ Keterangan: chr  "BBNI" "BBNI" "BBNI" "BBNI" ...
a <- ggplot(data_final, aes(x = Time, y = Price, color = Keterangan)) +
  geom_line(aes()) +
  xlab("Date") +
  ylab("Harga Penutupan Saham") +
  labs(title = "Harga Saham BBNI, BBCA, dan ANTAM") +
  theme_minimal()

a

a+
  scale_x_date(date_labels = "%b %Y", date_breaks = "2 months" )+
  theme_minimal()+
  theme(axis.text.x=element_text(angle=50, hjust=1))+
  geom_vline(xintercept =
               as.Date("2023-03-16"),
             linetype = 2, color = "tomato4",
             linewidth = 0.75)+
  geom_vline(xintercept =
               as.Date("2024-03-16"),
             linetype = 2, color = "tomato4",
             linewidth = 0.75)+
  geom_vline(xintercept =
               as.Date("2025-03-16"),
             linetype = 2, color = "tomato4",
             linewidth = 0.75)+
  geom_vline(xintercept =
               as.Date("2026-03-16"),
             linetype = 2, color = "tomato4",
             linewidth = 0.75) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  theme(legend.position = "top")

# BOX PLOT
ggplot(data_final, aes(x = Time, y = Price, fill = Keterangan)) +
  geom_boxplot() +
  theme_minimal() +
  labs(title = "Boxplot Stock Prices")
## Warning: Orientation is not uniquely specified when both the x and y aesthetics are
## continuous. Picking default orientation 'x'.

Berdasarkan visualisasi diatas digunakan data ketiga saham dalam periode 3 tahun terakhir, dapat disimpulkan bahwa saham BBCA merupakan saham dengan harga tertinggi dengan pergerakan paling stabil. Saham BBNI menunjukkan fluktuasi yang moedrat dengan tren relatif stabil pada akhir periode. Sedangkan, saham ANTM mengalami kenaikan yang paling siginifikan dan memiliki volatilitas yang tinggi. Dimana saham ANTM memiliki potensi pertumbuhan yang besar, sedangkan sahan BBCA lebih cocok untuk investor yang mengutamakan kestabilan