Setup

Memanggil package yang diperlukan untuk visualisasi data.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.0     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1     
## ── 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(nycflights13)

Dataset yang digunakan adalah flights dari package nycflights13.


Data Visualization

Data visualization adalah proses menampilkan data dalam bentuk grafik untuk memahami pola, hubungan antar variabel, dan distribusi data.

Dalam R, visualisasi biasanya dibuat menggunakan ggplot2 yang merupakan bagian dari tidyverse.


Struktur Dasar ggplot

Struktur dasar grafik menggunakan ggplot adalah:

ggplot(data = flights, aes(x = dep_delay, y = arr_delay)) +
  geom_point()
## Warning: Removed 9430 rows containing missing values or values outside the scale range
## (`geom_point()`).

Penjelasan:


Scatterplot

Scatterplot digunakan untuk melihat hubungan antara dua variabel numerik.

Untuk menghindari terlalu banyak titik, kita menggunakan sebagian data.

flights_sample <- flights %>%
  sample_n(1000)

ggplot(data = flights_sample, aes(x = dep_delay, y = arr_delay)) +
  geom_point()
## Warning: Removed 30 rows containing missing values or values outside the scale range
## (`geom_point()`).


Overplotting

Jika terlalu banyak titik yang saling menumpuk, grafik menjadi sulit dibaca. Hal ini disebut overplotting.

Solusinya adalah menggunakan transparansi.

ggplot(data = flights_sample, aes(x = dep_delay, y = arr_delay)) +
  geom_point(alpha = 0.3)
## Warning: Removed 30 rows containing missing values or values outside the scale range
## (`geom_point()`).


Jitter

Jitter digunakan untuk menyebarkan titik agar tidak saling menumpuk.

ggplot(data = flights_sample, aes(x = carrier, y = dep_delay)) +
  geom_jitter()
## Warning: Removed 28 rows containing missing values or values outside the scale range
## (`geom_point()`).


Menambahkan Warna

Warna dapat digunakan untuk membedakan kategori.

ggplot(data = flights_sample, aes(x = dep_delay, y = arr_delay, color = carrier)) +
  geom_point(alpha = 0.5)
## Warning: Removed 30 rows containing missing values or values outside the scale range
## (`geom_point()`).


Garis Tren

Garis tren membantu melihat pola hubungan antar variabel.

ggplot(data = flights_sample, aes(x = dep_delay, y = arr_delay)) +
  geom_point(alpha = 0.3) +
  geom_smooth()
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 30 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 30 rows containing missing values or values outside the scale range
## (`geom_point()`).


Faceting

Faceting membagi grafik menjadi beberapa panel berdasarkan kategori tertentu.

ggplot(data = flights_sample, aes(x = dep_delay, y = arr_delay)) +
  geom_point(alpha = 0.3) +
  facet_wrap(~ carrier)
## Warning: Removed 30 rows containing missing values or values outside the scale range
## (`geom_point()`).


Histogram

Histogram digunakan untuk melihat distribusi variabel numerik.

ggplot(data = flights_sample, aes(x = dep_delay)) +
  geom_histogram(binwidth = 5)
## Warning: Removed 28 rows containing non-finite outside the scale range
## (`stat_bin()`).


Boxplot

Boxplot menunjukkan distribusi data dan membantu mendeteksi outlier.

ggplot(data = flights_sample, aes(x = carrier, y = dep_delay)) +
  geom_boxplot()
## Warning: Removed 28 rows containing non-finite outside the scale range
## (`stat_boxplot()`).


Barplot

Barplot digunakan untuk melihat jumlah kategori dalam dataset.

ggplot(data = flights_sample, aes(x = carrier)) +
  geom_bar()


Barplot dengan Dua Variabel

Barplot juga dapat digunakan untuk membandingkan dua kategori.

ggplot(data = flights_sample, aes(x = carrier, fill = origin)) +
  geom_bar()