#################################################################################################### # For: Norway # Paper: Mental Health Prevalences # Programmer: Renate Houts # File: 03_Plots_Sept2023.R # Date: 27-September-2023 # # Purpose: # RMH -- Prepare plots from files created in SAS # # ################################################################################################## # Load necessary libraries library(plyr) # Needed for mapvalues; plyr needs to be loaded before tidyverse library(tidyverse) # Needed for data wrangling library(data.table) library(plotly) library(ggplot2) library(waffle) # File that maps ICPC codes to Categories and Conditions chapter.letters <- c("Psychological", "", "A", "B", "D", "F", "H", "K", "L", "N", "R", "S", "T", "U", "W", "X") chapter.labels <- c("Psychological", "", "General and Unspecified", "Blood, Blood Forming Organs and Immune Function", "Digestive", "Eye", "Ear", "Cardiovascular", "Musculoskeletal", "Neurological", "Respiratory", "Skin", "Endocrine/Metabolic and Nutritional", "Urogenital", "Pregnancy, Childbearing, Family Planning", "Male/Female Genital") chapter.order <- c("Psychological", "", "Musculoskeletal", "Cardiovascular", "Respiratory", "General and Unspecified", "Skin", "Endocrine/Metabolic and Nutritional", "Digestive", "Neurological", "Urogenital", "Male/Female Genital", "Pregnancy, Childbearing, Family Planning", "Eye", "Ear", "Blood, Blood Forming Organs and Immune Function") category.order <- c("Psychological", "P-dx", "", "Circulatory System", "Pulmonary System and Allergy", "Endocrine System", "Neurological System", "Cancers", "Musculoskeletal System", "Hematological System", "Gastrointestinal System", "Urogenital System") infpain.order <- c("Psychological", "", "Infection", "Pain", "Injury") mh.order <- c("Psychological", "", "Depression", "Sleep disturbance", "Acute stress reaction", "Anxiety", "Dementia/Memory problems", "Substance abuse", "Psychosis", "ADHD", "Phobia/Compulsive disorder", "Sexual concern", "Child/Adolescent behavior symptom/complaint", "Developmental delay/Learning problems", "PTSD", "Personality disorder", "Continence issues", "Suicide/Suicide attempt", "Neuresthenia/surmenage (chronic fatigue)", "Somatization", "Fear of mental disorder", "Eating disorder", "Phase of life problem adult", "Stammering/stuttering/tic", "Feeling/behaving irritable/angry", "Other psychological symptom/disease") mh.order2 <- c("Depression", "Anxiety", "Sleep disturbance", "Other psychological symptom/disease", "Substance abuse", "Acute stress reaction", "Psychosis", "Dementia/Memory problems", "ADHD", "Phobia/Compulsive disorder", "Developmental delay/Learning problems", "PTSD", "Personality disorder", "Child/Adolescent behavior symptom/complaint", "Sexual concern", "Neuresthenia/surmenage (chronic fatigue)", "Eating disorder", "Somatization", "Continence issues", "Suicide/Suicide attempt", "Phase of life problem adult", "Stammering/stuttering/tic", "Fear of mental disorder", "Feeling/behaving irritable/angry") # Read in data files pers_enc <- read.csv("C:/Users/rh93/Box/Duke_DPPPlab/Renate2015/2022_Norway/2023_DayInLifeGP/ExportsFromNorway/PersonEncounter_GT10_Sept2023.csv") pers_enc_age <- read.csv("C:/Users/rh93/Box/Duke_DPPPlab/Renate2015/2022_Norway/2023_DayInLifeGP/ExportsFromNorway/PersonEncounterAge_GT10_Sept2023.csv") # Figure 2A. Person_Level Mental Health categories person_mh <- pers_enc %>% filter(code %in% mh.order) Fig2A.plot <- person_mh %>% plot_ly(., x = ~code, y = ~all_p.p, name = "Everyone", type = 'bar', marker = list(color = '#00afef')) %>% add_trace(y = ~female_p.p, name = "Females", type = 'bar', marker = list(color = '#ff6532')) %>% add_trace(y = ~male_p.p, name = "Males", type = 'bar', marker = list(color = '#92d050')) %>% layout(yaxis = list(title = "Percent of People", tickformat = ".0%", tickprefix = "", ticksufffix = "", range = list(0, .6)), xaxis = list(title = "Mental Health Category", tickprefix = "", ticksufffix = "", type = 'category', categoryarray = mh.order)) Fig2A.plot # Figure 2B. Person_Level Mental Health Categories by Age Fig2B.plot <- pers_enc_age %>% plot_ly(., x = ~age, y = ~dep_p.p, name = "Depression", type = 'scatter', mode = 'lines', line = list(color = "#1C86EE")) %>% add_trace(y = ~slp_p.p, name = "Sleep disturbance", type = 'scatter', mode = 'lines', line = list(color = "#008B00")) %>% add_trace(y = ~str_p.p, name = "Acute stress reaction", type = 'scatter', mode = 'lines', line = list(color = "#404040")) %>% add_trace(y = ~anx_p.p, name = "Anxiety", type = 'scatter', mode = 'lines', line = list(color = "#E31A1C")) %>% add_trace(y = ~dem_p.p, name = "Dementia/Memory problems", type = 'scatter', mode = 'lines', line = list(color = "#7EC0EE")) %>% add_trace(y = ~sub_p.p, name = "Substance abuse", type = 'scatter', mode = 'lines', line = list(color = "#FF7F00")) %>% add_trace(y = ~psy_p.p, name = "Psychosis", type = 'scatter', mode = 'lines', line = list(color = "#FFD700")) %>% add_trace(y = ~adhd_p.p, name = "ADHD", type = 'scatter', mode = 'lines', line = list(color = "#FB9A99")) %>% add_trace(y = ~phb_p.p, name = "Phobia/Compulsive disorder", type = 'scatter', mode = 'lines', line = list(color = "#90EE90")) %>% add_trace(y = ~sex_p.p, name = "Sexual concern", type = 'scatter', mode = 'lines', line = list(color = "#0000FF")) %>% add_trace(y = ~cha_p.p, name = "Child/Adolescent behavior symptom/complaint", type = 'scatter', mode = 'lines', line = list(color = "#EEE685")) %>% add_trace(y = ~dev_p.p, name = "Developmental delay/Learning problems", type = 'scatter', mode = 'lines', line = list(color = "#CAB2D6")) %>% add_trace(y = ~ptsd_p.p, name = "PTSD", type = 'scatter', mode = 'lines', line = list(color = "#FDBF6F")) %>% add_trace(y = ~per_p.p, name = "Personality disorder", type = 'scatter', mode = 'lines', line = list(color = "#B3B3B3")) %>% add_trace(y = ~con_p.p, name = "Continence issues", type = 'scatter', mode = 'lines', line = list(color = "#EE7942")) %>% add_trace(y = ~sui_p.p, name = "Suicide/Suicide attempt", type = 'scatter', mode = 'lines', line = list(color = "#36648B")) %>% add_trace(y = ~chf_p.p, name = "Neuresthenia/surmenage (chronic fatigue)", type = 'scatter', mode = 'lines', line = list(color = "#FF1493")) %>% add_trace(y = ~fea_p.p, name = "Fear of mental disorder", type = 'scatter', mode = 'lines', line = list(color = "#FF1493")) %>% add_trace(y = ~som_p.p, name = "Somatization", type = 'scatter', mode = 'lines', line = list(color = "#BF3EFF")) %>% add_trace(y = ~eat_p.p, name = "Eating disorder", type = 'scatter', mode = 'lines', line = list(color = "#00FF00")) %>% add_trace(y = ~adl_p.p, name = "Phase of life problem adult", type = 'scatter', mode = 'lines', line = list(color = "#CDCD00")) %>% add_trace(y = ~stu_p.p, name = "Stammering/stuttering/tic", type = 'scatter', mode = 'lines', line = list(color = "#00CED1")) %>% add_trace(y = ~irr_p.p, name = "Feeling/behaving irritable/angry", type = 'scatter', mode = 'lines', line = list(color = "#B4EEB4")) %>% add_trace(y = ~oth_p.p, name = "Other psychological symptom/disease", type = 'scatter', mode = 'lines', line = list(color = "#6A3D9A")) %>% layout(yaxis = list(title = "Percent of People", tickformat = ".0%", range = list(0, 0.12), tickprefix = "", ticksufffix = ""), xaxis = list(title = 'Patient Age', tickprefix = "", ticksufffix = "")) Fig2B.plot # Figure 3A. Encounter_Level Mental Health categories mh.order2 <- c("Depression", "Anxiety", "Sleep disturbance", "Substance abuse", "Acute stress reaction", "Psychosis", "Dementia/Memory problems", "ADHD", "Phobia/Compulsive disorder", "Developmental delay/Learning problems", "PTSD", "Personality disorder", "Child/Adolescent behavior symptom/complaint", "Sexual concern", "Neuresthenia/surmenage (chronic fatigue)", "Eating disorder", "Somatization", "Continence issues", "Suicide/Suicide attempt", "Phase of life problem adult", "Stammering/stuttering/tic", "Fear of mental disorder", "Feeling/behaving irritable/angry", "Other psychological symptom/disease") tilesize <- 10000 # N per tile num_columns <- 50 # N tiles per column encounter_mh <- pers_enc %>% filter(code %in% mh.order2) %>% select(code, all_n.e, all_p.e) %>% arrange(factor(code, levels = mh.order2)) waffle_data <- encounter_mh %>% rowwise() %>% summarize(Name = code, id = 0:(all_n.e/tilesize)) %>% ungroup() %>% mutate(global_id = row_number()) Fig3A.plot <- ggplot(waffle_data, aes(y = (global_id - 1) %% num_columns, x = (global_id - 1) %/% num_columns, fill = Name)) + geom_tile(width = 0.9, height = 0.9, color = "black") + coord_fixed(ratio = 1) + scale_x_continuous(expand = c(0,0), name = "") + scale_y_continuous(expand = c(0,0), name = "") + scale_fill_manual(values = mh.colors <- c("#404040", "#FB9A99", "#E31A1C", "#EEE685", "#EE7942", "#7EC0EE", "#1C86EE", "#CAB2D6", "#00FF00", "#FF83FA", "#B4EEB4", "#FF1493", "#6A3D9A", "#B3B3B3", "#CDCD00", "#90EE90", "#FFD700", "#FDBF6F", "#0000FF", "#008B00", "#BF3EFF", "#00CED1", "#FF7F00", "#36648B")) + theme_minimal() + guides(fill=guide_legend(ncol = 1)) Fig3A.plot enc_mh <- pers_enc %>% filter(code %in% mh.order2) %>% mutate(code.f = factor(code, levels = mh.order2), all_n10K = round(all_n.e/10000, digits = 0)) %>% arrange(all_n10K) %>% select(code, all_n10K) Fig3A.plot.1 <- waffle(enc_mh, rows = 50, size = 0.9, colors = c("#404040", "#FB9A99", "#E31A1C", "#EEE685", "#EE7942", "#7EC0EE", "#1C86EE", "#CAB2D6", "#00FF00", "#FF83FA", "#B4EEB4", "#FF1493", "#6A3D9A", "#B3B3B3", "#CDCD00", "#90EE90", "#FFD700", "#FDBF6F", "#0000FF", "#008B00", "#BF3EFF", "#00CED1", "#FF7F00", "#36648B")) Fig3A.plot.1 # Figure 3B. Encounter_Level Mental Health Codes by Age Fig3B.plot <- pers_enc_age %>% plot_ly(., x = ~age, y = ~dep_p.e, name = "Depression", type = 'scatter', mode = 'lines', line = list(color = "#1C86EE")) %>% add_trace(y = ~anx_p.e, name = "Anxiety", type = 'scatter', mode = 'lines', line = list(color = "#E31A1C")) %>% add_trace(y = ~slp_p.e, name = "Sleep disturbance", type = 'scatter', mode = 'lines', line = list(color = "#008B00")) %>% add_trace(y = ~sub_p.e, name = "Substance abuse", type = 'scatter', mode = 'lines', line = list(color = "#FF7F00")) %>% add_trace(y = ~str_p.e, name = "Acute stress reaction", type = 'scatter', mode = 'lines', line = list(color = "#404040")) %>% add_trace(y = ~psy_p.e, name = "Psychosis", type = 'scatter', mode = 'lines', line = list(color = "#FFD700")) %>% add_trace(y = ~dem_p.e, name = "Dementia/Memory problems", type = 'scatter', mode = 'lines', line = list(color = "#7EC0EE")) %>% add_trace(y = ~adhd_p.e, name = "ADHD", type = 'scatter', mode = 'lines', line = list(color = "#FB9A99")) %>% add_trace(y = ~phb_p.e, name = "Phobia/Compulsive disorder", type = 'scatter', mode = 'lines', line = list(color = "#90EE90")) %>% add_trace(y = ~dev_p.e, name = "Developmental delay/Learning problems", type = 'scatter', mode = 'lines', line = list(color = "#CAB2D6")) %>% add_trace(y = ~ptsd_p.e, name = "PTSD", type = 'scatter', mode = 'lines', line = list(color = "#FDBF6F")) %>% add_trace(y = ~per_p.e, name = "Personality disorder", type = 'scatter', mode = 'lines', line = list(color = "#B3B3B3")) %>% add_trace(y = ~cha_p.e, name = "Child/Adolescent behavior symptom/complaint", type = 'scatter', mode = 'lines', line = list(color = "#EEE685")) %>% add_trace(y = ~sex_p.e, name = "Sexual concern", type = 'scatter', mode = 'lines', line = list(color = "#0000FF")) %>% add_trace(y = ~chf_p.e, name = "Neuresthenia/surmenage (chronic fatigue)", type = 'scatter', mode = 'lines', line = list(color = "#FF1493")) %>% add_trace(y = ~eat_p.e, name = "Eating disorder", type = 'scatter', mode = 'lines', line = list(color = "#00FF00")) %>% add_trace(y = ~som_p.e, name = "Somatization", type = 'scatter', mode = 'lines', line = list(color = "#BF3EFF")) %>% add_trace(y = ~con_p.e, name = "Continence issues", type = 'scatter', mode = 'lines', line = list(color = "#EE7942")) %>% add_trace(y = ~sui_p.e, name = "Suicide/Suicide attempt", type = 'scatter', mode = 'lines', line = list(color = "#36648B")) %>% add_trace(y = ~adl_p.e, name = "Phase of life problem adult", type = 'scatter', mode = 'lines', line = list(color = "#CDCD00")) %>% add_trace(y = ~stu_p.e, name = "Stammering/stuttering/tic", type = 'scatter', mode = 'lines', line = list(color = "#00CED1")) %>% add_trace(y = ~fea_p.e, name = "Fear of mental disorder", type = 'scatter', mode = 'lines', line = list(color = "#FF83FA")) %>% add_trace(y = ~irr_p.e, name = "Feeling/behaving irritable/angry", type = 'scatter', mode = 'lines', line = list(color = "#B4EEB4")) %>% add_trace(y = ~oth_p.e, name = "Other psychological symptom/disease", type = 'scatter', mode = 'lines', line = list(color = "#6A3D9A")) %>% layout(yaxis = list(title = "Percent of Encounters", tickformat = ".0%", range = list(0, 0.06), tickprefix = "", ticksufffix = ""), xaxis = list(title = "Patient Age at Encounter", tickprefix = "", ticksufffix = "")) Fig3B.plot # Figure 4A. Encounter_Level Disease chapters encounter_chapter <- pers_enc %>% filter(code %in% chapter.letters) %>% mutate(chapter = mapvalues(code, from = chapter.letters, to = chapter.labels)) Fig4A.plot <- encounter_chapter %>% plot_ly(., x = ~chapter, y = ~all_p.e, name = "Everyone", type = 'bar') %>% add_trace(y = ~female_p.e, name = "Females", type = 'bar') %>% add_trace(y = ~male_p.e, name = "Males", type = 'bar') %>% layout(yaxis = list(title = "Percent of Encounters", tickformat = ".0%", range = list(0, .2)), xaxis = list(title = "ICPC-2 Chapter", type = 'category', categoryarray = chapter.order)) Fig4A.plot # For Table 1 person_chapter <- encounter_chapter %>% select(chapter, all_n.p, all_p.p, all_n.e, all_p.e) # Figure 4B. Encounter_Level Disease chapters (Age) Fig4B.plot <- pers_enc_age %>% plot_ly(., x = ~age, y = ~P_p.e, name = "Psychological", type = 'scatter', mode = 'lines', line = list(color = "#FAAA18", width = 4)) %>% add_trace(y = ~L_p.e, name = "Musculoskeletal", type = 'scatter', mode = 'lines', line = list(color = "#48ACAD", width = 2)) %>% add_trace(y = ~K_p.e, name = "Cardiovascular", type = 'scatter', mode = 'lines', line = list(color = "#6A9EDA", width = 2)) %>% add_trace(y = ~R_p.e, name = "Respiratory", type = 'scatter', mode = 'lines', line = list(color = "#A782AD", width = 2)) %>% add_trace(y = ~A_p.e, name = "General and Unspecified", type = 'scatter', mode = 'lines', line = list(color = "#376CA7", width = 2)) %>% add_trace(y = ~S_p.e, name = "Skin", type = 'scatter', mode = 'lines', line = list(color = "#1A6481", width = 2)) %>% add_trace(y = ~T_p.e, name = "Endocrine/Metabolic & Nutritional", type = 'scatter', mode = 'lines', line = list(color = "#744896", width = 2)) %>% add_trace(y = ~D_p.e, name = "Digestive", type = 'scatter', mode = 'lines', line = list(color = "#808080", width = 2)) %>% add_trace(y = ~N_p.e, name = "Neurological", type = 'scatter', mode = 'lines', line = list(color = "#C2B461", width = 2)) %>% add_trace(y = ~U_p.e, name = "Urological", type = 'scatter', mode = 'lines', line = list(color = "#2B8B8D", width = 2)) %>% add_trace(y = ~X_p.e, name = "Male/Female Genital", type = 'scatter', mode = 'lines', line = list(color = "#C16FA3", width = 2)) %>% add_trace(y = ~W_p.e, name = "Pregnancy, Childbearing, Family Planning", type = 'scatter', mode = 'lines', line = list(color = "#A3A3A3", width = 2)) %>% add_trace(y = ~F_p.e, name = "Eye", type = 'scatter', mode = 'lines', line = list(color = "#8B8032", width = 2)) %>% add_trace(y = ~H_p.e, name = "Ear", type = 'scatter', mode = 'lines', line = list(color = "#A84137", width = 2)) %>% add_trace(y = ~B_p.e, name = "Blood, Blood Forming Organs & Immune Function", type = 'scatter', mode = 'lines', line = list(color = "#882D58", width = 2)) %>% layout(yaxis = list(title = "Percent of Encounters", tickformat = ".0%", dtick = .1, range = list(0, 0.4), tickprefix = "", ticksufffix = ""), xaxis = list(title = "Patient Age at Encounter", tickprefix = "", ticksufffix = "")) Fig4B.plot # Figure 4c. Encounter_Level Disease categories encounter_category <- pers_enc %>% filter(code %in% category.order) Fig4C.plot <- encounter_category %>% plot_ly(., x = ~code, y = ~all_p.e, name = "Everyone", type = 'bar') %>% add_trace(y = ~female_p.e, name = "Females", type = 'bar') %>% add_trace(y = ~male_p.e, name = "Males", type = 'bar') %>% layout(yaxis = list(title = "Percent of Encounters", tickformat = ".0%", range = list(0, .2)), xaxis = list(title = "Disease Category", type = 'category', categoryarray = category.order)) Fig4C.plot # Figure 4D. MH vs Infections, Pain, Injuries encounter_infpain <- pers_enc %>% filter(code %in% infpain.order) Fig4D.plot <- encounter_infpain %>% plot_ly(., x = ~code, y = ~all_p.e, name = "Everyone", type = 'bar') %>% add_trace(y = ~female_p.e, name = "Females", type = 'bar') %>% add_trace(y = ~male_p.e, name = "Males", type = 'bar') %>% layout(yaxis = list(title = "Percent of Encounters", tickformat = ".0%", range = list(0, .2)), xaxis = list(title = "", type = 'category', categoryarray = infpain.order)) Fig4D.plot reticulate::conda_install('r-reticulate', 'python-kaleido') reticulate::conda_install('r-reticulate', 'plotly', channel = 'plotly') reticulate::use_miniconda('r-reticulate') reticulate::py_run_string("import sys") save_image(Fig2B.plot, file = "C:/Users/rh93/Box/Duke_DPPPlab/Renate2015/2022_Norway/2023_DayInLifeGP/Caspi_Fig2B_27Sept2023.pdf", format = pdf) save_image(Fig3B.plot, file = "C:/Users/rh93/Box/Duke_DPPPlab/Renate2015/2022_Norway/2023_DayInLifeGP/Caspi_Fig3B_27Sept2023.pdf", format = pdf) save_image(Fig4B.plot, file = "C:/Users/rh93/Box/Duke_DPPPlab/Renate2015/2022_Norway/2023_DayInLifeGP/Caspi_Fig4B_27Sept2023.pdf", format = pdf) save_image(Fig2B.plot, file = "C:/Users/rh93/Box/Duke_DPPPlab/Renate2015/2022_Norway/2023_DayInLifeGP/Caspi_Fig2B_27Sept2023.png", format = png, width = 4*300, height = 2.5*300) save_image(Fig3B.plot, file = "C:/Users/rh93/Box/Duke_DPPPlab/Renate2015/2022_Norway/2023_DayInLifeGP/Caspi_Fig3B_27Sept2023.png", format = png, width = 4*300, height = 2.5*300) save_image(Fig4B.plot, file = "C:/Users/rh93/Box/Duke_DPPPlab/Renate2015/2022_Norway/2023_DayInLifeGP/Caspi_Fig4B_27Sept2023.png", format = png, width = 4*300, height = 2.5*300)