Another very common visual tool for analysis is a heatmap. We’ve seen through our previous assignments and readings that these are useful for taking frequency or count data and showing their distribution. There are many applications for this type of figure, including tracking abundances of species across functional units, making comparisons between treatments, etc. The data set we will be working with for this assignment comes from drug/substance data. Columns display the measurements as percentage of the age group that used a certain substance in a 12-month period, as well as the median number of times an individual used in the 12-month period.
Complete the prompts below and be sure to answer all questions as completely as possible!
#install.packages("tidyverse")
#install.packages("patchwork")
#install.packages("ggplot2")
#install.packages("RColorBrewer")
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.4.3
## Warning: package 'tibble' was built under R version 4.4.3
## Warning: package 'tidyr' was built under R version 4.4.3
## Warning: package 'readr' was built under R version 4.4.3
## Warning: package 'purrr' was built under R version 4.4.3
## Warning: package 'dplyr' was built under R version 4.4.3
## Warning: package 'lubridate' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.0 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.2 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(patchwork)
library(ggplot2)
library(RColorBrewer)
druguse<-read.csv('drug-use-by-age.csv',
header=T,
na.strings = "-")
druguse[,3:28] <- sapply(druguse[,3:28],as.numeric)
use as percentage of the age group that used
in the 12-month period, and frequency as the median number
of times an individual used in the 12-month period), but categories are
split among columns.One tidy format would have use and
frequency as their own columns, with drug_type
as indicating the type of drug. Converting to this format would allow
for easier and more direct analysis. Run the code chunk below to rework
the data:
Our steps will be:
age and
n.names_pattern argument to split these column
titles by drug type and measurement type (“use” versus “frequency”)use and
frequency as their own variables.druguse %>%
pivot_longer(cols = -c(age,n),
names_pattern = "(.*)_(.*)",
names_to=c("drug_type","measurement")) %>%
pivot_wider(names_from = measurement,
values_from = value)->drugusetidy
drugusetidy %>%
ggplot(data =.,
aes(x = age,
y = drug_type,
fill = use))+
geom_tile()+
scale_fill_gradientn("Percentage Of\nAge Group Using\nIn The Past Year",
colors = brewer.pal(9,name="YlOrRd"))+
theme(axis.text.x = element_text(angle = 60, hjust=1))+
labs(x="Age Group",
y="Drug Type",
title="Drug-use Percentages by Age")+
theme(plot.background = element_rect(fill="gray"),
legend.background = element_rect(fill="gray"))
The heat map shows that alcohol has the highest use especially around ages 21–25. Marijuana use is also high for people in later teens and early twenties.
drugusetidy %>%
filter(drug_type %in% c("marijuana","pain_releiver")) %>%
ggplot(data =.,
aes(x = age,
y = drug_type,
fill = use))+
geom_tile()+
scale_fill_gradientn("Percentage Of\nAge Group Using\nIn The Past Year",
colors = brewer.pal(9,name="YlOrRd"))+
theme(axis.text.x = element_text(angle = 60, hjust=1))+
labs(x="Age Group",
y="Drug Type",
title="Drug-use Percentages by Age")+
theme(plot.background = element_rect(fill="gray"),
legend.background = element_rect(fill="gray"))
drugusetidy %>%
filter(drug_type %in% c("meth","sedative","cocaine")) %>%
ggplot(data =.,
aes(x = age,
y = drug_type,
fill = use))+
geom_tile()+
scale_fill_gradientn("Percentage Of\nAge Group Using\nIn The Past Year",
colors = brewer.pal(9,name="YlOrRd"))+
theme(axis.text.x = element_text(angle = 60, hjust=1))+
labs(x="Age Group",
y="Drug Type",
title="Drug-use Percentages by Age")+
theme(plot.background = element_rect(fill="gray"),
legend.background = element_rect(fill="gray"))
The heat map shows that cocaine use is highest around ages 18–29 and decreases with age. Meth and sedative use seem to be lower across all age groups.
use
, recreate the figure from 3 but instead of using use, use
frequency (NOTE: you may need to look at the columns in
drugusetidy to get the variable correct)BONUS (2 pts): change the legend title to reflect that we are now looking at a median frequency by age!
drugusetidy %>%
filter(drug_type %in% c("marijuana","pain_releiver")) %>%
ggplot(data =.,
aes(x = age,
y = drug_type,
fill = frequency))+
geom_tile()+
scale_fill_gradientn(" Median Frequency",
colors = brewer.pal(9,name="YlOrRd"))+
theme(axis.text.x = element_text(angle = 60, hjust=1))+
labs(x="Age Group",
y="Drug Type",
title="Drug-use Frequency by Age")+
theme(plot.background = element_rect(fill="gray"),
legend.background = element_rect(fill="gray"))
The heat map above shows that marijuana has a higher frequency of use than pain meds, especially in individuals that are in their 20s-30s, with it peaking at 30-34. pain reliever seems to be low in all age groups except for when it noticibly is larger in frequency at 12.
drugusetidy %>%
filter(drug_type %in% c("meth", "sedative", "cocaine")) %>%
ggplot(data =.,
aes(x = age,
y = drug_type,
fill = frequency))+
geom_tile()+
scale_fill_gradientn(" Median Frequency",
colors = brewer.pal(9,name="YlOrRd"))+
theme(axis.text.x = element_text(angle = 60, hjust=1))+
labs(x="Age Group",
y="Drug Type",
title="Drug-use Frequency by Age")+
theme(plot.background = element_rect(fill="gray"),
legend.background = element_rect(fill="gray"))
The heat map shows that meth has the highest frequency in some age groups, especially ages 19 and 35–49. Sedative use also peaks around ages 50–64. Cocaine has a lower frequency overall throughout all the age groups.