## Prevalence of Drug Use Disorders by age(Part 2) * Prevalence, "Share (%) of population suffering from drug use disorders By Age. " ### Software required to recreate plot: * Install: R language, RTools40 and RStudio (see link below) * Packages: GGPLot2, dplyr, janitor. * Download code and data from Github.com (see link below). * Move R code and data to working directory => https://youtu.be/k_6L-zaRlK0 Youtube => https://git.skyjake.fi/skyjake/lagrange/releases Gemini Browser => gemini://gemlog.blue/users/cariboudatascience/ Caribou Data Science(Capsule) ### Code: ``` library(ggplot2) library(scales) library(dplyr) library(readr) library(tidyr) library(janitor) rm(list=ls()) df <- read_csv("./prevalence-of-drug-use-disorders-by-age.csv") %>% filter(Code =="USA") %>% select(-Entity,-Code) colnames(df) <- c("Year","age_14","age_19","age_24", "age_29","age_34","all_age_percent","age5_14","age15_49", "age_70","standard_percent") df <- df %>% select(Year,age_14,age_19,age_24,age_29,age_34,age5_14, age15_49,age_70,all_age_percent,standard_percent) * Two Quick Plots: ggplot(df) + geom_line(aes(x=Year,y=all_age_percent)) + labs(title="All Age Precent") ggplot(df) + geom_line(aes(x=Year,y=standard_percent)) + labs(title="Age-standardized Percent") + * User dplyr pivot_longer to re-arrange longer age_longer <- pivot_longer(df,cols=age_14:standard_percent) age_longer$Percent <- age_longer$value /100 head(age_longer) * Age 5-14 and Age 15-49 age_longer %>% filter(name=="age5_14") %>% ggplot() + geom_line(aes(x=Year,y=Percent,col=name)) + labs(title="Drug by Year for Ages 5 - 14", y="Percent of Drug Use") + scale_y_continuous(labels=percent) age_longer %>% filter(name=="age15_49") %>% ggplot() + geom_line(aes(x=Year,y=Percent,col=name)) + labs(title="Drug by Year for Ages 15 - 49",y="Percent of Drug Use") + scale_y_continuous(labels=percent) * Percent by Age Groups: 10 - 70 short_list <-age_longer %>% filter(! (name %in% c("age15_49","all_age_percent","standard_percent","age5_14"))) ggplot(short_list) + geom_line(aes(x=Year,y=Percent,col=name)) + scale_y_continuous(labels=percent) + guides(color = guide_legend(override.aes = list(size = 2))) + labs(title="USA Prevalence of Drug Use Disorders by Age") ``` ### Plot Gallery => https://github.com/davidjayjackson/Gemini-content/blob/main/age-5-15.png Plot of Ages 5 - 15 Years => https://github.com/davidjayjackson/Gemini-content/blob/main/age_15_49.png PLot of Ages 15-49 Years => https://github.com/davidjayjackson/Gemini-content/blob/main/percent_by_age.png Prevalence by Age => https://github.com/davidjayjackson/Gemini-content/blob/main/age-standardized-percent.png Standard Percent (all ages / all sex) ## Resources: => https://github.com/davidjayjackson/Gemini-content R Code, data => https://cloud.r-project.org/ R Language and Rtools40 (click and "base" and "Rtools") => https://rstudio.com/products/rstudio/download/ Download and install RStudio