bersatulawancovid <- c("cuci tangan", "pakai masker", "jaga jarak")
bersatulawancovid
## [1] "cuci tangan" "pakai masker" "jaga jarak"
Rekapitulasi data COVID-19 Indonesia tersedia dalam API publik yang beralamat di https://data.covid19.go.id/public/api/update.json.maka kita akan langsung mengambil data pada laman website tersebut.
library(httr)
resp <- GET ("https://data.covid19.go.id/public/api/update.json")
library(httr)
resp <- GET("https://data.covid19.go.id/public/api/update.json")
status_code (resp)
## [1] 200
dan sekarang kita akan mencoba mengetahui status permintaan dengan cara mengakses elemen dari resp. Status code tersebut tersedia dengan nama status_code dan dapat diakses dengan menggunakan operator $ pada obyek resp.
library(httr)
resp <- GET("https://data.covid19.go.id/public/api/update.json")
resp$status_code
## [1] 200
identical(resp$status_code, status_code(resp))
## [1] TRUE
library(httr)
resp <- GET("https://data.covid19.go.id/public/api/update.json")
cov_id_raw <- content(resp, as = "parsed", simplifyVector = TRUE)
length(cov_id_raw)
## [1] 2
names(cov_id_raw)
## [1] "data" "update"
cov_id_update <- cov_id_raw$update
sekarang kita akan menganalisa data diantaranya :
Kapan tanggal pembaharuan data penambahan kasus? Berapa jumlah penambahan kasus sembuh? Berapa jumlah penambahan kasus meninggal? Berapa jumlah total kasus positif hingga saat ini? Berapa jumlah total kasus meninggal hingga saat ini?
lapply(cov_id_update, names)
## $penambahan
## [1] "jumlah_positif" "jumlah_meninggal" "jumlah_sembuh" "jumlah_dirawat"
## [5] "tanggal" "created"
##
## $harian
## [1] "key_as_string" "key" "doc_count"
## [4] "jumlah_meninggal" "jumlah_sembuh" "jumlah_positif"
## [7] "jumlah_dirawat" "jumlah_positif_kum" "jumlah_sembuh_kum"
## [10] "jumlah_meninggal_kum" "jumlah_dirawat_kum"
##
## $total
## [1] "jumlah_positif" "jumlah_dirawat" "jumlah_sembuh" "jumlah_meninggal"
cov_id_update$penambahan$tanggal
## [1] "2021-02-19"
cov_id_update$penambahan$jumlah_sembuh
## [1] 10783
cov_id_update$penambahan$jumlah_meninggal
## [1] 183
cov_id_update$total$jumlah_positif
## [1] 1263299
cov_id_update$total$jumlah_meninggal
## [1] 34152
sebelum melangkah lebih jauh mari kita jawab pertanyaan - pertanyaan berikut :
Berapa jumlah total kasus COVID-19 di Jawa Timur? Berapa persentase kematian akibat COVID-19 di Jawa Timur? Berapa persentase tingkat kesembuhan dari COVID-19 di Jawa Timur?
library(httr)
resp_jatim <- GET("https://data.covid19.go.id/public/api/prov_detail_JAWA_TIMUR.json")
cov_jatim_raw <- content(resp_jatim, as = "parsed", simplifyVector = TRUE)
names(cov_jatim_raw)
## [1] "last_date" "provinsi" "kasus_total"
## [4] "kasus_tanpa_tgl" "kasus_dengan_tgl" "meninggal_persen"
## [7] "meninggal_tanpa_tgl" "meninggal_dengan_tgl" "sembuh_persen"
## [10] "sembuh_tanpa_tgl" "sembuh_dengan_tgl" "list_perkembangan"
## [13] "data"
cov_jatim_raw$kasus_total
## [1] 122807
cov_jatim_raw$meninggal_persen
## [1] 6.674701
cov_jatim_raw$sembuh_persen
## [1] 84.87627
Informasi umum mengenai COVID-19 di Jawa Timur telah Anda dapatkan. Namun informasi akan lebih lengkap jika Anda memiliki data perkembangan COVID-19 dari waktu ke waktu, apakah Anda setuju?
cov_jatim <- cov_jatim_raw$list_perkembangan
str(cov_jatim)
## 'data.frame': 335 obs. of 9 variables:
## $ tanggal : num 1.58e+12 1.58e+12 1.58e+12 1.58e+12 1.58e+12 ...
## $ KASUS : int 2 0 0 0 0 0 0 0 8 7 ...
## $ MENINGGAL : int 0 0 0 0 0 0 1 2 0 0 ...
## $ SEMBUH : int 0 1 0 0 0 1 0 2 2 0 ...
## $ DIRAWAT_OR_ISOLASI : int 2 -1 0 0 0 -1 -1 -4 6 7 ...
## $ AKUMULASI_KASUS : int 2 2 2 2 2 2 2 2 10 17 ...
## $ AKUMULASI_SEMBUH : int 0 1 1 1 1 2 2 4 6 6 ...
## $ AKUMULASI_MENINGGAL : int 0 0 0 0 0 0 1 3 3 3 ...
## $ AKUMULASI_DIRAWAT_OR_ISOLASI: int 2 1 1 1 1 0 -1 -5 1 8 ...
head(cov_jatim)
## tanggal KASUS MENINGGAL SEMBUH DIRAWAT_OR_ISOLASI AKUMULASI_KASUS
## 1 1.584490e+12 2 0 0 2 2
## 2 1.584576e+12 0 0 1 -1 2
## 3 1.584662e+12 0 0 0 0 2
## 4 1.584749e+12 0 0 0 0 2
## 5 1.584835e+12 0 0 0 0 2
## 6 1.584922e+12 0 0 1 -1 2
## AKUMULASI_SEMBUH AKUMULASI_MENINGGAL AKUMULASI_DIRAWAT_OR_ISOLASI
## 1 0 0 2
## 2 1 0 1
## 3 1 0 1
## 4 1 0 1
## 5 1 0 1
## 6 2 0 0
Setelah mengekstrak dan mengamati cov_jatim, Anda menemukan beberapa kejanggalan pada data tersebut. Diantaranya adalah kejanggalan data pada kolom tanggal dan format penulisan kolom yang tidak konsisten. Sekarang Anda akan mencoba melakukan beberapa tahapan untuk menjinakan data tersebut sehingga dapat diolah dan dianalisis dengan lebih mudah. sebagai berikut :
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
new_cov_jatim <-
cov_jatim %>%
select(-contains("DIRAWAT_OR_ISOLASI")) %>%
select(-starts_with("AKUMULASI")) %>%
rename(
kasus_baru = KASUS,
meninggal = MENINGGAL,
sembuh = SEMBUH
) %>%
mutate(
tanggal = as.POSIXct(tanggal / 1000, origin = "1970-01-01"),
tanggal = as.Date(tanggal)
)
str(new_cov_jatim)
## 'data.frame': 335 obs. of 4 variables:
## $ tanggal : Date, format: "2020-03-18" "2020-03-19" ...
## $ kasus_baru: int 2 0 0 0 0 0 0 0 8 7 ...
## $ meninggal : int 0 0 0 0 0 0 1 2 0 0 ...
## $ sembuh : int 0 1 0 0 0 1 0 2 2 0 ...
library(ggplot2)
library(hrbrthemes)
ggplot(new_cov_jatim, aes(tanggal, sembuh)) +
geom_col(fill = "olivedrab2") +
labs(
x = NULL,
y = "Jumlah kasus",
title = "Kasus Harian Sembuh Dari COVID-19 di Jawa Timur",
caption = "Sumber data: covid.19.go.id"
) +
theme_ipsum(
base_size = 13,
plot_title_size = 21,
grid = "Y",
ticks = TRUE
) +
theme(plot.title.position = "plot")
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
library(ggplot2)
library(hrbrthemes)
ggplot(new_cov_jatim, aes(tanggal, meninggal)) +
geom_col(fill = "darkslategray4") +
labs(
x = NULL,
y = "Jumlah kasus",
title = "Kasus Harian Meninggal Akibat COVID-19 di Jawa Timur",
caption = "Sumber data: covid.19.go.id"
) +
theme_ipsum(
base_size = 13,
plot_title_size = 21,
grid = "Y",
ticks = TRUE
) +
theme(plot.title.position = "plot")
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
Setelah mengamati grafik perkembangan kasus Anda menyadari bahwa terjadi fluktuasi pertambahan kasus harian. Dilandasi hal tersebut Anda kemudian ingin mencoba mengamati bagaimana perkembangan kasus dalam rentang waktu pekanan. Bagaimanakah caranya? Anda dapat dengan mudah bekerja dengan data tanggal apabila menggunakan paket lubridate. Adapun yang akan digunakan untuk mengekstrak informasi pekan dalam satu tahun adalah fungsi week().sebagai berikut :
library(dplyr)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
cov_jatim_pekanan <- new_cov_jatim %>%
count(
tahun = year(tanggal),
pekan_ke = week(tanggal),
wt = kasus_baru,
name = "jumlah"
)
glimpse(cov_jatim_pekanan)
## Rows: 49
## Columns: 3
## $ tahun <dbl> 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020...
## $ pekan_ke <dbl> 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26...
## $ jumlah <int> 2, 41, 102, 279, 129, 253, 303, 483, 712, 1552, 1185, 1332...
Pertanyaan baru muncul di benak Anda setelah melakukan inspeksi terhadap data cov_jatim_pekanan tersebut: "Apakah pekan ini lebih baik dari pekan kemarin?".
Demi menjawab hal tersebut Anda melakukan kalkulasi sederhana dengan tahapan berikut:
Membuat kolom baru yang berisi jumlah kasus baru dalam satu pekan sebelumnya. Kolom ini diberi nama "jumlah_pekanlalu". Mengganti nilai NA pada kolom "jumlah_pekanlalu" dengan nilai 0 Melakukan komparasi antara kolom "jumlah" dengan kolom "jumlah_pekanlalu". Hasil komparasi ini disimpan dalam kolom baru dengan nama "lebih_baik", isinya adalah TRUE apabila jumlah kasus baru pekan ini lebih rendah dibandingkan jumlah kasus pekan lalu
library(dplyr)
cov_jatim_pekanan <-
cov_jatim_pekanan %>%
mutate(
jumlah_pekanlalu = dplyr::lag(jumlah, 1),
jumlah_pekanlalu = ifelse(is.na(jumlah_pekanlalu), 0, jumlah_pekanlalu),
lebih_baik = jumlah < jumlah_pekanlalu
)
glimpse(cov_jatim_pekanan)
## Rows: 49
## Columns: 5
## $ tahun <dbl> 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 20...
## $ pekan_ke <dbl> 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24...
## $ jumlah <int> 2, 41, 102, 279, 129, 253, 303, 483, 712, 1552, 11...
## $ jumlah_pekanlalu <dbl> 0, 2, 41, 102, 279, 129, 253, 303, 483, 712, 1552,...
## $ lebih_baik <lgl> FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FA...
library(ggplot2)
library(hrbrthemes)
ggplot(cov_jatim_pekanan, aes(pekan_ke, jumlah, fill = lebih_baik)) +
geom_col(show.legend = FALSE) +
scale_x_continuous(breaks = 9:29, expand = c(0, 0)) +
scale_fill_manual(values = c("TRUE" = "seagreen3", "FALSE" = "salmon")) +
labs(
x = NULL,
y = "Jumlah kasus",
title = "Kasus Pekanan Positif COVID-19 di Jawa Timur",
subtitle = "Kolom hijau menunjukan penambahan kasus baru lebih sedikit dibandingkan satu pekan sebelumnya",
caption = "Sumber data: covid.19.go.id"
) +
theme_ipsum(
base_size = 13,
plot_title_size = 21,
grid = "Y",
ticks = TRUE
) +
theme(plot.title.position = "plot")
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
Ada yang akhirnya sembuh, namun tak sedikit pula yang meninggal akibat COVID-19. Sementara itu penambahan kasus baru terus terjadi di masyarakat. Hal ini mungkin memicu pertanyaan lain di diri Anda: "Hingga saat ini ada berapa kasus yang masih aktif?". Aktif dalam artian sedang dalam perawatan atau isolasi.
library(dplyr)
cov_jatim_akumulasi <-
new_cov_jatim %>%
transmute(
tanggal,
akumulasi_aktif = cumsum(kasus_baru) - cumsum(sembuh) - cumsum(meninggal),
akumulasi_sembuh = cumsum(sembuh),
akumulasi_meninggal = cumsum(meninggal)
)
tail(cov_jatim_akumulasi)
## tanggal akumulasi_aktif akumulasi_sembuh akumulasi_meninggal
## 330 2021-02-10 8998 102399 8033
## 331 2021-02-11 9611 102803 8060
## 332 2021-02-12 9715 103400 8135
## 333 2021-02-13 9948 103708 8154
## 334 2021-02-14 10118 104031 8177
## 335 2021-02-15 10342 104222 8194
library(ggplot2)
ggplot(data = cov_jatim_akumulasi, aes(x = tanggal, y = akumulasi_aktif)) +
geom_line()
Kesimpulan apa yang dapat Anda tarik dari grafik tersebut?
library(ggplot2)
ggplot(data = cov_jatim_akumulasi, aes(x = tanggal)) +
geom_line(aes(x = tanggal, y = akumulasi_aktif), color = "blue") +
geom_line(aes(x = tanggal, y = akumulasi_sembuh), color = "green") +
geom_line(aes(x = tanggal, y = akumulasi_meninggal), color = "red")
library(dplyr)
library(tidyr)
dim(cov_jatim_akumulasi)
## [1] 335 4
cov_jatim_akumulasi_pivot <-
cov_jatim_akumulasi %>%
gather(
key = "kategori",
value = "jumlah",
-tanggal
) %>%
mutate(
kategori = sub(pattern = "akumulasi_", replacement = "", kategori)
)
dim(cov_jatim_akumulasi_pivot)
## [1] 1005 3
glimpse(cov_jatim_akumulasi_pivot)
## Rows: 1,005
## Columns: 3
## $ tanggal <date> 2020-03-18, 2020-03-19, 2020-03-20, 2020-03-21, 2020-03-2...
## $ kategori <chr> "aktif", "aktif", "aktif", "aktif", "aktif", "aktif", "akt...
## $ jumlah <int> 2, 1, 1, 1, 1, 0, -1, -5, 1, 8, 19, 28, 28, 28, 34, 31, 79...
cov_jatim_akumulasi_pivot <-
cov_jatim_akumulasi %>%
pivot_longer(
cols = -tanggal,
names_to = "kategori",
names_prefix = "akumulasi_",
values_to = "jumlah"
)
dim(cov_jatim_akumulasi_pivot)
## [1] 1005 3
glimpse(cov_jatim_akumulasi_pivot)
## Rows: 1,005
## Columns: 3
## $ tanggal <date> 2020-03-18, 2020-03-18, 2020-03-18, 2020-03-19, 2020-03-1...
## $ kategori <chr> "aktif", "sembuh", "meninggal", "aktif", "sembuh", "mening...
## $ jumlah <int> 2, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 2, 0, -1, ...
library(ggplot2)
library(hrbrthemes)
ggplot(cov_jatim_akumulasi_pivot, aes(tanggal, jumlah, colour = (kategori))) +
geom_line(size = 0.9) +
scale_y_continuous(sec.axis = dup_axis(name = NULL)) +
scale_colour_manual(
values = c(
"aktif" = "salmon",
"meninggal" = "darkslategray4",
"sembuh" = "olivedrab2"
),
labels = c("Aktif", "Meninggal", "Sembuh")
) +
labs(
x = NULL,
y = "Jumlah kasus akumulasi",
colour = NULL,
title = "Dinamika Kasus COVID-19 di Jawa Timur",
caption = "Sumber data: covid.19.go.id"
) +
theme_ipsum(
base_size = 13,
plot_title_size = 21,
grid = "Y",
ticks = TRUE
) +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = "top"
)
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database