ggplot2 adalah salah satu paket visualisasi data paling populer dalam bahasa pemrograman R. Dikembangkan oleh Hadley Wickham, ggplot2 mengimplementasikan konsep “Grammar of Graphics” yang memungkinkan pengguna untuk membuat visualisasi data yang kompleks dengan cara yang sistematis dan intuitif. Dengan ggplot2, Anda dapat membuat berbagai jenis grafik, seperti scatter plot, bar plot, histogram, dan banyak lagi, hanya dengan beberapa baris kode.
Salah satu keunggulan ggplot2 adalah kemampuannya untuk memisahkan komponen data dari komponen estetika. Ini berarti Anda dapat fokus pada data yang ingin divisualisasikan tanpa harus khawatir tentang detail teknis seperti warna, ukuran, atau bentuk. Selain itu, ggplot2 sangat fleksibel dan dapat disesuaikan dengan kebutuhan analisis data Anda.
ggplot2 juga memiliki komunitas yang besar dan aktif, sehingga banyak sumber daya dan tutorial tersedia untuk membantu Anda mempelajari dan menguasai paket ini. Dengan ggplot2, visualisasi data menjadi lebih mudah, cepat, dan menarik.
ggplot2 bekerja dengan membangun plot lapis demi lapis. Setiap lapis menambahkan elemen baru ke plot, seperti titik, garis, atau batang. Berikut adalah contoh sederhana penggunaan ggplot2 untuk membuat scatter plot:
# memanggil library
library(ggplot2)
# membuat data dengan base R
data <- data.frame(x = rnorm(100), y = rnorm(100))
# membuat scotter plot
ggplot(data, aes(x = x, y = y)) +
geom_point() +
labs(title = "Plot Titik", x = "Sumbu X", y = "Sumbu Y") +
theme_dark()
# membuat data dengan base R
data <- data.frame(x = rnorm(100), y = rnorm(100), grup = sample(c("A","B","C","D"), 100, replace = TRUE))
# membuat scotter plot dengan warna berbeda setiap grupnya
ggplot(data, aes(x = x, y = y, color = grup)) +
geom_point() +
facet_wrap(~ grup) +
scale_color_manual(values = c("A" = "green", "B" = "red", "C" = "blue", "D" = "yellow")) +
theme_minimal()
Pada contoh di atas, ggplot() digunakan untuk menginisialisasi plot, aes() menentukan pemetaan estetika (sumbu x dan y), geom_point() menambahkan titik-titik ke plot, labs() menambahkan judul dan label sumbu, dan theme_minimal() menerapkan tema minimalis.
# memanggil library
library(readr)
# membuat nama data dan memanggilnya sesuai nama file
iris_iris <- read_csv("iris - iris.csv")
## Rows: 150 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): variety
## dbl (4): sepal.length, sepal.width, petal.length, petal.width
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 1628 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): date, scientific_name, common_name, zone, site, plot, spp, sex, rc...
## dbl (6): pit, toe_num, sv_length, total_length, weight, pc
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ggplot(lizard, aes(x = total_length , y = weight)) +
geom_point() +
labs(title = "Plot Titik", x = "total_length" , y = "weight" )
ggplot(lizard, aes(x = total_length , y = common_name)) +
geom_jitter() +
labs(title = "Plot Titik", x = "total_length" , y = "common_name" )
ggplot(lizard, aes(x = total_length)) +
geom_histogram() +
labs(title = "Plot histogram", x = "total_length" )
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(lizard, aes(x = weight)) +
geom_histogram(colour = "blue", fill = "yellow", size = 2, linetype = "dotted") +
labs(title = "Plot Histogram Berwarna", x = "weight" )
## 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.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(lizard, aes(x = total_length , y = weight)) +
geom_point(colour = "blue", shape = 21, size = 1) +
labs(title = "Plot Titik", x = "total_length" , y = "weight" )
ggplot(lizard, aes(x = total_length, y = weight, color = common_name, size = total_length)) +
geom_point(shape = 19) +
labs(title = "Plot Titik", x = "total_length", y = "weight") +
theme_minimal()
ggplot(data, aes(x = gdpPercap, y = lifeExp )) +
geom_point(alpha = 0.6, colour = "navy") +
scale_x_log10() +
labs(title = "Sebaran angka hidup perkapita", x = "gdpPercap", y = "lifeExp") +
theme_minimal()
gapminder_plot <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop )) +
geom_point(alpha = 0.6) +
scale_x_log10() +
scale_color_viridis_d(option = "viridis")+
labs(title = "Sebaran angka hidup perkapita", x = "gdpPercap", y = "lifeExp") +
theme_minimal()
gapminder_plot
library(gganimate)
animated_gapminder <- gapminder_plot + transition_time(year)+
labs(title = "Year: {frame_time}")
animate(animated_gapminder, nframes = 100, renderer = gifski_renderer("animated_gapminder.gif"))
library(gganimate)
gapminder_plot + facet_wrap(~continent) +
transition_time(year) +
labs(title = "Year: {frame_time}")
library(gganimate)
gapminder_plot + transition_time(year) +
labs(title = "Year: {frame_time}") +
view_follow(fixed_y = TRUE)