Dengue Data Hub
  • About
  • denguedatahub Installation
  • All Countries
  • Sri Lanka
  • USA
  • Singapore
  • China
  • Other Countries
  • Web Scraping Functions
  • Data Manipulation Functions
  • Interactive Data Explorer
  • 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)
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: 23,882 × 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 2006-12-23 2006-12-29 Colombo        71         0      1972
 2  2006    52 2006-12-23 2006-12-29 Gampaha        12         0      2631
 3  2006    52 2006-12-23 2006-12-29 Kalutara       12         0       703
 4  2006    52 2006-12-23 2006-12-29 Kandy          20         0      1062
 5  2006    52 2006-12-23 2006-12-29 Matale          4         0       260
 6  2006    52 2006-12-23 2006-12-29 NuwaraEliya     1         0        85
 7  2006    52 2006-12-23 2006-12-29 Galle           1         0       332
 8  2006    52 2006-12-23 2006-12-29 Hambanthota     1         0       147
 9  2006    52 2006-12-23 2006-12-29 Matara         11         0       489
10  2006    52 2006-12-23 2006-12-29 Jaffna          0         0       931
# ℹ 23,872 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