60  Density Plot

60.0.1 Import dataset

Code
library(tidyverse)
Code
df_bp <- 
    readstata13::read.dta13("C:/Dataset/BP.dta") %>% 
    select(sex, sbp, dbp, saltadd) %>% 
    pivot_longer(
        cols = c(sbp, dbp), 
        values_to = "pressure", 
        names_to = "bp_type")

dataF <- readstata13::read.dta13("C:/Dataset/olivia_data_wide.dta")

60.0.2 Primary density plot

Code
df_bp %>% 
    filter(bp_type == "sbp") %>% 
    ggplot(aes(x = pressure)) +
    geom_density(
        color = "blue", 
        fill = 'red', 
        linetype = "dashed", 
        alpha = 0.2) +
    theme_light()
Figure 60.1: Density plot of the Systolic blood pressures

60.0.3 Density with separate colors

Code
df_bp %>% 
    filter(bp_type == "sbp") %>% 
    drop_na(saltadd) %>% 
    ggplot(aes(x = pressure, color = saltadd, fill = saltadd)) +
    geom_density(
        linetype = "dashed", alpha = 0.2) +
    theme_light()+
    scale_color_brewer(palette = "Dark2")
Figure 60.2: Density plot of the Systolic blood pressures

60.0.4 Densityplot with facets

Code
df_bp %>% 
    drop_na(saltadd) %>% 
    ggplot(aes(x = pressure, color = saltadd)) +
    geom_density(aes(fill = saltadd), linetype = "dashed", alpha = 0.2) +
    theme_light()+
    scale_color_brewer(palette = "Dark2") +
    facet_grid(bp_type ~ sex)
Figure 60.3: Density plot of the Systolic blood pressures

60.0.5 ggridges plot

Code
df_bp %>% 
    drop_na(saltadd) %>% 
    ggplot(aes(x = pressure, fill = bp_type)) +
    ggridges::geom_density_ridges(aes(y = saltadd), alpha = .3) +
    labs(x = "Pressure", 
         y = "Salt added to diet") +
    ggridges::theme_ridges(font_size = 12) +
    scale_fill_discrete(
        name = "Blood Pressure Type", 
        labels = c("sbp" = "Systolic", "dbp" = "Diastolic")) +
    theme(legend.position = "right")
Picking joint bandwidth of 11.5
Figure 60.4: Comparative Systolic and Diastolic Blood Pressue for salt addiotin and sex
Code
dataF %>% 
    select(mcv1, mcv2, mcv3, mcv4, mcv5, agecat, id) %>%
    pivot_longer(cols = mcv1:mcv5, names_to = "Time", values_to = "MCV") %>% 
    ggplot(aes(x = MCV, fill = Time)) +
    ggridges::geom_density_ridges(aes(y = agecat), alpha = .5) +
    labs(x = "MCV", 
         y = "Age Group Category (years)",
         title = "Sequential changes in MCV over the study duration per age category") +
    ggridges::theme_ridges() +
    scale_fill_discrete(name = "Measure", 
                        labels = c("mcv1" = "First", 
                                   "mcv2" = "Second", 
                                   "mcv3" = "Third",
                                   "mcv4" = "Fourth",
                                   "mcv5" = "Fifth")) +
    theme(legend.position = "right")
Picking joint bandwidth of 3.72