Nested pie chart (diagram pie bersarang) sering disebut juga dengan dobel donut charts atau multi-level donut chart. Nested pie chart berguna saat data yang digunakan terdiri atas kategori kecil yang menjadi bagian dari kategori yang lebih besar atau ketika kita ingin membandingkan data serupa dari berbagai periode/kategori dan memperlihatkan hasilnya dalam satu bagan.
Berikut diilustrasikan pembentukan nested pie chart menggunakan data bank.csv yang digunakan pada pertemuan-pertemuan sebelumnya.
library(ggplot2)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.5
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ tibble 3.1.6 ✓ purrr 0.3.4
## ✓ tidyr 1.2.0 ✓ stringr 1.4.0
## ✓ readr 2.1.2 ✓ forcats 0.5.1
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'readr' was built under R version 4.0.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
data = read.csv("/Volumes/Macintosh HD - Data/KULIAH S2/STA12612 Visualisasi Data/bank.csv", sep = ",", header = TRUE)
str(data)
## 'data.frame': 4521 obs. of 18 variables:
## $ age : int 30 33 35 30 59 35 36 39 41 43 ...
## $ job : chr "unemployed" "services" "management" "management" ...
## $ marital : chr "married" "married" "single" "married" ...
## $ education: chr "primary" "secondary" "tertiary" "tertiary" ...
## $ default : chr "no" "no" "no" "no" ...
## $ balance : int 1787 4789 1350 1476 0 747 307 147 221 -88 ...
## $ housing : chr "no" "yes" "yes" "yes" ...
## $ loan : chr "no" "yes" "no" "yes" ...
## $ contact : chr "cellular" "cellular" "cellular" "unknown" ...
## $ day : int 19 11 16 3 5 23 14 6 14 17 ...
## $ month : chr "oct" "may" "apr" "jun" ...
## $ duration : int 79 220 185 199 226 141 341 151 57 313 ...
## $ campaign : int 1 1 1 4 1 2 1 2 2 1 ...
## $ pdays : int -1 339 330 -1 -1 176 330 -1 -1 147 ...
## $ previous : int 0 4 1 0 0 3 2 0 0 2 ...
## $ y : chr "no" "no" "no" "no" ...
## $ weight : num 63.2 68.5 78.5 95.3 60.1 94.8 97.2 82.8 81.2 53.1 ...
## $ hight : num 161 165 174 187 158 ...
ggplot(data) +
aes(x = education,fill = marital,color = marital) +
geom_bar(position = "fill") +
coord_polar("y")+
theme_minimal()
ggplot(data) +
aes(x = marital,fill = education,color = education) +
geom_bar(position = "fill") +
scale_color_manual(values=c("red", "green","blue","purple")) +
scale_fill_hue(direction = 1) +
coord_polar("y")+
theme_void()
Untuk membuat peta pohon di R dapat digunakan library treemap. Peta pohon mirip dengan diagram lingkaran karena menampilkan proporsi secara visual dengan memvariasikan area bentuk. Peta pohon memiliki dua keunggulan dibandingkan diagram lingkaran. Pertama, elemen yang dapat ditampilkan lebih banyak. Sementara dalam diagram lingkaran, ada batas atas jumlah irisan yang dapat ditambahkan ke lingkaran (jika irisan terlalu banyak maka diagram lingkaran menjadi kurang informatif). Pada treemap, dapat menampilkan ratusan hingga ribuan irisan kategori. Kedua, treemap memungkinkan untuk mengatur elemen data secara hierarkis. Artinya, proporsi pada data dapat dikelompokkan menggunakan variabel kategori dalam data Anda. Untuk membentuk treeemap, diperlukan variabel kuantitatif bernilai positif. Misalnya: - Jumlah pembunuhan (variabel kuantitatif) di kabupaten yang berada di provinsi Jawa Barat (variabel kategoris) - Jumlah pekerja (variabel kuantitatif) di industri tertentu (variabel kategoris)
library(ggplot2)
library(dplyr)
library(treemapify)
datatm <- data %>% count(job)
datatm
## job n
## 1 admin. 478
## 2 blue-collar 946
## 3 entrepreneur 168
## 4 housemaid 112
## 5 management 969
## 6 retired 230
## 7 self-employed 183
## 8 services 417
## 9 student 84
## 10 technician 768
## 11 unemployed 128
## 12 unknown 38
#Data nasabah bank sesuai pekerjaannya
pl <- ggplot(data = datatm, aes(fill = job, area = n))
pl <- pl + geom_treemap()
pl <- pl + theme(legend.position = "bottom")
pl
#lanjutan
pl <- ggplot(data = datatm, aes(fill = job, area = n, label = n))
pl <- pl + geom_treemap()
pl <- pl + geom_treemap_text(colour ="white", place = "centre")
pl <- pl + theme(legend.position = "right")
pl
#Lanjutan
pl <- ggplot(data = datatm, aes(fill = job, area = n, label = paste0(job,"\n", n)))
pl <- pl + geom_treemap()
pl <- pl + geom_treemap_text(colour ="white", place = "centre")
pl <- pl + theme(legend.position = "none")
pl
#install.packages("randomcoloR")
library(randomcoloR)
a <- 12
palette <- distinctColorPalette(a)
pl <- ggplot(data = datatm, aes(fill = job, area = n, label = paste0(job,"\n", n)))
pl <- pl + geom_treemap()
pl <- pl + geom_treemap_text(colour ="white", place = "centre")
pl <- pl + labs(title ="Pekerjaan Nasabah pada Bank X")
pl <- pl + labs(caption ="#Aspraknidiaaulia")
pl <- pl + theme(legend.position = "none")
pl <- pl + scale_fill_manual(values = palette)
pl
Parallel set plot memungkinkan untuk membandingkan variabel dari beberapa pengamatan individu pada satu set variabel numerik. Parallel set plot menghubungkan nilai variabel sebagai rangkaian garis.
library(tidyverse)
library(hrbrthemes)
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
## Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
## if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
library(patchwork)
library(GGally)
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
library(viridis)
## Loading required package: viridisLite
datasp <- iris
# Plot
datasp %>%
ggparcoord(
columns = 1:4, groupColumn = 5, order = "anyClass",
showPoints = TRUE,
title = "Parallel Coordinate Plot for the Iris Data",
alphaLines = 0.3
) +
scale_color_viridis(discrete=TRUE) +
theme_ipsum()+
theme(
plot.title = element_text(size=10)
)
diamonds %>%
sample_n(10) %>%
ggparcoord(
columns = c(1,5:7),
groupColumn = 2,
#order = "anyClass",
showPoints = TRUE,
title = "Diamonds features",
alphaLines = 0.3
) +
scale_color_viridis(discrete=TRUE) +
theme_ipsum()+
theme(
plot.title = element_text(size=10)
)