Ada beberapa library yang digunakan, seperti:
library(ggplot2)
library(dplyr)
##
## 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(readxl)
library(sf)
## Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
Data HairEyeColor adalah paket bawaan datasets di R. HairEyeColor adalah dataset kategorik yang menggambarkan hubungan antara warna rambut, warna mata, dan jenis kelamin dalam bentuk tabel kontingensi.
datasets::HairEyeColor
## , , Sex = Male
##
## Eye
## Hair Brown Blue Hazel Green
## Black 32 11 10 3
## Brown 53 50 25 15
## Red 10 10 7 7
## Blond 3 30 5 8
##
## , , Sex = Female
##
## Eye
## Hair Brown Blue Hazel Green
## Black 36 9 5 2
## Brown 66 34 29 14
## Red 16 7 7 7
## Blond 4 64 5 8
hair_df <- as.data.frame(HairEyeColor)
head(HairEyeColor)
## , , Sex = Male
##
## Eye
## Hair Brown Blue Hazel Green
## Black 32 11 10 3
## Brown 53 50 25 15
## Red 10 10 7 7
## Blond 3 30 5 8
##
## , , Sex = Female
##
## Eye
## Hair Brown Blue Hazel Green
## Black 36 9 5 2
## Brown 66 34 29 14
## Red 16 7 7 7
## Blond 4 64 5 8
View(HairEyeColor)
NROW(HairEyeColor)
## [1] 4
dim(HairEyeColor)
## [1] 4 4 2
Bar Chart digunakan untuk menampilkan jumlah orang berdasarkan warna mata.
# Menghitung total jumlah orang berdasarkan warna mata
eye_counts <- hair_df %>%
group_by(Eye) %>%
summarise(total = sum(Freq))
# Bar plot
barplot(eye_counts$total, names.arg = eye_counts$Eye,
col = c("brown", "blue", "darkgoldenrod", "green"),
main = "Jumlah Orang Berdasarkan Warna Mata",
xlab = "Warna Mata", ylab = "Jumlah Orang")
Needle Chart digunakan untuk menampilkan jumlah orang berdasarkan warna rambut.
hair_summary <- hair_df %>%
group_by(Hair) %>%
summarise(Total = sum(Freq))
ggplot(hair_summary, aes(x = Hair, y = Total)) +
geom_segment(aes(xend = Hair, yend = 0), color = "blue", size = 1.2) +
geom_point(size = 5, color = "red", alpha = 1) +
coord_flip() +
labs(title = "Needle Plot Horizontal: Warna Rambut vs Total Orang",
x = "Warna Rambut", y = "Total Orang") +
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.
Grouped Bar Chart digunakan untuk memperlihatkan perbedaan jumlah orang berdasarkan warna mata dan jenis kelamin.
ggplot(hair_df, aes(x = Eye, y = Freq, fill = Sex)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Grouped Bar Chart: Warna Mata dan Jenis Kelamin",
x = "Warna Mata", y = "Jumlah Orang",
fill = "Jenis Kelamin") +
theme_minimal()
Stacked Bar Chart digunakan untuk melihat hubungan warna rambut dan warna mata.
ggplot(hair_df, aes(x = Hair, y = Freq, fill = Eye)) +
geom_bar(stat = "identity", position = "stack") +
labs(title = "Stacked Bar Chart: Warna Rambut vs Warna Mata",
x = "Warna Rambut", y = "Jumlah Orang",
fill = "Warna Mata") +
theme_minimal()
eye_summary <- hair_df %>%
group_by(Eye) %>%
summarise(Total = sum(Freq))
ggplot(eye_summary, aes(x = "", y = Total, fill = Eye)) +
geom_col(width = 1, color = "black") + # Membuat batang untuk pie
coord_polar(theta = "y") + # Ubah ke bentuk pie chart
geom_text(aes(label = Total), position = position_stack(vjust = 0.5), size = 5) +
scale_fill_manual(values = c("Brown" = "brown", "Blue" = "blue",
"Green" = "green", "Hazel" = "darkgoldenrod")) + # Warna khusus untuk hazel
labs(title = "Pie Chart: Warna Mata dalam Dataset",
fill = "Warna Mata") +
theme_void() # Hilangkan grid & sumbu
Peta Spasial ini menggunakan data dari excel dan shape untuk wilayah Jambi.
#Import Data Excel
data.spasial=read_xlsx("C:/Users/priya/Downloads/Export_Output.xlsx",sheet = 1)
head(data.spasial)
## # A tibble: 6 × 7
## NAME_1 ID_2 NAME_2 TYPE_2 ENGTYPE_2 Longitude Latitude
## <chr> <dbl> <chr> <chr> <chr> <dbl> <dbl>
## 1 Jambi 73 Batang Hari Kabupaten Regency 103. -1.75
## 2 Jambi 74 Bungo Kabupaten Regency 102. -1.60
## 3 Jambi 75 Jambi Kotamadya Municipality 104. -1.62
## 4 Jambi 76 Kerinci Kabupaten Regency 101. -2.03
## 5 Jambi 77 Merangin Kabupaten Regency 102. -2.18
## 6 Jambi 78 Muaro Jambi Kabupaten Regency 104. -1.60
#IMPORT PETA SHP
shp.jambi=read_sf("C:/Users/priya/Downloads/PETA SHP 34 Prov/9-Jambi/Export_Output.shp")
#Menggabungkan Data ke file SHP
gabung.jambi=left_join(shp.jambi,data.spasial,by="ID_2")
head(gabung.jambi)
## Simple feature collection with 6 features and 13 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 101.1227 ymin: -2.770429 xmax: 104.3701 ymax: -1.09967
## Geodetic CRS: WGS 84
## # A tibble: 6 × 14
## NAME_1.x ID_2 NAME_2.x TYPE_2.x ENGTYPE_2.x Longitude.x Latitude.x
## <chr> <dbl> <chr> <chr> <chr> <dbl> <dbl>
## 1 Jambi 73 Batang Hari Kabupaten Regency 103. -1.75
## 2 Jambi 74 Bungo Kabupaten Regency 102. -1.60
## 3 Jambi 75 Jambi Kotamadya Municipality 104. -1.62
## 4 Jambi 76 Kerinci Kabupaten Regency 101. -2.03
## 5 Jambi 77 Merangin Kabupaten Regency 102. -2.18
## 6 Jambi 78 Muaro Jambi Kabupaten Regency 104. -1.60
## # ℹ 7 more variables: geometry <MULTIPOLYGON [°]>, NAME_1.y <chr>,
## # NAME_2.y <chr>, TYPE_2.y <chr>, ENGTYPE_2.y <chr>, Longitude.y <dbl>,
## # Latitude.y <dbl>
Pemetaan data Spasial dengan nilai Latitude.
#Pemetaan Index Rate Kasus DBD
plot.jambi = ggplot(data=gabung.jambi) +
geom_sf(aes(fill = Latitude.x)) +
scale_fill_distiller("Index Rate", palette = "GnBu")
plot.jambi