VISUALISASI ANTAR PEUBAH NUMERIK
Packages
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'tidyr' was built under R version 4.3.2
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'dplyr' was built under R version 4.3.3
## Warning: package 'stringr' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(reshape2)
##
## Attaching package: 'reshape2'
##
## The following object is masked from 'package:tidyr':
##
## smiths
library(ggcorrplot)
## Warning: package 'ggcorrplot' was built under R version 4.3.3
library(ggplot2)
library(sf)
## Warning: package 'sf' was built under R version 4.3.3
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.2
library(rnaturalearth)
## Warning: package 'rnaturalearth' was built under R version 4.3.3
library(rnaturalearthdata)
## Warning: package 'rnaturalearthdata' was built under R version 4.3.3
##
## Attaching package: 'rnaturalearthdata'
##
## The following object is masked from 'package:rnaturalearth':
##
## countries110
library(ggspatial)
## Warning: package 'ggspatial' was built under R version 4.3.3
Data
data <- read.csv("C:/Users/ASUS/OneDrive/Documents/SEM 4/VISDAT/data tugas uas.csv")
head(data)
Korelasi
ggplot(data, aes(x = QualityofSleep, y = SleepDuration, color = Gender)) +
geom_point() +
labs(title = "Scatter Plot Durasi Tidur vs Kualitas Tidur", x = "Kualitas Tidur", y = "Durasi Tidur", color = "Gender") +
theme_minimal()
Dari scatter plot yang dihasilkan, hubungan antara durasi tidur dengan
kualitas tidur pada perempuan dan laki-laki memiliki hungungan positif.
Hal tersebut menjunjukan bahwa jika kualitas tidur meningkat maka durasi
tidur pun meningkat.
Matrix Plot
dataa <- data[, -which(names(data) == "Person.ID")]
data_numerik <- select_if(dataa, is.numeric)
str(data_numerik)
## 'data.frame': 374 obs. of 7 variables:
## $ Age : int 27 28 28 28 28 28 29 29 29 29 ...
## $ SleepDuration : num 6.1 6.2 6.2 5.9 5.9 5.9 6.3 7.8 7.8 7.8 ...
## $ QualityofSleep : int 6 6 6 4 4 4 6 7 7 7 ...
## $ Physical.Activity.Level: int 42 60 60 30 30 30 40 75 75 75 ...
## $ StressLevel : int 6 8 8 8 8 8 7 6 6 6 ...
## $ Heart.Rate : int 77 75 75 85 85 85 82 70 70 70 ...
## $ Daily.Steps : int 4200 10000 10000 3000 3000 3000 3500 8000 8000 8000 ...
data_melt <- cor(data_numerik[sapply(data_numerik,is.numeric)])
data_melt <- melt(data_melt)
ggplot(data_melt, aes(Var1, Var2, fill = value)) +
geom_tile(color = "white") +
scale_fill_gradient2(low = "darkblue", mid = "white", high = "darkred", midpoint = 0, limits = c(-1,1), name="Korelasi") +
labs(title = "Corellogram") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))
Matrix plot ini dapat memberikan informasi terkait kekuatan hubungan
antar variabel. Variabel yang memiliki korelasi positif yang kuat akan
berwarna maroon, yaitu sleep duration dan quality of sleep. Sedangakan,
variabel yang memliki korelasi negatif yang kuat akan berwarna biru
dongker, yaitu quality of sleep dan stress level.
Piecewise Constant (Fungsi Tangga)
mod_tangga = lm(SleepDuration ~ cut(QualityofSleep,5),data=data)
summary(mod_tangga)
##
## Call:
## lm(formula = SleepDuration ~ cut(QualityofSleep, 5), data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.64026 -0.20367 -0.09524 0.25634 0.75974
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.23333 0.09326 66.838 <2e-16 ***
## cut(QualityofSleep, 5)(5,6] -0.03810 0.09845 -0.387 0.699
## cut(QualityofSleep, 5)(6,7] 0.90693 0.10026 9.045 <2e-16 ***
## cut(QualityofSleep, 5)(7,8] 1.17034 0.09826 11.911 <2e-16 ***
## cut(QualityofSleep, 5)(8,9.01] 2.01033 0.10083 19.937 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3231 on 369 degrees of freedom
## Multiple R-squared: 0.8369, Adjusted R-squared: 0.8351
## F-statistic: 473.4 on 4 and 369 DF, p-value: < 2.2e-16
ggplot(data,aes(x=QualityofSleep, y=SleepDuration)) +
geom_point(alpha=0.55, color="black") +
stat_smooth(method = "lm",
formula = y~cut(x,3),
lty = 1, col = "red",se = F)+
theme_bw()
Piecewise Constant atau fungsi tangga tersebut dapat menunjukan hubungan
antara quality of sleep dan sleep duration. Garis merah menunjukan
fungsi berpotongan atau fungsi langkah untuk memodelkan atau
menginterpretasi distribusi titik-titik data.
Locally Estimated Scatter Plot Smoothing (LOESS)
ggplot(data, aes(x = QualityofSleep, y = SleepDuration)) +
geom_point(color = "blue", size = 3, alpha = 0.6) +
geom_smooth(method = "loess", color = "darkred", linetype = "dashed", size = 1.5) +
labs(
x = "Usia",
y = "Durasi Tidur",
title = "LOESS Visualization of Quality of Sleep vs Sleep Duration",
subtitle = "Smoothed scatterplot with LOESS curve",
caption = "bismillah"
) +
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.
## `geom_smooth()` using formula = 'y ~ x'
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : pseudoinverse used at 7
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : neighborhood radius 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : reciprocal condition number 0
## Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
## else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at 7
## Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
## else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius 1
## Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
## else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
## number 0
LOESS atau Locally Estimated Scatter Plot Smoothing adalah kurva yang
dapat menggambarkan hubungan antara kedua variabel dengan bantuan garis
yang lebih mulus dan halus serta kontinu. Jadi gambar di atas adalah
scatter plot yang disesuaikan dengan kurva LOESS. Menunjukan bahwa kedua
variabel berbanding lurus atau memiliki hubungan positif.
VISUALISASI TIME SERIES
databank <- read_excel("C:/Users/ASUS/OneDrive/Documents/SEM 4/VISDAT/Data Time Series.xlsx")
head(databank)
Grafik 1
ggplot(databank, aes(x = Date, y = BCA)) +
geom_point() +
labs(title = "Scatter Plot of Time Series Data",
x = "Date",
y = "Value")
Scatter plot tersebut menuntukan fluktuasi dari harga saham Bank BNI
dari 5 tahun terakhir yaitu 2019-2024. Jika diperhatikan, saham Bank BNI
mengalami penurunan dari tahun 2019 sampai 2020. Namun bila dilihat dari
keseluruhan, mulai tahun 2020 hingga 2024, harga saham mengalami
peningkatan secara konsisten walaupun pada tahun 2021 hingga
peertengahan 2021 mengalami penurunan.
Grafik 2
ggplot(databank, aes(x = Date, y = BNI)) +
geom_line() +
labs(title = "Scatter Plot of Time Series Data",
x = "Date",
y = "Value")
Grafik ini merepresentasikan hal yang sama dengan scatter plot
sebelumnya, namun pada grafik ini berupa line chart. Menurut saya, bila
ditampilkan grafik garis akan menunjukan kenaikan dan penurunan yang
lebih tergambar jelas.
Grafik 3
ggplot(databank, aes(x = Date, y = BNI)) +
geom_line() +
geom_point() +
labs(title = "Scatter Plot of Time Series Data",
x = "Date",
y = "Value")
Grafik diatas berupa gabungan antara scatter plot dan line chart. Tetap
merepresentasikan hal yang sama.
Grafik 4
window_size <- 10
ggplot(databank, aes(x = Date)) +
geom_line(aes(y = BCA), color = "darkred", linetype = "dashed", size = 1) +
geom_ribbon(aes(ymin = -Inf, ymax = BCA), fill = "red", alpha = 0.2) +
labs(title = paste("Time Series Data of BCA's Close (Window Size:", window_size, ")"),
x = "Tanggal",
y = "Harga Penutupan") +
theme_minimal()
Grafik di atas adalah grafik yang dapat merepresentasikn fluktuasi harga
saham bank BCA 5 tahun terakhir. Secara keseluruhan, saham BCA mengalami
kenaikan dari tahun ke tahun.
Grafik 5
ggplot() +
geom_line(data = databank, aes(x = Date, y = BCA, color = "BCA")) +
geom_line(data = databank, aes(x = Date, y = BNI, color = "BNI")) +
scale_color_manual(values = c("lightblue", "pink")) +
labs(title = "Time Series Data for Close of BCA and BNI",
x = "Tanggal",
y = "Harga Penutupan") +
theme_minimal() +
theme(axis.title.y = element_text(color = "black")) +
labs(color = "Bank")
Dua line chart di atas bertujuan untuk menunjukan peebandingan harga
saham Bank BNI dan Bank BCA 5 tahun terakhir, yaitu tahun 2019-2024.
Garis biru merupakan fluktuasi harga saham Bank BCA. Sedangkan, warna
biru menunjukan fluktuasi harga saham Bank BNI. Terlihat bahwa saham
Bank BNI memiliki harga yang berada dibawah harga saham Bank BCA. Tetapi
keduanya sama sama mengalami kenaikan bila dilihat secara
keseluruhan.
VISUALISASI SPASIAL
Packages
library(sf)
library(indonesia)
library(dplyr)
library(readxl)
library(ggplot2)
Data
indonesia_provinsi <- id_map ("indonesia", "provinsi")
indonesia_provinsi$nama_provinsi
## [1] "Aceh" "Bali" "Kep. Bangka Belitung"
## [4] "Banten" "Bengkulu" "Gorontalo"
## [7] "Papua Barat" "DKI Jakarta" "Jambi"
## [10] "Jawa Barat" "Jawa Tengah" "Jawa Timur"
## [13] "Kalimantan Barat" "Kalimantan Selatan" "Kalimantan Tengah"
## [16] "Kalimantan Timur" "Kalimantan Utara" "Kep. Riau"
## [19] "Lampung" "Maluku Utara" "Maluku"
## [22] "Nusa Tenggara Barat" "Nusa Tenggara Timur" "Papua"
## [25] "Riau" "Sulawesi Barat" "Sulawesi Selatan"
## [28] "Sulawesi Tengah" "Sulawesi Tenggara" "Sulawesi Utara"
## [31] "Sumatera Barat" "Sumatera Selatan" "Sumatera Utara"
## [34] "DI Yogyakarta"
Peta Indonesia
ggplot() + geom_sf(data = indonesia_provinsi, fill = "white", color = "black")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
Gambar di atas merupakan gambar peta indonesia yang diberi pembatas
berdasarkan wilayah provinsinya.