Stacked Bar Chart
library(dplyr)
# Menghitung jumlah jawaban Benar dan Salah berdasarkan Kab/Kota dan KBLI
data_summary2 <- data.pkl64 %>%
group_by(b1r102, b3r302_b_value, b12r1202_label) %>%
summarise(count = n(), .groups = "drop") %>%
group_by(b1r102, b3r302_b_value) %>%
mutate(percentage = count / sum(count) * 100) # Menghitung persentase
data_summary2
## # A tibble: 99 × 5
## # Groups: b1r102, b3r302_b_value [63]
## b1r102 b3r302_b_value b12r1202_label count percentage
## <chr> <chr> <chr> <int> <dbl>
## 1 OGAN ILIR C Benar 146 82.5
## 2 OGAN ILIR C Salah 31 17.5
## 3 OGAN ILIR E Benar 3 100
## 4 OGAN ILIR F Benar 9 81.8
## 5 OGAN ILIR F Salah 2 18.2
## 6 OGAN ILIR G Benar 414 85.2
## 7 OGAN ILIR G Salah 72 14.8
## 8 OGAN ILIR H Benar 53 89.8
## 9 OGAN ILIR H Salah 6 10.2
## 10 OGAN ILIR I Benar 136 88.3
## # ℹ 89 more rows
library(extrafont)
## Registering fonts with R
font_import() # Mendeteksi semua font yang tersedia di sistem
## Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
## Continue? [y/n]
## Exiting.
loadfonts(device = "win") # Untuk Windows
## Agency FB already registered with windowsFonts().
## Algerian already registered with windowsFonts().
## Arial Black already registered with windowsFonts().
## Arial already registered with windowsFonts().
## Arial Narrow already registered with windowsFonts().
## Arial Rounded MT Bold already registered with windowsFonts().
## Aringgo - Personal use already registered with windowsFonts().
## Bahnschrift already registered with windowsFonts().
## Baskerville Old Face already registered with windowsFonts().
## Bauhaus 93 already registered with windowsFonts().
## Bell MT already registered with windowsFonts().
## Berlin Sans FB already registered with windowsFonts().
## Berlin Sans FB Demi already registered with windowsFonts().
## Bernard MT Condensed already registered with windowsFonts().
## Blackadder ITC already registered with windowsFonts().
## Bodoni Bd BT already registered with windowsFonts().
## Bodoni Bk BT already registered with windowsFonts().
## Bodoni MT already registered with windowsFonts().
## Bodoni MT Black already registered with windowsFonts().
## Bodoni MT Condensed already registered with windowsFonts().
## Bodoni MT Poster Compressed already registered with windowsFonts().
## Book Antiqua already registered with windowsFonts().
## Bookman Old Style already registered with windowsFonts().
## Bookshelf Symbol 7 already registered with windowsFonts().
## Bradley Hand ITC already registered with windowsFonts().
## Britannic Bold already registered with windowsFonts().
## Broadway already registered with windowsFonts().
## Brush Script MT already registered with windowsFonts().
## Calibri already registered with windowsFonts().
## Calibri Light already registered with windowsFonts().
## Californian FB already registered with windowsFonts().
## Calisto MT already registered with windowsFonts().
## Cambria already registered with windowsFonts().
## Candara already registered with windowsFonts().
## Candara Light already registered with windowsFonts().
## Castellar already registered with windowsFonts().
## Centaur already registered with windowsFonts().
## Century already registered with windowsFonts().
## Century725 Cn BT already registered with windowsFonts().
## Century751 No2 BT already registered with windowsFonts().
## Century751 BT already registered with windowsFonts().
## Century751 SeBd BT already registered with windowsFonts().
## Century Gothic already registered with windowsFonts().
## CentSchbkCyrill BT already registered with windowsFonts().
## Century Schoolbook already registered with windowsFonts().
## Chiller already registered with windowsFonts().
## Clarendon Blk BT already registered with windowsFonts().
## Clarendon BT already registered with windowsFonts().
## Clarendon Lt BT already registered with windowsFonts().
## Colonna MT already registered with windowsFonts().
## Comic Sans MS already registered with windowsFonts().
## Consolas already registered with windowsFonts().
## Constantia already registered with windowsFonts().
## Cooper Black already registered with windowsFonts().
## Copperplate Gothic Bold already registered with windowsFonts().
## Copperplate Gothic Light already registered with windowsFonts().
## Corbel already registered with windowsFonts().
## Corbel Light already registered with windowsFonts().
## Courier New already registered with windowsFonts().
## Curlz MT already registered with windowsFonts().
## DeVinne Txt BT already registered with windowsFonts().
## DFGothic-EB already registered with windowsFonts().
## DFKaiSho-SB already registered with windowsFonts().
## DFMincho-SU already registered with windowsFonts().
## DFMincho-UB already registered with windowsFonts().
## DFMincho-W5 already registered with windowsFonts().
## DFPOP1-W9 already registered with windowsFonts().
## Dubai already registered with windowsFonts().
## Dubai Light already registered with windowsFonts().
## Dubai Medium already registered with windowsFonts().
## Ebrima already registered with windowsFonts().
## Edwardian Script ITC already registered with windowsFonts().
## Elephant already registered with windowsFonts().
## Embassy BT already registered with windowsFonts().
## EngraversGothic BT already registered with windowsFonts().
## Engravers MT already registered with windowsFonts().
## Eras Bold ITC already registered with windowsFonts().
## Eras Demi ITC already registered with windowsFonts().
## Eras Light ITC already registered with windowsFonts().
## Eras Medium ITC already registered with windowsFonts().
## Exotc350 Bd BT already registered with windowsFonts().
## Exotc350 DmBd BT already registered with windowsFonts().
## Felix Titling already registered with windowsFonts().
## Footlight MT Light already registered with windowsFonts().
## Forte already registered with windowsFonts().
## Franklin Gothic Book already registered with windowsFonts().
## Franklin Gothic Demi already registered with windowsFonts().
## Franklin Gothic Demi Cond already registered with windowsFonts().
## Franklin Gothic Heavy already registered with windowsFonts().
## Franklin Gothic Medium already registered with windowsFonts().
## Franklin Gothic Medium Cond already registered with windowsFonts().
## Freehand521 BT already registered with windowsFonts().
## Freestyle Script already registered with windowsFonts().
## French Script MT already registered with windowsFonts().
## Futura Md BT already registered with windowsFonts().
## Futura Bk BT already registered with windowsFonts().
## Gabriola already registered with windowsFonts().
## Gadugi already registered with windowsFonts().
## Garamond already registered with windowsFonts().
## Genuine already registered with windowsFonts().
## Geometr212 BkCn BT already registered with windowsFonts().
## Geometr415 Blk BT already registered with windowsFonts().
## Geometr706 BlkCn BT already registered with windowsFonts().
## GeoSlab703 Md BT already registered with windowsFonts().
## GeoSlab703 MdCn BT already registered with windowsFonts().
## Georgia already registered with windowsFonts().
## Gigi already registered with windowsFonts().
## Gill Sans Ultra Bold already registered with windowsFonts().
## Gill Sans Ultra Bold Condensed already registered with windowsFonts().
## Gill Sans MT already registered with windowsFonts().
## Gill Sans MT Condensed already registered with windowsFonts().
## Gill Sans MT Ext Condensed Bold already registered with windowsFonts().
## Gloucester MT Extra Condensed already registered with windowsFonts().
## Goudy Old Style already registered with windowsFonts().
## Goudy Stout already registered with windowsFonts().
## Haettenschweiler already registered with windowsFonts().
## Harlow Solid Italic already registered with windowsFonts().
## Harrington already registered with windowsFonts().
## High Tower Text already registered with windowsFonts().
## Humanst521 BT already registered with windowsFonts().
## Humanst521 Lt BT already registered with windowsFonts().
## Humnst777 Blk BT already registered with windowsFonts().
## Humnst777 BlkCn BT already registered with windowsFonts().
## Humnst777 Cn BT already registered with windowsFonts().
## Humnst777 Lt BT already registered with windowsFonts().
## Humnst777 BT already registered with windowsFonts().
## IcoMoon-Free already registered with windowsFonts().
## Impact already registered with windowsFonts().
## Imprint MT Shadow already registered with windowsFonts().
## Informal Roman already registered with windowsFonts().
## Ink Free already registered with windowsFonts().
## Javanese Text already registered with windowsFonts().
## Jokerman already registered with windowsFonts().
## Juice ITC already registered with windowsFonts().
## Kaufmann BT already registered with windowsFonts().
## Kristen ITC already registered with windowsFonts().
## Kunstler Script already registered with windowsFonts().
## Wide Latin already registered with windowsFonts().
## Leelawadee UI already registered with windowsFonts().
## Leelawadee UI Semilight already registered with windowsFonts().
## Lucida Bright already registered with windowsFonts().
## Lucida Calligraphy already registered with windowsFonts().
## Lucida Console already registered with windowsFonts().
## Lucida Fax already registered with windowsFonts().
## Lucida Handwriting already registered with windowsFonts().
## Lucida Sans already registered with windowsFonts().
## Lucida Sans Typewriter already registered with windowsFonts().
## Lucida Sans Unicode already registered with windowsFonts().
## Magneto already registered with windowsFonts().
## Maiandra GD already registered with windowsFonts().
## Malgun Gothic already registered with windowsFonts().
## Malgun Gothic Semilight already registered with windowsFonts().
## Marlett already registered with windowsFonts().
## Matura MT Script Capitals already registered with windowsFonts().
## Microsoft Himalaya already registered with windowsFonts().
## Microsoft Yi Baiti already registered with windowsFonts().
## Microsoft New Tai Lue already registered with windowsFonts().
## Microsoft PhagsPa already registered with windowsFonts().
## Microsoft Sans Serif already registered with windowsFonts().
## Microsoft Tai Le already registered with windowsFonts().
## Mistral already registered with windowsFonts().
## Modern No. 20 already registered with windowsFonts().
## Mongolian Baiti already registered with windowsFonts().
## Monotype Corsiva already registered with windowsFonts().
## MS Outlook already registered with windowsFonts().
## MS Reference Sans Serif already registered with windowsFonts().
## MS Reference Specialty already registered with windowsFonts().
## MT Extra already registered with windowsFonts().
## MV Boli already registered with windowsFonts().
## Myanmar Text already registered with windowsFonts().
## News701 BT already registered with windowsFonts().
## News706 BT already registered with windowsFonts().
## NewsGoth BT already registered with windowsFonts().
## NewsGoth Lt BT already registered with windowsFonts().
## NewsGoth Cn BT already registered with windowsFonts().
## Niagara Engraved already registered with windowsFonts().
## Niagara Solid already registered with windowsFonts().
## Nunito ExtraLight already registered with windowsFonts().
## OCR-A BT already registered with windowsFonts().
## OCR A Extended already registered with windowsFonts().
## OCR-B 10 BT already registered with windowsFonts().
## Old English Text MT already registered with windowsFonts().
## Onyx already registered with windowsFonts().
## Palace Script MT already registered with windowsFonts().
## Palatino Linotype already registered with windowsFonts().
## Papyrus already registered with windowsFonts().
## Parchment already registered with windowsFonts().
## Perpetua already registered with windowsFonts().
## Perpetua Titling MT already registered with windowsFonts().
## Playbill already registered with windowsFonts().
## Poor Richard already registered with windowsFonts().
## Pristina already registered with windowsFonts().
## Rage Italic already registered with windowsFonts().
## Ravie already registered with windowsFonts().
## Roboto already registered with windowsFonts().
## Rockwell already registered with windowsFonts().
## Rockwell Condensed already registered with windowsFonts().
## Rockwell Extra Bold already registered with windowsFonts().
## Sans Serif Collection already registered with windowsFonts().
## Schadow BT already registered with windowsFonts().
## Script MT Bold already registered with windowsFonts().
## Segoe Fluent Icons already registered with windowsFonts().
## Segoe MDL2 Assets already registered with windowsFonts().
## Segoe Print already registered with windowsFonts().
## Segoe Script already registered with windowsFonts().
## Segoe UI already registered with windowsFonts().
## Segoe UI Light already registered with windowsFonts().
## Segoe UI Semibold already registered with windowsFonts().
## Segoe UI Semilight already registered with windowsFonts().
## Segoe UI Black already registered with windowsFonts().
## Segoe UI Emoji already registered with windowsFonts().
## Segoe UI Historic already registered with windowsFonts().
## Segoe UI Symbol already registered with windowsFonts().
## Segoe UI Variable already registered with windowsFonts().
## Showcard Gothic already registered with windowsFonts().
## SimSun-ExtB already registered with windowsFonts().
## SimSun-ExtG already registered with windowsFonts().
## Sitka Text already registered with windowsFonts().
## Snap ITC already registered with windowsFonts().
## Square721 BT already registered with windowsFonts().
## Square721 Cn BT already registered with windowsFonts().
## Stencil already registered with windowsFonts().
## Swis721 Blk BT already registered with windowsFonts().
## Swis721 BlkCn BT already registered with windowsFonts().
## Swis721 WGL4 BT already registered with windowsFonts().
## Swis721 BT already registered with windowsFonts().
## Swis721 Cn BT already registered with windowsFonts().
## Swis721 Hv BT already registered with windowsFonts().
## Swis721 Lt BT already registered with windowsFonts().
## Swis721 LtEx BT already registered with windowsFonts().
## Sylfaen already registered with windowsFonts().
## Symbol already registered with windowsFonts().
## Tahoma already registered with windowsFonts().
## Tempus Sans ITC already registered with windowsFonts().
## Times New Roman already registered with windowsFonts().
## Trebuchet MS already registered with windowsFonts().
## Tw Cen MT already registered with windowsFonts().
## Tw Cen MT Condensed already registered with windowsFonts().
## Tw Cen MT Condensed Extra Bold already registered with windowsFonts().
## TypoUpright BT already registered with windowsFonts().
## Verdana already registered with windowsFonts().
## Viner Hand ITC already registered with windowsFonts().
## Vivaldi already registered with windowsFonts().
## Vladimir Script already registered with windowsFonts().
## Webdings already registered with windowsFonts().
## Wingdings already registered with windowsFonts().
## Wingdings 2 already registered with windowsFonts().
## Wingdings 3 already registered with windowsFonts().
library(ggplot2)
# Membuat stacked bar chart dengan format teks yang diinginkan
ggplot(data_summary2, aes(x = as.factor(b3r302_b_value), y = percentage, fill = b12r1202_label)) +
geom_bar(stat = "identity", position = "stack") + # Stacked Bar dengan skala 100%
# Menambahkan label persentase dengan font Times New Roman (10pt)
geom_text(aes(label = paste0(round(percentage, 1), "%"), color = b12r1202_label),
position = position_stack(vjust = 0.5), size = 4, fontface = "bold", family = "Times New Roman") +
facet_wrap(~b1r102, scales = "free_x") + # Panel per Kab/Kota
# Menyesuaikan judul grafik dengan font Courier, ukuran 14 pt, dan centered
ggtitle("Distribusi Persentase Jawaban Benar dan Salah B12R02 Berdasarkan KBLI di Setiap Kab/Kota") +
theme_minimal() +
# Menyesuaikan warna batang (Benar = Biru, Salah = Merah)
scale_fill_manual(values = c("Benar" = "#4a3d99", "Salah" = "#ffa18e")) +
scale_color_manual(values = c("Benar" = "white", "Salah" = "black")) + # Warna teks menyesuaikan dengan batang
# Mengatur sumbu Y dalam skala 0-100% dengan format persentase
scale_y_continuous(labels = scales::percent_format(scale = 1), limits = c(0, 100)) +
# Mengatur gaya teks
theme(
plot.title = element_text(family = "Courier New", size = 14, face = "bold", hjust = 0.5), # Judul grafik
axis.title.x = element_text(family = "Arial", size = 10, face = "bold"), # Label sumbu X
axis.title.y = element_text(family = "Arial", size = 10, face = "bold"), # Label sumbu Y
axis.text.x = element_text(angle = 90, hjust = 1, family = "Times New Roman", size = 10), # Label Kode KBLI
axis.text.y = element_text(family = "Times New Roman", size = 10) # Label sumbu Y
)
## Warning: Removed 1 rows containing missing values (`geom_bar()`).
