input data

library(ggplot2)
library(readxl)
fabian <- read_xlsx("/Users/M.Fabian.R.D/Desktop/SEMESTER 4/visualisasi data/data_excell_terbaru.xlsx")
fabian
## # A tibble: 10 × 7
##       id tahun_data kabupaten_kota jumlah_ibuhamil_dpt_…¹ jumlah_ibuhamil_dpt_…²
##    <dbl>      <dbl> <chr>                           <dbl>                  <dbl>
##  1     1       2023 Kab.Cilacap                     27808                  26614
##  2     2       2023 Kab.Banyumas                    21336                  21797
##  3     3       2023 Kab.Purbaling…                  12607                  12521
##  4     4       2023 Kab.Banjarneg…                  12487                  11369
##  5     5       2023 Kab.Kebumen                     15593                  16289
##  6     6       2023 Kab.Purworejo                    7488                   7312
##  7     7       2023 Kab.Wonosobo                     9890                  10245
##  8     8       2023 Kab.Magelang                    15645                  15182
##  9     9       2023 Kab.Boyolali                    13307                  12924
## 10    10       2023 Kab.Klaten                      13449                  12908
## # ℹ abbreviated names: ¹​jumlah_ibuhamil_dpt_pelayanan_k_satu,
## #   ²​jumlah_ibuhamil_dpt_pelayanan_k_empat
## # ℹ 2 more variables: jumlah_ibuhamil_dpt_pelayanan_nifas <dbl>,
## #   jumlah_ibu_yg_bersalin_di_fasyankes <dbl>

histogram

perintah atau syntax untuk histogram sebagai berikut

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu))+
  geom_histogram() #jika menggunakan geom_hist langsung melihat sebaran data dengan defaultnya 30 kelas 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu))+
  geom_histogram(bins = 90) #untuk melihat sebaran lebih banyak 

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu))+
  geom_histogram(bins = 90, col = "skyblue") # menambahkan warna 

library(scales)
ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu))+
  geom_histogram(bins = 10, col = "black", fill = "lightgreen") +
  scale_x_continuous(labels = comma)+
  scale_y_continuous(labels = comma)+
  labs(x = "jumlah ibu hamil mendapatkan pelayanan",
       y = "frekuensi")

density plot

berikut ada beberapa cara untuk membuat data density

library(ggridges)
library(ggplot2)
ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu, y = jumlah_ibuhamil_dpt_pelayanan_k_empat))+
  geom_density_2d_filled()

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu))+
  geom_density()

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu))+
  geom_density(fill = "lightgreen", alpha = 0.7)+
  scale_x_continuous(labels = comma)+
  scale_y_continuous(labels = comma)+
  labs(x = "jumlah ibu hamil dapat pelayanan",
       y = "frekuensi")

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu, fill = kabupaten_kota, color = kabupaten_kota))+
  geom_density(alpha = 0.3)+
  scale_x_continuous(labels = comma)+
  scale_y_continuous(labels = comma)+
  labs(x = "jumlah ibu hamil dapat pelayanan")
## Warning: Groups with fewer than two data points have been dropped.
## Groups with fewer than two data points have been dropped.
## Groups with fewer than two data points have been dropped.
## Groups with fewer than two data points have been dropped.
## Groups with fewer than two data points have been dropped.
## Groups with fewer than two data points have been dropped.
## Groups with fewer than two data points have been dropped.
## Groups with fewer than two data points have been dropped.
## Groups with fewer than two data points have been dropped.
## Groups with fewer than two data points have been dropped.
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf
## Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
## -Inf

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu, fill = kabupaten_kota, y = kabupaten_kota))+
  geom_density_ridges2()+
  labs(x = "jumlah ibu hamil dapat pelayanan", y = "kabupaten/kota")
## Picking joint bandwidth of NaN

boxplot

berikut adalah beberapa syntax untuk membuat boxplot

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu))+
  geom_boxplot()

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu))+
  geom_boxplot()+
  coord_flip() # jikalau ingin vertikal 

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu, y = kabupaten_kota))+
  geom_boxplot() #untuk membandingkan sebaran harga warna berlian #dan untuk mengelompokkan 

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu, y = kabupaten_kota, fill = kabupaten_kota))+
  geom_boxplot() #untuk memberikan warna 

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu, y = kabupaten_kota, fill = kabupaten_kota))+
  geom_boxplot() +
  theme(legend.position = "none")

violin plot

berikut adalah beberapa syntax untuk violin plot

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu, y = "all"))+
  geom_violin(fill = "black", alpha = 0.5)+
  geom_boxplot(fill = "pink", width = 0.1)

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu, y = kabupaten_kota))+
  geom_violin(fill = "black", alpha = 0.5)+
  geom_boxplot(fill = "pink", width = 0.1) # untuk membuat banyak data terbagi menjadi beberapa grafik
## Warning: Groups with fewer than two datapoints have been dropped.
## ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Warning in max(data$density, na.rm = TRUE): no non-missing arguments to max;
## returning -Inf
## Warning: Computation failed in `stat_ydensity()`.
## Caused by error in `$<-.data.frame`:
## ! replacement has 1 row, data has 0

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu, y = "all"))+
  geom_violin(fill = "black", alpha = 0.5)+
  geom_boxplot(fill = "pink", width = 0.1)+
  scale_x_continuous(labels = comma)

QQ-PLOT

ggplot(data = fabian, aes(sample = jumlah_ibuhamil_dpt_pelayanan_k_satu))+
  stat_qq(col = "green", cex = 2)+
  stat_qq_line(col = "red", lwd = 0.6)

scatter plot

ggplot(data = fabian, mapping = aes(x = jumlah_ibuhamil_dpt_pelayanan_k_satu, y = jumlah_ibuhamil_dpt_pelayanan_k_empat , color = jumlah_ibuhamil_dpt_pelayanan_nifas))+
  geom_point()

Line chart Dasar

library(readxl)
library(ggplot2)

alibaba <- read_xlsx("//Users/M.Fabian.R.D/Desktop/SEMESTER 4/visualisasi data/Ali_Baba_Stock_Data.xlsx")
alibaba$Date <- as.Date(alibaba$Date)
head(alibaba)
## # A tibble: 6 × 7
##   Date       `Adj Close` Close  High   Low  Open    Volume
##   <date>           <dbl> <dbl> <dbl> <dbl> <dbl>     <dbl>
## 1 2014-09-19        90.7  93.9  99.7  89.9  92.7 271879400
## 2 2014-09-22        86.8  89.9  92.9  89.5  92.7  66657800
## 3 2014-09-23        84.2  87.2  90.5  86.6  88.9  39009800
## 4 2014-09-24        87.5  90.6  90.6  87.2  88.5  32088000
## 5 2014-09-25        85.9  88.9  91.5  88.5  91.1  28598000
## 6 2014-09-26        87.4  90.5  90.5  88.7  89.7  18340000
str(alibaba)
## tibble [2,617 × 7] (S3: tbl_df/tbl/data.frame)
##  $ Date     : Date[1:2617], format: "2014-09-19" "2014-09-22" ...
##  $ Adj Close: num [1:2617] 90.7 86.8 84.2 87.5 85.9 ...
##  $ Close    : num [1:2617] 93.9 89.9 87.2 90.6 88.9 ...
##  $ High     : num [1:2617] 99.7 92.9 90.5 90.6 91.5 ...
##  $ Low      : num [1:2617] 89.9 89.5 86.6 87.2 88.5 ...
##  $ Open     : num [1:2617] 92.7 92.7 88.9 88.5 91.1 ...
##  $ Volume   : num [1:2617] 2.72e+08 6.67e+07 3.90e+07 3.21e+07 2.86e+07 ...
colnames(alibaba)
## [1] "Date"      "Adj Close" "Close"     "High"      "Low"       "Open"     
## [7] "Volume"
ggplot(data = alibaba, mapping = aes(x = Date, y = Close)) + 
  geom_line() + 
  labs(x = "tanggal", y = "banyaknya kasus harian")

Area Plot

  • Untuk menambah estetika dapat dilakukan dengan mengarsir daerah di bawah plot deret waktu. Plot seperti ini disebut Area Plot. Hasilnya sebagai berikut.
ggplot(data = alibaba, aes(x=Date,y=Close)) +
  geom_line(lwd=1.2, col="darkgreen") +
  geom_area(fill="green", alpha=0.3)

labs(x="tanggal", x="banyaknya kasus harian")
## $x
## [1] "tanggal"
## 
## attr(,"class")
## [1] "labels"

Multiple Line Chart

  • Pada plot ini dapat digunakan untuk membandingkan trend data deret waktu pada beberapa wilayah/kelompok.
ggplot(data = alibaba, aes(x=Date)) +
  geom_line(aes(y=Close), lwd=1.2, col="blue") +
  geom_line(aes(y=Open), lwd=1.2, col="red") +
  labs(x="tanggal", y="banyaknya kasus harian") 

  • Untuk menambahkan nama kelompok digunakan perintah berikut.
ggplot(data = alibaba, aes(x=Date)) +
  geom_line(aes(y=Close), lwd=1.2, col="pink") +
  geom_line(aes(y=Low), lwd=0.1, col="blue") +
  xlim(min(alibaba$Date),max(alibaba$Date)+100) +
  geom_text(x=max(alibaba$Date),y=tail(alibaba$Close,1)+30,
            label="Close alibaba", size = 5,
            color="black", hjust=1) +
  geom_text(x=max(alibaba$Date),y=tail(alibaba$Open,1)+30,
            label="Open alibaba", size = 5,
            color="chocolate", hjust= 2) +
  labs(x="tanggal", x="banyaknya kasus harian")