I’m Briandamar Kencana a data enthusiast. I graduated with a bachelor’s degree in statistics. I have some work experience in the field of data, such as an internship at momobil.id, independent research and advisory Indonesia, staff data analyst at Alif Aza Asia and reserach fellow at Bank Indonesia, and then I continued my career as an analyst at a commercial bank in Indonesia. In addition, I have participated in several data training courses at Algoritma Data Science School, Coursera, Udemy and Dicoding.
The data presents the development of the spread of COVID-19 cases in Indonesia at the provincial level. This data is updated every 16:00 WIB after the press conference was completed by the Indonesian Government Spokesperson for COVID-19, Mr. Achmad Yurianto from the Indonesian Ministry of Health.
Covid-19 Data in Indonesia
In this article, I will visualize the data covid-19 that occurred in Indonesia. I took some parts of the plot that I made using FlexDashboard and Shiny as shown below:
Covid-19 Data in Indonesia
Data visualization is the representation of data or information in a graph, chart, or other visual format. It communicates relationships of the data with images. This is important because it allows trends and patterns to be more easily seen. With the rise of big data upon us, we need to be able to interpret increasingly larger batches of data
Goals : To visualize covid-19 data using line charts+animate, Barchart and leaflet. I will use ggplot, ggplotly, gganimate and plotly.
Before starting the visualization I will explain some of the packages that I use and the stages of prepocessing that I do.
I will use the following packages:
BNPB provides an API for me to be able to access covid-19 development data. The data provided is still in Json format, so it needs to be converted into a data frame and adjust the data type for each variable, then eliminate the data that contains NA.
library(httr)
library(jsonlite)
library(dplyr)
library(lubridate)
path_covid_harian <- "https://opendata.arcgis.com/datasets/db3be1cadcf44b6fa709274c12726c59_0.geojson"
data_covid_harian<-GET(url = path_covid_harian) %>%
content(as="text", encoding="UTF-8") %>%
fromJSON() %>%
.$features %>%
.$properties %>%
mutate(Tanggal=as_date(ymd_hms(Tanggal,tz = "Asia/Jakarta"))) %>%
select(Tanggal,Jumlah_Kasus_Kumulatif,Jumlah_Pasien_Sembuh, Jumlah_Pasien_Meninggal, Jumlah_pasien_dalam_perawatan) %>%
arrange(desc(Tanggal)) %>%
filter(!is.na(Jumlah_Kasus_Kumulatif))
DT::datatable(head(data_covid_harian,500),
options = list(
pageLength = 10,
scrollX = TRUE,
scrollY = "500px"),
caption = htmltools::tags$caption(
style = 'caption-side:bottom; text-align: right;',
'Data Source:', htmltools::em('https://bnpb-inacovid19.hub.arcgis.com')))
#Data_Corona_Provinsi
path_covid_provinsi<-"https://opendata.arcgis.com/datasets/0c0f4558f1e548b68a1c82112744bad3_0.geojson"
data_covid_provinsi<-GET(url = path_covid_provinsi) %>%
content(as="text", encoding="UTF-8") %>%
fromJSON() %>%
.$features %>%
.$properties %>%
rename("Kasus_Terkonfirmasi" = "Kasus_Posi",
"Sembuh" = "Kasus_Semb",
"Meninggal" = "Kasus_Meni",
"kode" = "Kode_Provi") %>%
filter(Provinsi != "Indonesia")
data_covid_provinsi$Provinsi<-toupper(data_covid_provinsi$Provinsi)
#Data Lat dan long Provinsi
path_covid_location<-"https://opendata.arcgis.com/datasets/a1273ba480af4d528931e0cab073478d_0.geojson"
data_covid_location<-GET(url = path_covid_location) %>%
content(as="text", encoding="UTF-8") %>%
fromJSON() %>%
.$features %>%
.$properties %>%
rename("Provinsi" = "provinsi",
"Kecamatan" = "kecamatan",
"Kabupaten" = "kabupaten",
"Kategori" = "kategori")
data_covid_location_unik<-data_covid_location %>%
select(Provinsi, lat,lon) %>%
group_by(Provinsi) %>%
summarise(lat = mean(lat),
lon = mean(lon))
data_covid_provinsi_location <- left_join(data_covid_provinsi, data_covid_location_unik, by="Provinsi")
DT::datatable(head(data_covid_provinsi_location,50),
options = list(
pageLength = 10,
scrollX = TRUE,
scrollY = "500px"),
caption = htmltools::tags$caption(
style = 'caption-side:bottom; text-align: right;',
'Data Source:', htmltools::em('https://bnpb-inacovid19.hub.arcgis.com')))
Here I will make a line chart using ggplot + animate to make a graphic animation, secondly I will use plotly to make a line chart hoverable.
Ok, I will start from line chart using ggplot from the number of covid-19 cumulative cases in Indonesia.
library(ggplot2)
library(ggrepel)
datalabel<-head(data_covid_harian[order(data_covid_harian$Tanggal, decreasing=TRUE),],1)
ggplot(data = data_covid_harian, mapping = aes(y = Jumlah_Kasus_Kumulatif,
x = Tanggal))+
geom_line(color="#fc0000") +
geom_area(fill = '#fc0000', alpha = .1) +
geom_point(color="red",size=1) +
geom_text_repel(data=datalabel, aes(label=Jumlah_Kasus_Kumulatif),color="#FFA500")+
labs(title = "Perkembangan Kasus Terkonfirmasi\nCovid-19",
caption = "Source: https://bnpb-inacovid19.hub.arcgis.com\nBriandamar Kencana",
x = "Tanggal",
y = "Kasus Terkonfirmasi\nAkumulatif") +
scale_x_date(date_labels = "%y-%m-%d") +
theme(text = element_text(color = "#444444"),
panel.background = element_rect(fill = '#450303'),
panel.grid.minor = element_line(color = '#4f0202'),
panel.grid.major = element_line(color = '#572027'),
plot.title = element_text(size = 15, colour = "#ef5252"),
plot.subtitle = element_text(size = 12),
plot.caption = element_text(colour="#2b0905", face = "italic",vjust = 1,hjust = 1),
axis.title.x = element_text(hjust = 0,size = 8),
axis.title.y = element_text(vjust =1,angle = 0, size=8),
plot.background = element_rect(fill = "transparent"))
Information
Ggplot is a static plot, we can`t hover to a certain value in the plot. Then I will add transition_reveal (Date) to make it animate.
library(gganimate)
data_covid_harian<-data_covid_harian[order(data_covid_harian$Tanggal, decreasing = F), ]
plot_Kasus_Terkonfirmasi<-ggplot(data = data_covid_harian, mapping = aes(y = Jumlah_Kasus_Kumulatif,
x = Tanggal))+
geom_line(color="#fc0000") +
geom_area(fill = '#fc0000', alpha = .1) +
geom_point(color="red",size=5) +
geom_text_repel(data=data_covid_harian, aes(label=Jumlah_Kasus_Kumulatif), direction="x",color="#FFA500")+
labs(title = "Perkembangan Kasus Terkonfirmasi\nCovid-19",
subtitle = "Tanggal: {frame_along}",
caption = "Source: https://bnpb-inacovid19.hub.arcgis.com\nBriandamar Kencana",
x = "Tanggal",
y = "Kasus Terkonfirmasi\nAkumulatif") +
scale_x_date(date_labels = "%y-%m-%d") +
theme(text = element_text(color = "#444444"),
panel.background = element_rect(fill = '#450303'),
panel.grid.minor = element_line(color = '#4f0202'),
panel.grid.major = element_line(color = '#572027'),
plot.title = element_text(size = 15, colour = "#ef5252"),
plot.subtitle = element_text(size = 12),
plot.caption = element_text(colour="#2b0905", face = "italic",vjust = 1,hjust = 1),
axis.title.x = element_text(hjust = 0,size = 8),
axis.title.y = element_text(vjust =1,angle = 0, size=8),
plot.background = element_rect(fill = "transparent")) +
transition_reveal(Tanggal)
plot_Kasus_Terkonfirmasi
Interpretation
Corona cases in Indonesia has increased very significantly, updated data on 20 July 2020 provide information that the number of cases that have occurred already 88214 cases
Next I will show how to make a line chart using plotly. The difference between ggplot andplotly is in the dynamic or static of a plot. Ggplot is a static plot, whereas plotly orggplotly is a dynamic plot because we can hover to a certain value in the plot.
library(plotly)
plot_ly(data = data_covid_harian,
x = ~Tanggal,
y = ~Jumlah_Kasus_Kumulatif,
name = 'Positif',
type = "scatter",
mode = 'lines',
line = list(color = "#2d3c45",
width = 4)) %>%
add_trace(y = ~Jumlah_pasien_dalam_perawatan,
name = 'Dalam Perawatan',
line = list(color = "#5b6605", width = 4)) %>%
add_trace(y = ~Jumlah_Pasien_Sembuh,
name = 'Sembuh',
line = list(color = "#024f3d", width = 4)) %>%
add_trace(y = ~Jumlah_Pasien_Meninggal,
name = 'Meninggal',
line = list(color = "#6e0202", width = 4)) %>%
layout(yaxis = list(title = "Jumlah Pasien"),
xaxis = list(title = "Tanggal"),
showlegend = FALSE,
hovermode = "compare") %>%
layout(plot_bgcolor='#f2dfdf') %>%
layout(paper_bgcolor='#f2dfdf')
Interpretation
Updated data on 20 July 2020 provide that information from 88214 cases, 4239 people died, 36998 people are still being treated, dan 46977 people were cured
I will show you how to use ggplotly on barchart. Barchart to be displayed are 6 provinces with the most confirmed corona cases.
library(glue)
Plot_Kasus_Terbanyak_Positif<-data_covid_provinsi %>%
select(Provinsi, Kasus_Terkonfirmasi) %>%
mutate(keterangan = glue("Positif : {Kasus_Terkonfirmasi}")) %>%
arrange(desc(Kasus_Terkonfirmasi)) %>%
head(6) %>%
ggplot(aes(x=Kasus_Terkonfirmasi, y=reorder(Provinsi, Kasus_Terkonfirmasi), text=keterangan))+geom_col(aes(fill = Kasus_Terkonfirmasi), show.legend = F) +
labs(x = NULL, y = NULL, title = "6 Provinsi Kasus Positif Terbanyak")+scale_fill_gradient(high = "#2d3c45", low = "#18374a")+scale_x_continuous(labels = function(x){paste0(x/1000, 'K')}) +
theme(legend.key = element_rect(fill="black"),
legend.background = element_rect(color="white", fill="#263238"),
plot.subtitle = element_text(size=6, color="white"),
panel.background = element_rect(fill="#dddddd"),
panel.border = element_rect(fill=NA),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(color="darkgrey", linetype=2),
panel.grid.minor.y = element_blank(),
plot.background = element_rect(fill="#263238"),
text = element_text(color="white"),
axis.text = element_text(color="white")
)
ggplotly(Plot_Kasus_Terbanyak_Positif, tooltip = "text")
Interpretation
Province with the most cases on date 20 July 2020 in the province of JAWA TIMUR with a total of 18308 cases
Leaflet is one of the most popular open-source JavaScript libraries for interactive maps. It’s used by websites ranging from The New York Times and The Washington Post to GitHub and Flickr, as well as GIS specialists like OpenStreetMap, Mapbox, and CartoDB.
This R package makes it easy to integrate and control Leaflet maps in R, so here I will use the library(leaflet) to make leaflet.
library(leaflet)
mytext <- paste(
" Provinsi ", data_covid_provinsi_location$Provinsi, "",
"Terkonfirmasi Positif :", data_covid_provinsi_location$Kasus_Terkonfirmasi,
sep=" ") %>%
lapply(htmltools::HTML)
data_covid_provinsi_location %>%
leaflet() %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
addCircles(lng = ~lon, lat = ~lat, weight = 1,
radius = ~(Kasus_Terkonfirmasi*10000000)^0.5, popup = ~Provinsi,
label = mytext, color = "red")
We can also change the appearance of the leaflet
mytext <- paste(
" Provinsi ", data_covid_provinsi_location$Provinsi, "",
"Terkonfirmasi Positif :", data_covid_provinsi_location$Kasus_Terkonfirmasi,
sep=" ") %>%
lapply(htmltools::HTML)
data_covid_provinsi_location %>%
leaflet() %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircles(lng = ~lon, lat = ~lat, weight = 1,
radius = ~(Kasus_Terkonfirmasi*10000000)^0.5, popup = ~Provinsi,
label = mytext, color = "red")
mytext <- paste(
" Provinsi ", data_covid_provinsi_location$Provinsi, "",
"Terkonfirmasi Positif :", data_covid_provinsi_location$Kasus_Terkonfirmasi,
sep=" ") %>%
lapply(htmltools::HTML)
data_covid_provinsi_location %>%
leaflet() %>%
addTiles() %>%
addProviderTiles(providers$Stamen.Toner) %>%
addCircles(lng = ~lon, lat = ~lat, weight = 1,
radius = ~(Kasus_Terkonfirmasi*10000000)^0.5, popup = ~Provinsi,
label = mytext, color = "red")
for details you can visit the following site: Leaflet for R.
I have already shown how to visualizations data using ggplot,ggplotly, plotly and animate on linechart and barchart. In addition, I have also shown how to use leaflets on covid-19 in Indonesia. So hopefully useful :)
A work by Briandamar Kencana
damarbrian@gmail.com