Contoh Soal 1

data<-read.table(file.choose(), header=TRUE)
data
##               Provinsi   IPM
## 1                 ACEH 74.03
## 2       SUMATERA_UTARA 74.02
## 3       SUMATERA_BARAT 74.49
## 4                 RIAU 74.79
## 5                JAMBI 73.43
## 6     SUMATERA_SELATAN 72.30
## 7             BENGKULU 73.39
## 8              LAMPUNG 71.81
## 9  KEP_BANGKA_BELITUNG 73.33
## 10            KEP_RIAU 77.97
## 11         DKI_JAKARTA 83.08
## 12          JAWA_BARAT 74.43
## 13         JAWA_TENGAH 73.88
## 14       DI_YOGYAKARTA 81.55
## 15          JAWA_TIMUR 74.09
## 16              BANTEN 74.48
## 17                BALI 77.76
## 18 NUSA_TENGGARA_BARAT 70.93
## 19 NUSA_TENGGARA_TIMUR 67.39
## 20    KALIMANTAN_BARAT 70.13
## 21   KALIMANTAN_TENGAH 72.73
## 22  KALIMANTAN_SELATAN 73.03
## 23    KALIMANTAN_TIMUR 78.83
## 24    KALIMANTAN_UTARA 73.02
## 25      SULAWESI_UTARA 75.03
## 26     SULAWESI_TENGAH 71.56
## 27    SULAWESI_SELATAN 74.05
## 28   SULAWESI_TENGGARA 73.48
## 29           GORONTALO 71.23
## 30      SULAWESI_BARAT 68.20
## 31              MALUKU 71.57
## 32        MALUKU_UTARA 71.03
## 33         PAPUA_BARAT 67.02
## 34    PAPUA_BARAT_DAYA 68.63
## 35               PAPUA 73.00
## 36       PAPUA_SELATAN 67.90
## 37        PAPUA_TENGAH 59.75
## 38    PAPUA_PEGUNUNGAN 53.42
length(data$IPM)
## [1] 38
mean(data$IPM)
## [1] 72.38842
median(data$IPM)
## [1] 73.18
range(data$IPM)
## [1] 53.42 83.08
max(data$IPM)-min(data$IPM)
## [1] 29.66
max(data$IPM)
## [1] 83.08
min(data$IPM)
## [1] 53.42
var(data$IPM)
## [1] 26.52484
sd(data$IPM)
## [1] 5.150227
quantile(data$IPM)
##     0%    25%    50%    75%   100% 
## 53.420 71.080 73.180 74.345 83.080
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.6.1
ggplot(data, aes(x=IPM))+geom_histogram(binwidth=5, boundary=60, fill="pink", color="black")+scale_x_continuous(breaks=seq(60,85,by=5), limits=c(60,85))+labs(title="IPM di Indonesia", x="IPM", y="Freq")+theme_classic()+theme(axis.text.x=element_text(angle=0, hjust=0.5),plot.title=element_text(hjust=0.5, face="bold"))
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_bin()`).

ggplot(data, aes(x="", y=IPM))+geom_boxplot(fill="red")+labs(title="Boxplot IPM Indonesia 2024", x="", y="IPM")+theme_minimal()+theme(plot.title=element_text(hjust=0.5, face="bold"))

library(dplyr)
## Warning: package 'dplyr' was built under R version 4.6.1
## 
## 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)
## Warning: package 'tidyverse' was built under R version 4.6.1
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ lubridate 1.9.5     ✔ tibble    3.3.1
## ✔ purrr     1.2.2     ✔ tidyr     1.3.2
## ✔ readr     2.2.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)

Data_Kalimantan <- data %>%
  filter(str_starts(Provinsi, "KALIMANTAN"))

Data_Kalimantan
##             Provinsi   IPM
## 1   KALIMANTAN_BARAT 70.13
## 2  KALIMANTAN_TENGAH 72.73
## 3 KALIMANTAN_SELATAN 73.03
## 4   KALIMANTAN_TIMUR 78.83
## 5   KALIMANTAN_UTARA 73.02
ggplot(Data_Kalimantan, aes(x = Provinsi, y = IPM, fill = Provinsi)) +geom_col()+labs(title = "IPM Pulau Kalimantan Tahun 2024", x = "Provinsi", y = "IPM")+theme_minimal()+theme(axis.text.x = element_text(angle = 45, hjust = 1), plot.title = element_text(hjust = 0.5, face = "bold"), legend.position = "none")

# Contoh Soal 2

# Load library
library(ggplot2)
# Baca data
data = read.table(file.choose(), header = T, sep = "\t")
data
##         Nama_Produk  Kategori Penjualan
## 1             Beras   Sembako        35
## 2     Minyak_Goreng   Sembako        75
## 3        Gula_Pasir   Sembako        60
## 4        Mie_Instan   Makanan       150
## 5           Biskuit   Makanan        85
## 6          Susu_UHT   Makanan        70
## 7         The_Celup   Makanan        55
## 8       Sabun_Mandi Perawatan        90
## 9             Sampo Perawatan        65
## 10       Pasta_Gigi Perawatan        70
## 11 Sabun_Cuci_ Muka Perawatan        45
# Buat barplot dengan ggplot2
ggplot(data, aes(x = Nama_Produk, y = Penjualan, fill = Kategori))+geom_bar(stat = "identity")+labs(title = "Jumlah Penjualan Produk Toko MUTIA", x = "Produk", y = "Jumlah Penjualan")+theme_minimal()+theme(axis.text.x = element_text(angle = 45, hjust = 1), plot.title = element_text(hjust = 0.5, face = "bold"))

#Agregasi Data (Total Penjualan per Kategori)
total_penjualan<-aggregate(Penjualan ~ Kategori, data, sum)
total_penjualan
##    Kategori Penjualan
## 1   Makanan       360
## 2 Perawatan       270
## 3   Sembako       170
#Buat Barplot dengan ggplot2
ggplot(total_penjualan, aes(x=Kategori, y=Penjualan, fill=Kategori))+geom_bar(stat="identity")+labs(title="Jumlah Penjualan Produk per Kategori", x="Kategori", y="Jumlah Penjualan")+theme_minimal()+theme(axis.text.x=element_text(angle=45, hjust=1), plot.title=element_text(hjust=0.5, face="bold"))