Town X Dashboard

Column

Chart- Population Pyramid

Column

Chart-Known Sickness

Chart- Current Sickness (symptoms)

Row

Chart - Sought Health Facility

Chart - Main Percieved community health problem (Main_PCHP)

Pivot Table

Chart - Pivot Table: Sex by Age distribution of household members in town X, country X, 2025-07-14

District 1 Dashboard

Column

Chart -1.1 Sex

Column

Chart-1.2 Known Sickness

Chart-1.3 Current Sickness (symptoms)

Row

Chart -1.4 Sought Health Facility

Chart - 1.5 Main Percieved community health problem (Main_PCHP)

District 2 Dashboard

Column

Chart - 2.1 Sex

Column

Chart-2.2 Known Sickness

Chart-2.3 Current Sickness (symptoms)

Row

Chart -2.4 Sought Health Facility

Chart - 2.5 Main Percieved community health problem (Main_PCHP)

District 3 Dashboard

Column

Chart -3.1 Sex

Column

Chart-3.2 Known Sickness

Chart-3.3 Current Sickness (symptoms)

Row

Chart -3.4 Sought Health Facility

Chart - 3.5 Main Percieved community health problem (Main_PCHP)

Data Table

Chart - Data Table of My Hypothetical Community Health Data of Town-X with its three Districts (named as 1, 2 and 3), Country x, 2025-07-14

References

Column

References

1- The Graph Course. EPIREP_EN_parameterizing_reports.Rmd, Accessed on April 30, 2025: (https://thegraphcourses.org/courses/epirep/topics/parametrizing-reports)

2- Batra N., Spina A., Blomquiist P., et al. The Epidemiologist R Handbook, Sep 18, 2024. https://www.epirhandbook.com/en/new_pages/flexdashboard.html

3- https://www.youtube.com/watch?v=_a4S4tq62OE

4- https://www.youtube.com/watch?v=ef8qvUeopN4

Contact Me

Column

Contact Me

---
title: "Simple Community Health Dashboard (SCH_Dashboard)"
author: "Chernet Hailu Mengesha"
date: "`r Sys.Date()`"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
    theme: yeti
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)

if(require(pacman)) install.packages("pacman")
pacman::p_load(knitr, here, rmarkdown,flexdashboard, tidyverse, DT, scales, plotly, patchwork,  readr, stringr, forcats, memisc, pyramid, janitor, paletteer, ggsci, pwalk, rpivotTable, highcharter, htmltools, Viridis)

## Define-specific version of date()
date1<- format(Sys.time(), "%b %d,%Y")

# Define district names
District<-c("1", "2", "3")
# Load the dataset
Hypottowndata <- read_csv("../EpidBioRM/Hypottowndata.csv")
```

Sidebar {.sidebar}
==============================

### An Example of Simple Community Health (SCH) Dashboard (For Exercise)

The SCH_Dashboard is a households based community health data dashboard.

Please try to review this dashboard and let's work as a team for its improvement. You can get my address by clicking on the "Contact Me" menu.

   *The best comes as a result of collaborative endeavor!*
   
# Town X Dashboard

## Column {.tableset data-width=500}

### Chart- Population Pyramid

```{r, pyramidtownstep1 }
Hypottowndata1 <- Hypottowndata %>% mutate(Agegr = cut(Ageyrc, breaks = c(0, 4, 9, 14, 19, 24, 29, 34, 39, 44, 49, 54,59, 64, Inf),
                      labels = c('0-4', '5-9', '10-14', '15-19', '20-24', '25-29', '30-34', '35-39', '40-44', '45-49','50-54', '55-59', '60-64', '>64')))

```

```{r, pyramidtownstep2}
# To make town population pyramid
TOWnAgeSexd<-Hypottowndata1 %>% count(Agegr, Sex)
```

```{r, pyramidtownstep3}
# Create new subset
pyramid_data <- 
  Hypottowndata1 %>% 
  
  # Count total cases by age group and gender
  count(Agegr, Sex, name = "total") %>%  
  
  # Create new columns for x-axis values on the plot
  mutate(
    # New column with axis values - convert male counts to negative
    axis_counts = ifelse(Sex == "Male", -total, total),
    # New column for percentage axis values
    axis_percent = round(100 * (axis_counts / nrow(Hypottowndata1)), 
                         digits = 1))
```


```{r, pyramidtownstep4}
Hypottown_pyramid <- 
  ggplot() +
 
  geom_col(data = pyramid_data, #specify data to graph
           aes(
             x = Agegr,    # indicate x variable
             y = axis_counts,  # indicate NEGATED y variable
             fill = Sex))  +   # fill by sex
  theme_light() +
  coord_flip()
```

```{r, pyramidtownstep5}
max_count <- max(pyramid_data$total)
```

```{r, pyramidtownstep6}
custom_axes <- 

# Use previous graph
  Hypottown_pyramid +
  
# Adjust y-axis (total count)  
  scale_y_continuous(

    # Specify limit of y-axis using max value and making positive and negative
    limits = c(-max_count, max_count),
    
    # Specify the spacing between axis labels
    breaks = scales::breaks_width(4),
    
    # Make axis labels absolute so male labels appear positive
    labels = abs)
```

```{r, pyramidtownstep7}
custom_labels <- 
  
# Start with previous demographic pyramid
  custom_axes +
  
# Adjust the labels
  labs(
    title = paste("Town X households by Age and Sex", date1),
    subtitle = "Analysis of a Hypothetical Community Health Data",
    x = "Age Group",
    y = "Count", 
    fill = "Sex",
    caption = stringr::str_glue("Data are from a Hypothetical data for Exercise \nn = {nrow(Hypottowndata1)}"))
```

```{r, pyramidtownstep8}
custom_color_theme <- 
  
# Use previous graph
  custom_labels +
  
# Designate colors and legend labels manually
  scale_fill_manual(
    
    # Select color of sex fill
    values = c("Female" = "lightblue",
               "Male" = "darkblue"))+
    
# Adjust theme settings
  theme(
    axis.line = element_line(colour = "black"), # make axis line black
    plot.title = element_text(hjust = 0.5),     # center title
    plot.subtitle = element_text(hjust = 0.5),  # center subtitle
    plot.caption = element_text(hjust = 0,      # format caption text
                                size = 11, 
                                face = "italic")) 
 print(custom_color_theme)
```

## Column {data-width=500}

### Chart-Known Sickness {.no-padding}

```{r, Known_Sicknessstep1}
Sumbydistkndxd <- Hypottowndata %>% group_by(District, Known_Sick) %>% summarise(n = n())
```
```{r, Known_Sicknessstep2}
Percbydistkndxd  <- Sumbydistkndxd %>%
mutate(percentage = round((n / sum(n))*100, 1))
```
```{r, Known_Sicknessstep3}
#Group Bar Chart
Known_Sickness_Chart<- ggplot(Percbydistkndxd, aes(x = District, y = percentage, fill = Known_Sick)) +
  geom_col(position = "dodge") +
  labs(title = paste("Town X Household Members' with Known Sickness by district, Country X", date1),
       subtitle = stringr::str_glue("Analysis of a Hypothetical Community Health Data for Exercise \nn = {nrow(Hypottowndata1)} (Hypottowndata.csv)"),
       x= "District",
       y= "Household Members, %") +
  theme(axis.text.x = element_text(angle = 90)) 

Known_Sickness_Chart
```

### Chart- Current Sickness (symptoms){.no-padding}

```{r, Current_Sicknessstep1}
Sumbydistcurskd <- Hypottowndata %>% group_by(District, current_sick) %>% summarise(n = n())
```
```{r, Current_Sicknessstep2}
Percbydistcurskd  <- Sumbydistcurskd %>%
mutate(percentage = round((n / sum(n))*100, 1))
```
```{r, Current_Sicknessstep3}
#Group Bar Chart
Current_Sickness_Chart<- ggplot(Percbydistcurskd, aes(x = District, y = percentage, fill = current_sick)) +
  geom_col(position = "dodge") +
  labs(title = paste("Town-X Household Members' with Current Sickness(Symtom) by district, Country X", date1),
       subtitle = stringr::str_glue("Analysis of a Hypothetical Community Health Data for Exercise \nn = {nrow(Hypottowndata1)} (Hypottowndata.csv)"),
       x= "District",
       y= "Household Members, %") +
  theme(axis.text.x = element_text(angle = 90))

Current_Sickness_Chart
```

## Row
### Chart - Sought Health Facility

```{r}
distsouhfild <-Hypottowndata1 %>% filter(current_sick != "No")
```
```{r,Sought_hfstep2}
Sumbydistsouhfd <-distsouhfild %>% group_by(District, sought_hf) %>% summarise(n=n())
```
```{r,Sought_hfstep3}
Percbydistsouhfd  <- Sumbydistsouhfd %>%
  mutate(percentage = round((n / sum(n))*100, 1))
```
```{r,Sought_hftep4}
#Group Bar Chart
Sought_hf_Chart<- ggplot(Percbydistsouhfd, aes(x = District, y = percentage, fill = sought_hf)) +
  geom_col(position = "dodge") +
  labs(title = paste("Town X Household Members Sought Health Facility for the Current Sickness by district, Country X", date1),
       subtitle = stringr::str_glue("Analysis of a Hypothetical Community Health Data for Exercise \nn = {nrow(distsouhfild)} (Hypottowndata.csv)"),
       x= "District",
       y= "Household Members, %") +
  theme(axis.text.x = element_text(angle = 90)) +
  coord_flip()
Sought_hf_Chart
```

### Chart - Main Percieved community health problem (Main_PCHP)

```{r}
Sumbydistchpfild <-Hypottowndata %>% filter(Relation_to_hhh == "Household Head")
```
```{r, Main_PCHPstep2}
Sumbydistchpd <-Sumbydistchpfild %>% group_by(District, Main_PCHP) %>% summarise(n=n())
```
```{r,, Main_PCHPstep3 }
Percbydistchpd  <- Sumbydistchpd %>%
  mutate(percentage = round((n / sum(n))*100, 1)) %>%
  arrange(by=District, desc(percentage))
```
```{r, Main_PCHPstep4}
#Group Bar Chart
Main_PCHP_Chart <-
  ggplot(Percbydistchpd, aes(x = District, y = percentage,
                           fill = Main_PCHP)) +
  geom_col(position = "dodge") +
  labs(title = paste("Town X, District Household Members'Main Perceived Community Health Problem, Country X", date1),
       subtitle = stringr::str_glue("Analysis of a Hypothetical Community Health Data for Exercise \nn = {nrow(Sumbydistchpfild)} (Hypottowndata.csv)"),
       x= "District",
       y= "Household Members, %") +
  theme(axis.text.x = element_text(angle = 90)) +
  coord_flip()
Main_PCHP_Chart
```

# Pivot Table

### Chart - Pivot Table: Sex by Age distribution of household members in town X, country X, `r Sys.Date()` 

```{r, Pivot Table-Sex by Age}
# Pivot Table of Sex by and Age distribution of household members in town X

pt1<- (rpivotTable(Hypottowndata1,
            aggrigatorName = "Count",
            cols= "Agegr",
            rows= "Sex",
            renderName= "Heatmap"))
pt1

```

# District 1 Dashboard

## Column {.tableset data-width=500}

### Chart -1.1 Sex

```{r}
crsexpie <- function(district_name) {

subtowndata0 <- subset(Hypottowndata1, District == district_name)

Sumbysexdata0 <- subtowndata0 %>% group_by(Sex) %>% summarise(n = n())

Percentage_data0 <- Sumbysexdata0 %>% 
mutate(percentage = round((n / sum(n))*100, 1))

percentage_pie0 <-
  ggplot(Percentage_data0, aes(x = " ", y=percentage,fill=Sex)) +
  geom_col() +
  coord_polar(theta = "y")+
  geom_text(aes(label = paste(percentage, "\ %")),
            position = position_stack(vjust = 0.5), # Center the label
            color = "white",
            fontface = "bold") +
  labs(title = paste("Sex of persons in the households of district", district_name, "town X, country X", date1),
       subtitle = stringr::str_glue("Analysis of data \nn = {nrow(subtowndata0)} from a hypothetical data (hypottowndata)")) +
  theme_void()+
  scale_fill_viridis_d()
}
```

```{r, crsexpie1}
#create plot for the district
plots<- lapply(District[[1]], crsexpie)
#Print the plot
for(i in seq_along(plots)){print(plots[[1]])
}
```

## Column {data-width=500}

### Chart-1.2 Known Sickness {.no-padding}

```{r, crKnSbarch}
crKnSbarch <- function(district_name) {

subtowndata0 <- subset(Hypottowndata1, District == district_name)


Sumbydistkndxd0 <- subtowndata0 %>% group_by(Known_Sick) %>% summarise(n = n())

Percbydistkndxd0  <- Sumbydistkndxd0 %>%
mutate(percentage = round((n / sum(n))*100, 1))

#Group Bar Chart
Known_Sickness_Chart<- ggplot(Percbydistkndxd0, aes(x = " " , y = percentage, fill = Known_Sick)) +
  geom_col(position = "dodge") +
  labs(title = paste("District", district_name, "Household Members' with Known Sickness, Town X, Country X", date1),
       subtitle = stringr::str_glue("Analysis of a Hypothetical Community Health Data for Exercise \nn = {nrow(subtowndata0)} (Hypottowndata.csv)"),
       x= "Known_Sickness",
       y= "Household Members, %") +
   theme(axis.text.x = element_text(angle = 90)) 
}
```

```{r, crKnSbarch1}
#create bchart for the district
bcharts<- lapply(District[[1]], crKnSbarch)
#Print the bchart
for(i in seq_along(bcharts)){print(bcharts[[1]])
}
```

### Chart-1.3 Current Sickness (symptoms){.no-padding}

```{r, curSbarch}
curSbarch <- function(district_name) {

subtowndata0 <- subset(Hypottowndata1, District == district_name)


Sumbydistcurskd0 <- subtowndata0 %>% group_by(current_sick) %>% summarise(n = n())

Percbydistcurskd0  <- Sumbydistcurskd0 %>%
mutate(percentage = round((n / sum(n))*100, 1))

#Group Bar Chart
Current_Sickness_Chart<- ggplot(Percbydistcurskd0, aes(x = " ", y = percentage, fill = current_sick)) +
  geom_col(position = "dodge") +
  labs(title = paste("District", district_name, "Household Members' with Current Sickness, Town X, Country X", date1),
       subtitle = stringr::str_glue("Analysis of a Hypothetical Community Health Data for Exercise \nn = {nrow(subtowndata0)} (Hypottowndata.csv)"),
       x= paste("District", district_name),
       y= "Household Members, %") +
  theme(axis.text.x = element_text(angle = 90)) +
  coord_flip()
}
```


```{r, curSbarch1}
#create bchart for the district
curSbarchs<- lapply(District[[1]], curSbarch)
#Print the bchart
for(i in seq_along(curSbarchs)){print(curSbarchs[[1]])
}
```

## Row

### Chart -1.4 Sought Health Facility

```{r, souhfbarch}
souhfbarch <- function(district_name) {

subtowndata0 <- subset(Hypottowndata1, District == district_name)

distsouhfild0 <-subset(subtowndata0, current_sick != "No")

Sumbydistsouhfd0 <-distsouhfild0 %>% group_by(sought_hf) %>% summarise(n=n())

Percbydistsouhfd0  <- Sumbydistsouhfd0 %>%
  mutate(percentage = round((n / sum(n))*100, 1))

#Group Bar Chart
Sought_hf_Chart<- ggplot(Percbydistsouhfd0, aes(x = " ", y = percentage, fill = sought_hf)) +
  geom_col(position = "dodge") +
  labs(title = paste("District", district_name, "Household Members' Sought Health Facility for the Current Sickness, Town X, Country X", date1),
       subtitle = stringr::str_glue("Analysis of a Hypothetical Community Health Data for Exercise \nn = {nrow(distsouhfild0)} (Hypottowndata.csv)"),
       x= paste("District", district_name),
       y= "Household Members, %") +
  theme(axis.text.x = element_text(angle = 90))
 }
```

```{r souhfbarch1}
#create bchart for the district
souhfbarchs<- lapply(District[[1]], souhfbarch)
#Print the bchart
for(i in seq_along(souhfbarchs)){print(souhfbarchs[[1]])
}
```

### Chart - 1.5 Main Percieved community health problem (Main_PCHP)

```{r, MPCHPbarch}
MPCHPbarch <- function(district_name) {

subtowndata0 <- subset(Hypottowndata1, District == district_name)

Sumbydistchpfild0 <-subtowndata0  %>% filter(Relation_to_hhh == "Household Head")

Sumbydistchpd0 <-Sumbydistchpfild0 %>% group_by(Main_PCHP) %>% summarise(n=n())

Percbydistchpd0  <- Sumbydistchpd0 %>%
  mutate(percentage = round((n / sum(n))*100, 1)) %>%
  arrange(by=Main_PCHP, desc(percentage))

# Group Bar Chart
Main_PCHP_Chart <-
  ggplot(Percbydistchpd0, aes(x = " ", y = percentage,
                           fill = Main_PCHP)) +
  geom_col(position = "dodge") +
  labs(title = paste("District", district_name, "Household Members' Main Perceived Community Health Problem,Town X, Country X", date1),
       subtitle = stringr::str_glue("Analysis of a Hypothetical Community Health Data for Exercise \nn = {nrow(Sumbydistchpfild0)} (Hypottowndata.csv)"),
       x= paste("District", district_name),
       y= "Household Members, %") +
  theme(axis.text.x = element_text(angle = 90)) +
  coord_flip()
}
```

```{r MPCHPbarch1}
#create bchart for the district
MPCHPbarchs<- lapply(District[[1]], MPCHPbarch)
#Print the bchart
for(i in seq_along(MPCHPbarchs)){print(MPCHPbarchs[[1]])
}
```

# District 2 Dashboard

## Column {.tabset data-width=500}

### Chart - 2.1 Sex

```{r sexpie2}
#create a list of plots for each region
plots2<- lapply(District, crsexpie)
#Print each plot
for(i in seq_along(plots)){print(plots2[[2]])
}
```

## Column {data-width=500}

### Chart-2.2 Known Sickness {.no-padding} 

```{r, KnSbarch2}
#create bchart for the district
bcharts2<- lapply(District, crKnSbarch)
#Print the bchart
for(i in seq_along(bcharts)){print(bcharts2[[2]])
}
```

### Chart-2.3 Current Sickness (symptoms) {.no-padding}

```{r, curSbarch2}
#create bchart for the district
curSbarchs2<- lapply(District, curSbarch)
#Print the bchart
for(i in seq_along(curSbarchs)){print(curSbarchs2[[2]])
}
```

## Row

### Chart -2.4 Sought Health Facility

```{r souhfbarch2}
#create bchart for the district
souhfbarchs2<- lapply(District, souhfbarch)
#Print the bchart
for(i in seq_along(souhfbarchs)){print(souhfbarchs2[[2]])
}
```

### Chart - 2.5 Main Percieved community health problem (Main_PCHP)

```{r MPCHPbarch2}
#create bchart for the district
MPCHPbarchs2<- lapply(District, MPCHPbarch)
#Print the bchart
for(i in seq_along(MPCHPbarchs)){print(MPCHPbarchs2[[2]])
}
```

# District 3 Dashboard

## Column {.tabset data-width=500}

### Chart -3.1 Sex

```{r sexpie3}
#create a list of plots for each region
plots3<- lapply(District, crsexpie)
#Print each plot
for(i in seq_along(plots)){print(plots3[[3]])}
```

## Column {data-width=500}

### Chart-3.2 Known Sickness {.no-padding}

```{r, KnSbarch3}
#create bchart for the district
bcharts3<- lapply(District, crKnSbarch)
#Print the bchart
for(i in seq_along(bcharts)){print(bcharts3[[3]])}
```

### Chart-3.3 Current Sickness (symptoms) {.no-padding}

```{r, curSbarch3}
#create bchart for the district
curSbarchs3<- lapply(District, curSbarch)
#Print the bchart
for(i in seq_along(curSbarchs)){print(curSbarchs3[[3]])}
```

## Row

### Chart -3.4 Sought Health Facility

```{r souhfbarch3}
#create bchart for the district
souhfbarchs3<- lapply(District, souhfbarch)
#Print the bchart
for(i in seq_along(souhfbarchs)){print(souhfbarchs3[[3]])}
```

### Chart - 3.5 Main Percieved community health problem (Main_PCHP)

```{r MPCHPbarch3}
#create bchart for the district
MPCHPbarchs3<- lapply(District, MPCHPbarch)
#Print the bchart
for(i in seq_along(MPCHPbarchs)){print(MPCHPbarchs3[[3]])
}
```

# Data Table

### Chart - Data Table of My Hypothetical Community Health Data of Town-X with its three Districts (named as 1, 2 and 3), Country x, `r Sys.Date()`


```{r, mydttable}
datatable(Hypottowndata,
          caption = "My Hypothetical Community Health Data",
          rownames = T,
          filter = "top",
          extensions = "Buttons", options = list(
            dom ='Bfrtip',
            buttons = c('copy', 'print', "csv")
            ))
```

# References {Data-orientation=columns}

## Column {Data-width=100}

### References

1- The Graph Course. EPIREP_EN_parameterizing_reports.Rmd, Accessed on April 30, 2025: (https://thegraphcourses.org/courses/epirep/topics/parametrizing-reports)

2- Batra N., Spina A., Blomquiist P., et al. The Epidemiologist R Handbook, Sep 18, 2024. https://www.epirhandbook.com/en/new_pages/flexdashboard.html

3- https://www.youtube.com/watch?v=_a4S4tq62OE

4- https://www.youtube.com/watch?v=ef8qvUeopN4


# Contact Me {Data-orientation=columns}

## Column {Data-width=100}

### Contact Me

  - Chernet Hailu (MPH, Assist. Prof (On Retirement))
  - Email: chernet2013@gmail.com | chernet_mengesha@yahoo.com