Visualizing the DHS Data

Let us load the data we will need:

dhs14_design <- readRDS("data/dhs14_design.RDS")
dhs.df14 <- readRDS("data/dhs.df14.RDS")

Visualizing Data

Three common ways to generate basic graphics in R are via - base R - lattice - ggplot2

We will skip base R graphics since ggplot2 will be the graphics package for this class. Let us see how we use it, starting with a simple bar-chart

Remember the typical options…

ggplot2 can work with unit-level data (i.e., one row per observation) AND it can also work with calculated values stored in a data-frame (like our survey-weighted estimates from BRFSS/DHS)

we will see both ways in action

Bar-charts

Educational Attainment (educ)

library(ggplot2)
ggplot(data = dhs.df14, aes(x = educat)) + geom_bar()

Educational attainment by Wealth quintiles

ggplot(data = dhs.df14, aes(x = educat, group = wealthq, fill = wealthq)) +
  geom_bar()

ggplot(data = dhs.df14, aes(x = educat, group = wealthq, fill = wealthq)) +
  geom_bar(position = "dodge")

ggplot(data = dhs.df14, aes(x = educat, group = wealthq, fill = wealthq)) +
  geom_bar(position = "dodge") +
  facet_wrap(~ educat)

ggplot(data = dhs.df14, aes(x = educat, group = wealthq, fill = wealthq, y = ..prop..)) +
  geom_bar(position = "dodge") +
  facet_wrap(~ wealthq, ncol = 2) +
  scale_y_continuous(labels = scales::percent) +
  theme(legend.position = "bottom")

Weighted estimates

These are all unweighted estimates; how would we plot the weighted estimates?

library(survey)
library(srvyr)
tab.1 <- svymean(~ educat, dhs14_design, na.rm = TRUE)
tab.1 <- as.data.frame(tab.1) * 100
tab.1 <- round(tab.1, digits = 1)
tab.1
                           mean  SE
educatno education         24.0 0.6
educatincomplete primary    6.1 0.2
educatcomplete primary      4.1 0.2
educatincomplete secondary 13.3 0.3
educatcomplete secondary   38.5 0.6
educathigher               13.9 0.5
tab.1p <- tab.1 %>% 
  mutate(educ = rownames(tab.1),
         educ = stringr::str_replace(educ, "educat", ""),
         educ = stringr::str_to_title(educ),
         educ = ordered(educ, 
                        levels = c("No Education", "Incomplete Primary",
                                   "Complete Primary", "Incomplete Secondary", 
                                   "Complete Secondary", "Higher")
                        )
         )

knitr::kable(tab.1p)
mean SE educ
24.0 0.6 No Education
6.1 0.2 Incomplete Primary
4.1 0.2 Complete Primary
13.3 0.3 Incomplete Secondary
38.5 0.6 Complete Secondary
13.9 0.5 Higher
library(ggplot2)
ggplot(data = tab.1p, aes(x = educ, y = mean)) +
  geom_bar(stat = "identity") 

Weighted Educational attainment by Wealth quintiles?

tab.ew <- svyby(~educat, ~wealthq, dhs14_design, svymean, na.rm = TRUE)
tab.ew <- tab.ew[, c(1:7)] 
tab.ew2 <-  tab.ew[, c(2:7)] * 100  
tab.ew2 <- round(tab.ew2, digits  = 1)

knitr::kable(tab.ew2)
educatno education educatincomplete primary educatcomplete primary educatincomplete secondary educatcomplete secondary educathigher
poorest 47.7 8.6 4.3 13.8 22.7 3.0
poorer 37.4 8.9 4.9 14.8 30.4 3.6
middle 18.9 6.1 3.7 14.6 46.5 10.2
richer 14.0 5.1 5.0 13.8 44.8 17.3
richest 5.4 2.3 2.8 9.4 45.3 34.8
library(tidyr)
tab.ew3 <- tab.ew2 %>% 
  mutate(Wealth = rownames(tab.ew2)) %>% 
  gather(Education, Percent, 1:6)  %>% 
  mutate(Education = substring(Education, 7),
         Education = stringr::str_to_title(Education)
         ) %>% 
  mutate(Education2 = ordered(Education, 
                        levels = c("No Education", "Incomplete Primary",
                                   "Complete Primary", "Incomplete Secondary", 
                                   "Complete Secondary", "Higher")
                        ),
         Wealth = stringr::str_to_title(Wealth)
         ) %>% 
  mutate(Wealth2 = ordered(Wealth, 
                           levels = c("Poorest", "Poorer",
                                      "Middle", "Richer",
                                      "Richest")
                           )
         )

knitr::kable(tab.ew3)
Wealth Education Percent Education2 Wealth2
Poorest No Education 47.7 No Education Poorest
Poorer No Education 37.4 No Education Poorer
Middle No Education 18.9 No Education Middle
Richer No Education 14.0 No Education Richer
Richest No Education 5.4 No Education Richest
Poorest Incomplete Primary 8.6 Incomplete Primary Poorest
Poorer Incomplete Primary 8.9 Incomplete Primary Poorer
Middle Incomplete Primary 6.1 Incomplete Primary Middle
Richer Incomplete Primary 5.1 Incomplete Primary Richer
Richest Incomplete Primary 2.3 Incomplete Primary Richest
Poorest Complete Primary 4.3 Complete Primary Poorest
Poorer Complete Primary 4.9 Complete Primary Poorer
Middle Complete Primary 3.7 Complete Primary Middle
Richer Complete Primary 5.0 Complete Primary Richer
Richest Complete Primary 2.8 Complete Primary Richest
Poorest Incomplete Secondary 13.8 Incomplete Secondary Poorest
Poorer Incomplete Secondary 14.8 Incomplete Secondary Poorer
Middle Incomplete Secondary 14.6 Incomplete Secondary Middle
Richer Incomplete Secondary 13.8 Incomplete Secondary Richer
Richest Incomplete Secondary 9.4 Incomplete Secondary Richest
Poorest Complete Secondary 22.7 Complete Secondary Poorest
Poorer Complete Secondary 30.4 Complete Secondary Poorer
Middle Complete Secondary 46.5 Complete Secondary Middle
Richer Complete Secondary 44.8 Complete Secondary Richer
Richest Complete Secondary 45.3 Complete Secondary Richest
Poorest Higher 3.0 Higher Poorest
Poorer Higher 3.6 Higher Poorer
Middle Higher 10.2 Higher Middle
Richer Higher 17.3 Higher Richer
Richest Higher 34.8 Higher Richest
ggplot(data = tab.ew3, aes(x = Education2, y = Percent, group = Wealth2, fill = Education2)) +
  geom_bar(stat = "identity") +
  facet_wrap(~Wealth2, ncol = 5) + 
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 90))

Histograms with hemoglobin concentration

hemoglobinData <- read.csv(url("http://whitlockschluter.zoology.ubc.ca/wp-content/data/chapter02/chap02e3cHumanHemoglobinElevation.csv"))

ggplot(data = hemoglobinData, aes(x = hemoglobin)) + geom_histogram(fill="cornflowerblue", color = "white") + 
  labs(x = "Hemoglobin in grams (gm) per deciliter (dL)",  
       y = "Frequency", 
       title = "Histogram of Hemoglobin Concentration in Adult Males")