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!
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── 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"))
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("hallucinogen","cocaine","heroin")) %>%
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"))
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\nOf Use\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 Frequency by Age"
) +
theme(
plot.background = element_rect(fill="gray"),
legend.background = element_rect(fill="gray")
)
drugusetidy %>%
filter(
drug_type %in% c(
"alcohol",
"marijuana",
"cocaine"
)
) %>%
ggplot(
data = .,
aes(
x = age,
y = drug_type,
fill = frequency
)
) +
geom_tile() +
scale_fill_gradientn(
"Median Frequency\nOf Use\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 Frequency by Age"
) +
theme(
plot.background = element_rect(fill="gray"),
legend.background = element_rect(fill="gray")
)