Dengue Data Hub
  • About
  • denguedatahub Installation
  • All Countries
  • Sri Lanka
  • USA
  • Singapore
  • China
  • Other Countries
  • Web Scraping Functions
  • Data Manipulation Functions
  • Interactive Data Explorer
  • Package News
  • Collaborate with us

On this page

  • Application of min_max function
    • Apply min-max transformation using min_max function

Data Manipulation Functions

Application of min_max function

Visualizing multiple time series can be a challenging task due to issues like overlapping and scale differences. One popular approach to visualize all time series in a single plot. This visualization can be useful in identifying the dominating districts in terms of dengue outbreaks.

library(tidyverse)
library(denguedatahub)
library(viridis)
library(lubridate)
library(patchwork)
srilanka_weekly_data$start.date <- as.Date(
  srilanka_weekly_data$start.date,
  format = "%d/%m/%Y"
)
srilanka_weekly_data$end.date <- as.Date(
  srilanka_weekly_data$end.date,
  format = "%d/%m/%Y"
)
p1 <- ggplot(srilanka_weekly_data, aes(color=cases, x=start.date, y=cases, group=district)) + 
  geom_line() + 
  scale_color_viridis(option='turbo') +
  scale_x_date(date_breaks = "1 year", date_labels = "%Y") +  ylab("Dengue Counts") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 

p2 <- ggplot(srilanka_weekly_data, aes(color=district, x=start.date, y=cases, group=district)) + 
  geom_line() + 
  scale_x_date(date_breaks = "1 year", date_labels = "%Y") +  ylab("Dengue Counts") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 
p1/p2

However, relying solely on the above plots can sometimes make it difficult to identify whether all districts have a similar pattern or not. For instance, it may not be immediately clear whether the peak periods of all districts are similar or not. To overcome this challenge, we can apply a min-max transformation to each series based on its minimum and maximum values. This can help us to compare the patterns across different districts more easily, and gain insights into the temporal dynamics of dengue outbreaks in Sri Lanka.

The min_max function with the parameter local=TRUE helps you easily normalize the series using the min-max transformation with respect to each series minimum and maximum value.

Apply min-max transformation using min_max function

Step 1: Apply min-max transformation

srilanka.minmax <- min_max(srilanka_weekly_data, 
      variable.to.minmax="cases" , local = TRUE, group.var="district")
srilanka.minmax
# A tibble: 25,766 × 9
# Groups:   district [26]
    year  week start.date end.date district    cases min.group max.group
   <dbl> <dbl> <date>     <date>   <chr>       <dbl>     <dbl>     <dbl>
 1  2006    52 NA         NA       Colombo        71         0      1972
 2  2006    52 NA         NA       Gampaha        12         0      2631
 3  2006    52 NA         NA       Kalutara       12         0       703
 4  2006    52 NA         NA       Kandy          20         0      1062
 5  2006    52 NA         NA       Matale          4         0       260
 6  2006    52 NA         NA       NuwaraEliya     1         0        85
 7  2006    52 NA         NA       Galle           1         0       332
 8  2006    52 NA         NA       Hambanthota     1         0       147
 9  2006    52 NA         NA       Matara         11         0       489
10  2006    52 NA         NA       Jaffna          0         0       931
# ℹ 25,756 more rows
# ℹ 1 more variable: minmax.group <dbl>

Step 2: Transformed series

p3 <- ggplot(srilanka.minmax, aes(colour=minmax.group, x=start.date, y=minmax.group, group=district)) + 
  geom_line() + 
  scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
  scale_color_viridis(option='turbo') + ylab("Year") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

p4 <- ggplot(srilanka.minmax, aes(colour=district, x=start.date, y=minmax.group, group=district)) + 
  geom_line() + 
  scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
   ylab("Year") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
p3/p4