installed.packages("quantmod")
## Package LibPath Version Priority Depends Imports LinkingTo Suggests
## Enhances License License_is_FOSS License_restricts_use OS_type Archs
## MD5sum NeedsCompilation Built
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
symbols <- c("DIS","WBD","CMCSA")
data <- do.call(merge, lapply(symbols, function(s){
Cl(getSymbols(s, src="yahoo",
from="2023-01-01",
to="2025-12-31",
auto.assign=FALSE))
}))
colnames(data) <- symbols
head(data)
## DIS WBD CMCSA
## 2023-01-03 88.97 9.54 33.33646
## 2023-01-04 91.98 10.38 34.29241
## 2023-01-05 91.92 10.86 34.65792
## 2023-01-06 93.92 11.32 35.51078
## 2023-01-09 94.77 11.61 35.19213
## 2023-01-10 95.56 12.56 35.50141
library(ggplot2)
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.5.3
library(dplyr)
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
dis <- data.frame(
Time = as.Date(index(data)),
Price = coredata(data)[,"DIS"])
head(dis)
## Time Price
## 1 2023-01-03 88.97
## 2 2023-01-04 91.98
## 3 2023-01-05 91.92
## 4 2023-01-06 93.92
## 5 2023-01-09 94.77
## 6 2023-01-10 95.56
str(dis)
## 'data.frame': 751 obs. of 2 variables:
## $ Time : Date, format: "2023-01-03" "2023-01-04" ...
## $ Price: num 89 92 91.9 93.9 94.8 ...
# plot time series
d1 <- ggplot(dis, aes(x=Time, y=Price))+
geom_line()+
xlab("Date")
d1
wbd <- data.frame(
Time = as.Date(index(data)),
Price = coredata(data)[,"WBD"])
head(wbd)
## Time Price
## 1 2023-01-03 9.54
## 2 2023-01-04 10.38
## 3 2023-01-05 10.86
## 4 2023-01-06 11.32
## 5 2023-01-09 11.61
## 6 2023-01-10 12.56
str(wbd)
## 'data.frame': 751 obs. of 2 variables:
## $ Time : Date, format: "2023-01-03" "2023-01-04" ...
## $ Price: num 9.54 10.38 10.86 11.32 11.61 ...
# plot time series
d2 <- ggplot(wbd, aes(x=Time, y=Price))+
geom_line()+
xlab("Date")
d2
cmcsa <- data.frame(
Time = as.Date(index(data)),
Price = coredata(data)[,"CMCSA"])
head(cmcsa)
## Time Price
## 1 2023-01-03 33.33646
## 2 2023-01-04 34.29241
## 3 2023-01-05 34.65792
## 4 2023-01-06 35.51078
## 5 2023-01-09 35.19213
## 6 2023-01-10 35.50141
str(cmcsa)
## 'data.frame': 751 obs. of 2 variables:
## $ Time : Date, format: "2023-01-03" "2023-01-04" ...
## $ Price: num 33.3 34.3 34.7 35.5 35.2 ...
# plot time series
d3 <- ggplot(cmcsa, aes(x=Time, y=Price))+
geom_line()+
xlab("Date")
d3
multi <- data.frame(
Time = as.Date(index(data)),
coredata(data)
)
d4 <- pivot_longer(
multi,
cols = -Time,
names_to = "Stock",
values_to = "Price"
)
head(d4)
## # A tibble: 6 × 3
## Time Stock Price
## <date> <chr> <dbl>
## 1 2023-01-03 DIS 89.0
## 2 2023-01-03 WBD 9.54
## 3 2023-01-03 CMCSA 33.3
## 4 2023-01-04 DIS 92.0
## 5 2023-01-04 WBD 10.4
## 6 2023-01-04 CMCSA 34.3
ggplot(d4, aes(x = Time, y = Price, color = Stock)) +
geom_line(size = 1) +
theme_minimal() +
theme(legend.position = "bottom") +
labs(
title = "Stock Price Comparison",
x = "Date",
y = "Close Price"
)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
d1 <- ggplot(dis, aes(x=Time, y=Price))+
geom_line()+
geom_vline(
xintercept = as.Date(c("2024-01-01","2025-01-01")),
linetype = "dashed"
)+
xlab("Date")+
ylab("Close Price")+
ggtitle("DIS Stock Price")
d1
d2 <- ggplot(wbd, aes(x=Time, y=Price))+
geom_line()+
geom_vline(
xintercept = as.Date(c("2024-01-01","2025-01-01")),
linetype = "dashed"
)+
xlab("Date")+
ylab("Close Price")+
ggtitle("WBD Stock Price")
d2
d3 <- ggplot(cmcsa, aes(x=Time, y=Price))+
geom_line()+
geom_vline(
xintercept = as.Date(c("2024-01-01","2025-01-01")),
linetype = "dashed"
)+
xlab("Date")+
ylab("Close Price")+
ggtitle("CMCSA Stock Price")
d3
ggplot(d4, aes(x = Time, y = Price, color = Stock)) +
geom_line(size = 1) +
geom_vline(
xintercept = as.Date(c("2024-01-01","2025-01-01")),
linetype = "dashed"
) +
theme_minimal() +
theme(legend.position = "bottom") +
labs(
title = "Stock Price with Year Separation",
x = "Date",
y = "Close Price"
)
## Eksplorasi Data - Boxplot
ggplot(d4, aes(x = Stock, y = Price, fill = Stock)) +
geom_boxplot() +
theme_minimal() +
labs(
title = "Distribution of Stock Prices",
x = "Stock",
y = "Close Price"
)
## Kesimpulan Berdasarkan visualisasi data time series harga
saham selama periode 2023-2025, terlihat bahwa ketiga
perusahaan memiliki pola pergerakan yang berbeda. Saham The Walt
Disney Company (DIS) memiliki harga yang paling tinggi
dibandingkan dua perusahaan lainnya dan menunjukkan fluktuasi
yang cukup besar sepanjang periode pengamatan. Hal ini terlihat
dari rentang harga yang lebih lebar pada grafik
time series maupun boxplot.
Saham Comcast Corporation (CMCSA) berada pada tingkat harga menengah dengan pergerakan yang relatif lebih stabil dibandingkan DIS. Variasi harga yang tidak terlalu besar pada boxplot menunjukkan bahwa perubahan harga saham CMCSA cenderung lebih konsisten selama tiga tahun tersebut.
Sementara itu, saham Warner Bros. Discovery (WBD) memiliki harga paling rendah dibandingkan kedua perusahaan lainnya. Namun pada bagian akhir periode terlihat adanya kenaikan harga yang cukup signifikan. Hal ini juga terlihat pada boxplot dengan munculnya beberapa nilai yang lebih tinggi dari mayoritas data, yang menunjukkan adanya peningkatan harga pada akhir periode pengamatan.
Secara keseluruhan, visualisasi ini menunjukkan bahwa DIS memiliki volatilitas harga yang lebih tinggi, CMCSA relatif lebih stabil, dan WBD menunjukkan peningkatan harga pada akhir periode.