Membuat Grafik Covid-19 (2)

RB Fajriya Hakim
6 min readApr 8, 2020

--

Menggunakan RStudio

Assalamuálaikum teman-teman yang suka data..

Masih dalam suasana prihatin akan wabah, namun tidak mengurangi semangat kita untuk berkreasi sesuai bidang kita, dalam artikel sebelumnya akan kita coba detailkan lagi. Silahkan buka RStudio dan jika belum install beberapa paket yang kita perlukan (sampai dengan membuat dashboard seperti artikel sebelumnya), maka silahkan install terlebih dahulu,

install.packages(c("devtools", "flexdashboard", "leaflet", "leafpop"))

kemudian kita akan menginstal package ‘coronavirus’ yang telah dibuat oleh Rami Krispin dan tersedia di https://github.com/RamiKrispin/coronavirus dengan perintah seperti berikut,

devtools::install_github("RamiKrispin/coronavirus")

agak sedikit memerlukan waktu, detailnya bisa dilihat lagi para artikel saya sebelumnya disini. Hasil instalasi nampak seperti berikut,

Jika telah berhasil maka panggilah package tersebut dengan perintah,

library(coronavirus)

kemudian bisa dilihat apa saja isi dari package coronavirus

search()
ls(2)

nampak seperti berikut,

jadi terlihat ada empat fungsi dan data yakni, “coronavirus”, “covid_iran”, “covid_south_korea” dan “update_datasets”,

jika kita ingin mengupdate data (data berasal dari the Johns Hopkins University Center for Systems Science and Engineering (JHU CCSE)) maka tinggal kita perintahkan,

update_datasets()

kemudian akan memerlukan waktu untuk mengupdate data real time, kemudian keluar sebentar dari R dan masuk kembali agar data terupdate.

Jika kita ingin melihat datanya maka ketik perintah,

coronavirus

maka akan muncul hasil seperti berikut,

jika ingin melihat struktur datanya maka gunakan perintah,

str(coronavirus)

akan nampak,


> str(coronavirus)
'data.frame': 58900 obs. of 7 variables:
$ Province.State: chr "" "" "" "" ...
$ Country.Region: chr "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
$ Lat : num 33 33 33 33 33 33 33 33 33 33 ...
$ Long : num 65 65 65 65 65 65 65 65 65 65 ...
$ date : Date, format: "2020-01-22" "2020-01-23" ...
$ cases : int 0 0 0 0 0 0 0 0 0 0 ...
$ type : chr "confirmed" "confirmed" "confirmed" "confirmed" ...
>

jika ingin melihat salahsatu variabel dalam data tersebut cukup gunakan tanda penghubung $, misal ingin dilihat tanggal datanya maka cukup ketikkan,

coronavirus$date

maka hasilnya,

jika ingin melihat tampilan data yang lebih bagus, bisa digunakan library berikut (bisa anda install terlebih dahulu dengan perintah install.packages(),

library(dplyr)
library(magrittr)

kemudian gunakan perintah (merujuk pada github Antoine Soetewey) berikut untuk tampilan data,

df <- coronavirus %>%
dplyr::filter(Country.Region == "Indonesia") %>%
dplyr::group_by(Country.Region, type) %>%
dplyr::summarise(total = sum(cases))

kemudian panggil

df

hasilnya adalah,

> df
# A tibble: 3 x 3
# Groups: Country.Region [1]
Country.Region type total
<chr> <chr> <int>
1 Indonesia confirmed 2491
2 Indonesia death 209
3 Indonesia recovered 192
>

jika ingin lebih bagus lagi maka gunakan perintah seperti berikut,

df <- coronavirus %>%
# dplyr::filter(date == max(date)) %>%
dplyr::filter(Country.Region == "Indonesia") %>%
dplyr::group_by(Country.Region, type) %>%
dplyr::summarise(total = sum(cases)) %>%
tidyr::pivot_wider(
names_from = type,
values_from = total
)

jika kita panggil “df”, maka akan menghasilkan

> df
# A tibble: 1 x 4
# Groups: Country.Region [1]
Country.Region confirmed death recovered
<chr> <int> <int> <int>
1 Indonesia 2491 209 192
>

maka nampak bahwa Country.Region adalah bertipe “chr” atau character kemudian type kasusnya “confirmed” yang bertipe “int” atau integer dan seterusnya.

Kemudian jika kita ingin melihat data harian dengan perintah seperti berikut,

df_harian <- coronavirus %>%
dplyr::filter(Country.Region == "Indonesia") %>%
dplyr::filter(date >= "2020-03-01") %>%
dplyr::group_by(date, type) %>%
dplyr::summarise(total = sum(cases, na.rm = TRUE)) %>%
tidyr::pivot_wider(
names_from = type,
values_from = total
) %>%
dplyr::arrange(date) %>%
dplyr::ungroup() %>%
dplyr::mutate(active = confirmed - death) %>%
dplyr::mutate(
confirmed_cum = cumsum(confirmed),
death_cum = cumsum(death),
active_cum = cumsum(active)
)

kemudian kita panggil “data_harian” akan memberikan hasil seperti berikut,

kemudian akan kita buat plot untuk data tersebut dengan perintah berikut,

confirmed_color <- "purple"
active_color <- "#1f77b4"
recovered_color <- "forestgreen"
death_color <- "red"
plotly::plot_ly(data = df_harian) %>%
plotly::add_trace(
x = ~date,
# y = ~active_cum,
y = ~confirmed_cum,
type = "scatter",
mode = "lines+markers",
name = "Confirmed",
line = list(color = active_color),
marker = list(color = active_color)
) %>%
plotly::add_trace(
x = ~date,
y = ~death_cum,
type = "scatter",
mode = "lines+markers",
name = "Death",
line = list(color = death_color),
marker = list(color = death_color)
) %>%
plotly::add_annotations(
x = as.Date("2020-03-02"),
y = 1,
text = paste("Kasus Pertama"),
xref = "x",
yref = "y",
arrowhead = 5,
arrowhead = 3,
arrowsize = 1,
showarrow = TRUE,
ax = -10,
ay = -80
) %>%
plotly::add_annotations(
x = as.Date("2020-03-11"),
y = 3,
text = paste("Kematian Pertama"),
xref = "x",
yref = "y",
arrowhead = 5,
arrowhead = 3,
arrowsize = 1,
showarrow = TRUE,
ax = -90,
ay = -90
) %>%
plotly::layout(
title = "",
yaxis = list(title = "Jumlah kumulatif banyak kasus"),
xaxis = list(title = "Tanggal"),
legend = list(x = 0.1, y = 0.9),
hovermode = "compare"
)

sehingga menghasilkan,

nah untuk tahap pertama ini telah selesai. Jika kita zoom, maka nampak seperti berikut,

kemudian jika kita inginkan untuk membandingkan jumlah kasus baru di beberapa negara dapat kita gunakan perintah berikut,

konfirmasi_harian <- coronavirus %>%
dplyr::filter(type == "confirmed") %>%
dplyr::filter(date >= "2020-02-25") %>%
dplyr::mutate(country = Country.Region) %>%
dplyr::group_by(date, country) %>%
dplyr::summarise(total = sum(cases)) %>%
dplyr::ungroup() %>%
tidyr::pivot_wider(names_from = country, values_from = total)

nampak seperti berikut,

kemudian buatlah perintah berikut,


konfirmasi_harian %>%
plotly::plot_ly() %>%
plotly::add_trace(
x = ~date,
y = ~Indonesia,
type = "scatter",
mode = "lines+markers",
name = "Indonesia"
) %>%
plotly::add_trace(
x = ~date,
y = ~Malaysia,
type = "scatter",
mode = "lines+markers",
name = "Malaysia"
) %>%
plotly::add_trace(
x = ~date,
y = ~Singapore,
type = "scatter",
mode = "lines+markers",
name = "Singapore"
) %>%
plotly::add_trace(
x = ~date,
y = ~Thailand,
type = "scatter",
mode = "lines+markers",
name = "Thailand"
) %>%
plotly::layout(
title = "",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Jumlah kasus positif baru"),
xaxis = list(title = "Tanggal"),
hovermode = "compare",
margin = list(
b = 10,
t = 10,
pad = 2
)
)

nampak seperti berikut,

dapat anda zoom untuk melihat lebih detil,

untuk negara lain dapat anda coba dengan menggantikan nama negara yang diinginkan.

Kita juga bisa melihat jumlah kematian dengan perintah seperti berikut,


kematian_harian <- coronavirus %>%
dplyr::filter(type == "death") %>%
dplyr::filter(date >= "2020-02-25") %>%
dplyr::mutate(country = Country.Region) %>%
dplyr::group_by(date, country) %>%
dplyr::summarise(total = sum(cases)) %>%
dplyr::ungroup() %>%
tidyr::pivot_wider(names_from = country, values_from = total)
kematian_harian %>%
plotly::plot_ly() %>%
plotly::add_trace(
x = ~date,
y = ~Indonesia,
type = "scatter",
mode = "lines+markers",
name = "Indonesia"
) %>%
plotly::add_trace(
x = ~date,
y = ~Malaysia,
type = "scatter",
mode = "lines+markers",
name = "Malaysia"
) %>%
plotly::add_trace(
x = ~date,
y = ~Singapore,
type = "scatter",
mode = "lines+markers",
name = "Singapore"
) %>%
plotly::add_trace(
x = ~date,
y = ~Thailand,
type = "scatter",
mode = "lines+markers",
name = "Thailand"
) %>%
plotly::layout(
title = "",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Jumlah kematian baru"),
xaxis = list(title = "Tanggal"),
hovermode = "compare",
margin = list(
b = 10,
t = 10,
pad = 2
)
)

hasilnya seperti berikut,

dengan hasil grafik

dari hasil tersebut kita tahu gambaran jumlah kasus positif baru dan jumlah kematian baru secara grafis (kajian lebih lanjut dengan melihat parameter lain, misal jumlah penduduk, luas wilayah dan sebagainya).

Dengan beberapa package yang telah diinstal anda bisa membuat dashboardnya seperti pada artikel saya sebelumnya.

Terima kasih, semoga bermanfaat dan semoga wabah ini segera berakhir, aamiin.

Referensi:

  1. https://github.com/AntoineSoetewey/coronavirus_dashboard
  2. https://github.com/RamiKrispin/coronavirus

--

--