Scatter lot & Timeseries
ggplot(time_series_data, aes(x = date, y = value)) +
geom_point() +
labs(title = "Scatter Plot of Time Series Data",
x = "Date",
y = "Value")

ggplot(time_series_data, aes(x = date, y = value)) +
geom_point() + # Add points
geom_line() + # Connect points with lines
labs(title = "Scatter Plot with Connected Lines",
x = "Date",
y = "Value")

ggplot(time_series_data, aes(x = date, y = value)) +
geom_line() +
labs(title = "Random Walk Time Series Plot",
x = "Date",
y = "Value")

calculate_moving_average <- function(data, window_size) {
ma_values <- zoo::rollmean(data$value, k = window_size, align = "right", fill = NA)
return(ma_values)
}
window_size <- 10
time_series_data$ma <- calculate_moving_average(time_series_data, window_size)
# Create the plot
ggplot(time_series_data, aes(x = date)) +
geom_line(aes(y = value), color = "white", size = 1) +
geom_line(aes(y = ma), color = "red", linetype = "dashed", size = 1) + # emulusan
geom_ribbon(aes(ymin = -Inf, ymax = ma), fill = "red", alpha = 0.2) + # Area under curve
labs(title = paste("Time Series Data with Moving Average (Window Size:", window_size, ")"),
x = "Date",
y = "Value") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 9 rows containing missing values or values outside the scale range
## (`geom_line()`).

dataset <- read_excel("/Users/user/Downloads/DATA KURS FIX MPDW.xlsx")
str(dataset)
## tibble [206 × 1] (S3: tbl_df/tbl/data.frame)
## $ GBP: num [1:206] 20177 20118 20183 20208 20240 ...
data <- dataset$GBP
data.ts <- ts(data)
plot(data.ts, xlab ="Waktu", ylab = "Data Kurs (Ribu)", col="red", main = "Plot Data Kurs Jual")
points(data.ts)

data.train <- ts(data[1:145])
data.test <- ts(data[146:206], start = 146)
#Time Series Data
training.ts<-ts(data.train)
testing.ts<-ts(data.test)
ts.plot(data.ts, xlab = "Periode", ylab ="Data Inflasi (Persen)",
main = "Plot Data Training dan Data Testing")
lines(data.train, col = "blue")
lines(data.test, col="Red")
legend(-0.9,22100,c("Data Training","Data Testing"),
lty=8, col=c("blue","red"), cex=0.8)
abline(v=146, col=c("black"), lty=1, lwd=1)

Waktu Jamak
set.seed(123)
dates <- seq(as.Date("2020-01-01"), by = "month", length.out = 12)
values_location1 <- cumsum(rnorm(12, mean = 5, sd = 2)) + 100
values_location2 <- cumsum(rnorm(12, mean = 3, sd = 1)) + 80
time_series_data <- data.frame(date = rep(dates, 2),
value = c(values_location1, values_location2),
location = rep(c("Location 1", "Location 2"), each = 12))
last_values <- time_series_data %>%
group_by(location) %>%
summarise(last_value = last(value))
ggplot(time_series_data, aes(x = date, y = value, color = location, linetype = location)) +
geom_line() +
geom_text(data = last_values, aes(label = location),
x = max(time_series_data$date),
y = last_values$last_value,
hjust = -0.1, vjust = -0.5, size = 3, color = "black") +
labs(title = "Time Series Data for Two Locations/Scenarios",
x = "Date",
y = "Value") +
scale_color_manual(values = c("Location 1" = "blue", "Location 2" = "red")) +
scale_linetype_manual(values = c("Location 1" = "solid", "Location 2" = "dashed")) +
theme_minimal()

Dua Peubah
set.seed(123)
dates <- seq(as.Date("2020-01-01"), by = "month", length.out = 12)
values_location1 <- cumsum(rnorm(12, mean = 5, sd = 2)) + 100
values_location2 <- cumsum(rnorm(12, mean = 3, sd = 1)) + 80
time_series_data_location1 <- data.frame(date = dates, value = values_location1)
time_series_data_location2 <- data.frame(date = dates, value = values_location2)
# Visual
ggplot() +
geom_line(data = time_series_data_location1, aes(x = date, y = value, color = "Location 1")) +
geom_line(data = time_series_data_location2, aes(x = date, y = value * 2, color = "Location 2")) +
scale_color_manual(values = c("blue", "red")) +
labs(title = "Time Series Data for Two Locations/Scenarios",
x = "Date",
y = "Location 1 Value") +
theme_minimal() +
theme(axis.title.y = element_text(color = "blue"),
axis.title.y.right = element_text(color = "red"),
axis.text.y.right = element_text(color = "red")) + scale_y_continuous(sec.axis = sec_axis(~./2, name = "Location 2 Value")) + labs(color = "Locations")
