Sebaran Kepemilikan Mobil di Indonesia pada tahun 2022
# Memuat paket
library(readxl)
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(ggplot2)
library(rnaturalearth)
library(rnaturalearthdata)
##
## Attaching package: 'rnaturalearthdata'
## The following object is masked from 'package:rnaturalearth':
##
## countries110
# Path ke file Excel Anda
file_path <- "D:/beatrics/jumlah-mobil-di-indonesia-berdasarkan-provinsi-2022.xlsx"
# Membaca data dari file Excel
data <- read_excel(file_path, sheet = 1)
# Menampilkan data awal
head(data)
## # A tibble: 6 × 3
## No. Provinsi `Jumlah Mobil`
## <dbl> <chr> <chr>
## 1 1 Jawa Barat 3.814.187
## 2 2 DKI Jakarta 3.766.059
## 3 3 Jawa Timur 2.034.087
## 4 4 Jawa Tengah 1.456.793
## 5 5 Sumatra Utara 730.953
## 6 6 Sulawesi Selatan 564.060
# Membaca data dari Excel
data <- read_excel("D:/beatrics/jumlah-mobil-di-indonesia-berdasarkan-provinsi-2022.xlsx")
# Memuat data spasial Indonesia
indo <- ne_states(country = "Indonesia", returnclass = "sf")
# Menggabungkan data mobil dengan data spasial
merged_data <- merge(indo, data, by.x = "name", by.y = "Provinsi", all.x = TRUE)
library(RColorBrewer)
ggplot() +
geom_sf(data = merged_data, aes(fill = factor(`Jumlah Mobil`))) +
scale_fill_brewer(palette = "Set1") +
labs(title = "Jumlah Mobil di Indonesia Berdasarkan Provinsi (2022)",
fill = "Jumlah Mobil")
## Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors

ggsave("visualisasi_spasial.png")
## Saving 7 x 5 in image
## Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors